@@ -192,4 +192,115 @@ describe('usePackageComparison', () => {
192192 expect ( values [ 0 ] ?. status ) . toBe ( 'neutral' )
193193 } )
194194 } )
195+
196+ describe ( 'github metadata' , ( ) => {
197+ it ( 'fetches github stars and issues when repository is on github' , async ( ) => {
198+ const pkgName = 'github-pkg'
199+ vi . stubGlobal (
200+ '$fetch' ,
201+ vi . fn ( ) . mockImplementation ( ( url : string , options ?: { baseURL ?: string } ) => {
202+ const fullUrl = options ?. baseURL ? `${ options . baseURL } ${ url } ` : url
203+ if ( fullUrl . startsWith ( 'https://registry.npmjs.org/' ) ) {
204+ return Promise . resolve ( {
205+ 'name' : pkgName ,
206+ 'dist-tags' : { latest : '1.0.0' } ,
207+ 'repository' : { type : 'git' , url : 'https://github.com/owner/repo' } ,
208+ 'versions' : {
209+ '1.0.0' : { dist : { unpackedSize : 1000 } } ,
210+ } ,
211+ } )
212+ }
213+ if ( fullUrl . includes ( 'ungh.cc/repos/owner/repo' ) ) {
214+ return Promise . resolve ( { repo : { stars : 1500 } } )
215+ }
216+ if ( fullUrl . includes ( '/api/github/issues/owner/repo' ) ) {
217+ return Promise . resolve ( { issues : 50 } )
218+ }
219+ return Promise . resolve ( null )
220+ } ) ,
221+ )
222+
223+ const { status, getFacetValues } = await usePackageComparisonInComponent ( [ pkgName ] )
224+ await vi . waitFor ( ( ) => {
225+ expect ( status . value ) . toBe ( 'success' )
226+ } )
227+
228+ const stars = getFacetValues ( 'githubStars' ) [ 0 ]
229+ const issues = getFacetValues ( 'githubIssues' ) [ 0 ]
230+
231+ expect ( stars ) . toMatchObject ( { raw : 1500 , status : 'neutral' } )
232+ expect ( issues ) . toMatchObject ( { raw : 50 , status : 'neutral' } )
233+ } )
234+
235+ it ( 'skips github fetches for non-github repositories' , async ( ) => {
236+ const pkgName = 'gitlab-pkg'
237+ const fetchMock = vi
238+ . fn ( )
239+ . mockImplementation ( ( url : string , options ?: { baseURL ?: string } ) => {
240+ const fullUrl = options ?. baseURL ? `${ options . baseURL } ${ url } ` : url
241+ if ( fullUrl . startsWith ( 'https://registry.npmjs.org/' ) ) {
242+ return Promise . resolve ( {
243+ 'name' : pkgName ,
244+ 'dist-tags' : { latest : '1.0.0' } ,
245+ 'repository' : { type : 'git' , url : 'https://gitlab.com/owner/repo' } ,
246+ 'versions' : {
247+ '1.0.0' : { dist : { unpackedSize : 1000 } } ,
248+ } ,
249+ } )
250+ }
251+ return Promise . resolve ( null )
252+ } )
253+ vi . stubGlobal ( '$fetch' , fetchMock )
254+
255+ const { status, getFacetValues } = await usePackageComparisonInComponent ( [ pkgName ] )
256+ await vi . waitFor ( ( ) => {
257+ expect ( status . value ) . toBe ( 'success' )
258+ } )
259+
260+ expect ( fetchMock ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'ungh.cc' ) )
261+ expect ( fetchMock ) . not . toHaveBeenCalledWith ( expect . stringContaining ( '/api/github/issues' ) )
262+
263+ expect ( getFacetValues ( 'githubStars' ) [ 0 ] ) . toBeNull ( )
264+ expect ( getFacetValues ( 'githubIssues' ) [ 0 ] ) . toBeNull ( )
265+ } )
266+ } )
267+
268+ describe ( 'createdAt facet' , ( ) => {
269+ it ( 'displays the creation date without status' , async ( ) => {
270+ const createdDate = '2020-01-01T00:00:00.000Z'
271+ vi . stubGlobal (
272+ '$fetch' ,
273+ vi . fn ( ) . mockImplementation ( ( url : string , options ?: { baseURL ?: string } ) => {
274+ const fullUrl = options ?. baseURL ? `${ options . baseURL } ${ url } ` : url
275+ if ( fullUrl . startsWith ( 'https://registry.npmjs.org/' ) ) {
276+ return Promise . resolve ( {
277+ 'name' : 'test-package' ,
278+ 'dist-tags' : { latest : '1.0.0' } ,
279+ 'time' : {
280+ 'created' : createdDate ,
281+ '1.0.0' : createdDate ,
282+ } ,
283+ 'versions' : {
284+ '1.0.0' : { dist : { unpackedSize : 1000 } } ,
285+ } ,
286+ } )
287+ }
288+ return Promise . resolve ( null )
289+ } ) ,
290+ )
291+
292+ const { status, getFacetValues } = await usePackageComparisonInComponent ( [ 'test-package' ] )
293+ await vi . waitFor ( ( ) => {
294+ expect ( status . value ) . toBe ( 'success' )
295+ } )
296+
297+ const value = getFacetValues ( 'createdAt' ) [ 0 ]
298+ expect ( value ) . toMatchObject ( {
299+ raw : createdDate ,
300+ display : createdDate ,
301+ type : 'date' ,
302+ } )
303+ expect ( value ?. status ) . toBeUndefined ( )
304+ } )
305+ } )
195306} )
0 commit comments