@@ -12,34 +12,80 @@ async function fetchBadge(page: { request: { get: (url: string) => Promise<any>
1212}
1313
1414test . describe ( 'badge API' , ( ) => {
15- test ( 'unscoped package badge renders SVG' , async ( { page, baseURL } ) => {
16- const url = toLocalUrl ( baseURL , '/api/registry/badge/nuxt' )
17- const { response, body } = await fetchBadge ( page , url )
18-
19- expect ( response . status ( ) ) . toBe ( 200 )
20- expect ( response . headers ( ) [ 'content-type' ] ) . toContain ( 'image/svg+xml' )
21- expect ( body ) . toContain ( '<svg' )
22- expect ( body ) . toContain ( 'nuxt' )
15+ const badgeMap : Record < string , string > = {
16+ 'version' : 'version' ,
17+ 'license' : 'license' ,
18+ 'size' : 'install size' ,
19+ 'downloads' : 'downloads/mo' ,
20+ 'downloads-week' : 'downloads/wk' ,
21+ 'vulnerabilities' : 'vulns' ,
22+ 'dependencies' : 'dependencies' ,
23+ 'updated' : 'updated' ,
24+ 'engines' : 'node' ,
25+ 'types' : 'types' ,
26+ 'created' : 'created' ,
27+ 'maintainers' : 'maintainers' ,
28+ 'deprecated' : 'status' ,
29+ }
30+
31+ for ( const [ type , expectedLabel ] of Object . entries ( badgeMap ) ) {
32+ test . describe ( `${ type } badge` , ( ) => {
33+ test ( 'renders correct label' , async ( { page, baseURL } ) => {
34+ const url = toLocalUrl ( baseURL , `/api/registry/badge/${ type } /nuxt` )
35+ const { response, body } = await fetchBadge ( page , url )
36+
37+ expect ( response . status ( ) ) . toBe ( 200 )
38+ expect ( response . headers ( ) [ 'content-type' ] ) . toContain ( 'image/svg+xml' )
39+ expect ( body ) . toContain ( expectedLabel )
40+ } )
41+
42+ test ( 'scoped package renders successfully' , async ( { page, baseURL } ) => {
43+ const url = toLocalUrl ( baseURL , `/api/registry/badge/${ type } /@nuxt/kit` )
44+ const { response } = await fetchBadge ( page , url )
45+
46+ expect ( response . status ( ) ) . toBe ( 200 )
47+ } )
48+
49+ test ( 'explicit version badge renders successfully' , async ( { page, baseURL } ) => {
50+ const url = toLocalUrl ( baseURL , `/api/registry/badge/${ type } /nuxt/v/3.12.0` )
51+ const { response, body } = await fetchBadge ( page , url )
52+
53+ expect ( response . status ( ) ) . toBe ( 200 )
54+ if ( type === 'version' ) {
55+ expect ( body ) . toContain ( '3.12.0' )
56+ }
57+ } )
58+
59+ test ( 'respects name=true parameter' , async ( { page, baseURL } ) => {
60+ const packageName = 'nuxt'
61+ const url = toLocalUrl ( baseURL , `/api/registry/badge/${ type } /${ packageName } ?name=true` )
62+ const { body } = await fetchBadge ( page , url )
63+
64+ expect ( body ) . toContain ( packageName )
65+ expect ( body ) . not . toContain ( expectedLabel )
66+ } )
67+ } )
68+ }
69+
70+ test ( 'custom color parameter is applied to SVG' , async ( { page, baseURL } ) => {
71+ const customColor = 'ff69b4'
72+ const url = toLocalUrl ( baseURL , `/api/registry/badge/version/nuxt?color=${ customColor } ` )
73+ const { body } = await fetchBadge ( page , url )
74+
75+ expect ( body ) . toContain ( `fill="#${ customColor } "` )
2376 } )
2477
25- test ( 'scoped package badge renders SVG ' , async ( { page, baseURL } ) => {
26- const url = toLocalUrl ( baseURL , '/api/registry/badge/@nuxt/kit ' )
27- const { response , body } = await fetchBadge ( page , url )
78+ test ( 'invalid badge type defaults to version strategy ' , async ( { page, baseURL } ) => {
79+ const url = toLocalUrl ( baseURL , '/api/registry/badge/invalid-type/nuxt ' )
80+ const { body } = await fetchBadge ( page , url )
2881
29- expect ( response . status ( ) ) . toBe ( 200 )
30- expect ( response . headers ( ) [ 'content-type' ] ) . toContain ( 'image/svg+xml' )
31- expect ( body ) . toContain ( '<svg' )
32- expect ( body ) . toContain ( '@nuxt/kit' )
82+ expect ( body ) . toContain ( 'version' )
3383 } )
3484
35- test ( 'explicit version badge includes requested version ' , async ( { page, baseURL } ) => {
36- const url = toLocalUrl ( baseURL , '/api/registry/badge/nuxt/v/3.12.0 ' )
37- const { response, body } = await fetchBadge ( page , url )
85+ test ( 'missing package returns 404 ' , async ( { page, baseURL } ) => {
86+ const url = toLocalUrl ( baseURL , '/api/registry/badge/version/ ' )
87+ const { response } = await fetchBadge ( page , url )
3888
39- expect ( response . status ( ) ) . toBe ( 200 )
40- expect ( response . headers ( ) [ 'content-type' ] ) . toContain ( 'image/svg+xml' )
41- expect ( body ) . toContain ( '<svg' )
42- expect ( body ) . toContain ( 'nuxt' )
43- expect ( body ) . toContain ( '3.12.0' )
89+ expect ( response . status ( ) ) . toBe ( 404 )
4490 } )
4591} )
0 commit comments