@@ -27,7 +27,13 @@ import {
2727} from "vitest"
2828import { z } from "zod"
2929
30- import { getRegistriesConfig , getRegistry , getRegistryItems } from "./api"
30+ import {
31+ getRegistriesConfig ,
32+ getRegistriesIndex ,
33+ getRegistry ,
34+ getRegistryItems ,
35+ } from "./api"
36+ import { RegistriesIndexParseError } from "./errors"
3137
3238vi . mock ( "@/src/utils/handle-error" , ( ) => ( {
3339 handleError : vi . fn ( ) ,
@@ -96,6 +102,13 @@ const server = setupServer(
96102 } ,
97103 ] ,
98104 } )
105+ } ) ,
106+ http . get ( `${ REGISTRY_URL } /registries.json` , ( ) => {
107+ return HttpResponse . json ( {
108+ "@shadcn" : "https://ui.shadcn.com/r/styles/{style}/{name}.json" ,
109+ "@example" : "https://example.com/registry/styles/{style}/{name}.json" ,
110+ "@test" : "https://test.com/registry/{name}.json" ,
111+ } )
99112 } )
100113)
101114
@@ -1650,4 +1663,75 @@ describe("getRegistriesConfig", () => {
16501663 }
16511664 } )
16521665 } )
1666+
1667+ describe ( "getRegistriesIndex" , ( ) => {
1668+ it ( "should fetch and parse the registries index successfully" , async ( ) => {
1669+ const result = await getRegistriesIndex ( )
1670+
1671+ expect ( result ) . toEqual ( {
1672+ "@shadcn" : "https://ui.shadcn.com/r/styles/{style}/{name}.json" ,
1673+ "@example" : "https://example.com/registry/styles/{style}/{name}.json" ,
1674+ "@test" : "https://test.com/registry/{name}.json" ,
1675+ } )
1676+ } )
1677+
1678+ it ( "should respect cache options" , async ( ) => {
1679+ // Test with cache disabled
1680+ const result1 = await getRegistriesIndex ( { useCache : false } )
1681+ expect ( result1 ) . toBeDefined ( )
1682+
1683+ // Test with cache enabled
1684+ const result2 = await getRegistriesIndex ( { useCache : true } )
1685+ expect ( result2 ) . toBeDefined ( )
1686+
1687+ // Results should be the same
1688+ expect ( result1 ) . toEqual ( result2 )
1689+ } )
1690+
1691+ it ( "should use default cache behavior when no options provided" , async ( ) => {
1692+ const result = await getRegistriesIndex ( )
1693+ expect ( result ) . toBeDefined ( )
1694+ expect ( typeof result ) . toBe ( "object" )
1695+ } )
1696+
1697+ it ( "should handle network errors properly" , async ( ) => {
1698+ server . use (
1699+ http . get ( `${ REGISTRY_URL } /registries.json` , ( ) => {
1700+ return new HttpResponse ( null , { status : 500 } )
1701+ } )
1702+ )
1703+
1704+ await expect ( getRegistriesIndex ( { useCache : false } ) ) . rejects . toThrow ( )
1705+
1706+ try {
1707+ await getRegistriesIndex ( { useCache : false } )
1708+ } catch ( error ) {
1709+ expect ( error ) . not . toBeInstanceOf ( RegistriesIndexParseError )
1710+ }
1711+ } )
1712+
1713+ it ( "should handle invalid JSON response" , async ( ) => {
1714+ server . use (
1715+ http . get ( `${ REGISTRY_URL } /registries.json` , ( ) => {
1716+ return HttpResponse . json ( {
1717+ "invalid-namespace" : "some-url" ,
1718+ } )
1719+ } )
1720+ )
1721+
1722+ await expect ( getRegistriesIndex ( { useCache : false } ) ) . rejects . toThrow (
1723+ RegistriesIndexParseError
1724+ )
1725+ } )
1726+
1727+ it ( "should handle network timeout" , async ( ) => {
1728+ server . use (
1729+ http . get ( `${ REGISTRY_URL } /registries.json` , ( ) => {
1730+ return HttpResponse . error ( )
1731+ } )
1732+ )
1733+
1734+ await expect ( getRegistriesIndex ( { useCache : false } ) ) . rejects . toThrow ( )
1735+ } )
1736+ } )
16531737} )
0 commit comments