@@ -18,12 +18,8 @@ describe('useCodeFetch', () => {
1818 } ) ;
1919 }
2020
21- const apiResponse = {
22- implementations : [
23- { library_id : 'matplotlib' , code : 'import matplotlib\nfig, ax = plt.subplots()' } ,
24- { library_id : 'seaborn' , code : 'import seaborn as sns\nsns.heatmap(data)' } ,
25- ] ,
26- } ;
21+ const mplCode = 'import matplotlib\nfig, ax = plt.subplots()' ;
22+ const snsCode = 'import seaborn as sns\nsns.heatmap(data)' ;
2723
2824 describe ( 'getCode' , ( ) => {
2925 it ( 'returns null for uncached entries' , ( ) => {
@@ -32,37 +28,35 @@ describe('useCodeFetch', () => {
3228 } ) ;
3329
3430 it ( 'returns cached code after successful fetch' , async ( ) => {
35- mockFetchResponse ( apiResponse ) ;
31+ mockFetchResponse ( { code : mplCode } ) ;
3632 const { result } = renderHook ( ( ) => useCodeFetch ( ) ) ;
3733
3834 await act ( async ( ) => {
3935 await result . current . fetchCode ( 'scatter-basic' , 'matplotlib' ) ;
4036 } ) ;
4137
42- expect ( result . current . getCode ( 'scatter-basic' , 'matplotlib' ) ) . toBe (
43- 'import matplotlib\nfig, ax = plt.subplots()'
44- ) ;
38+ expect ( result . current . getCode ( 'scatter-basic' , 'matplotlib' ) ) . toBe ( mplCode ) ;
4539 } ) ;
4640 } ) ;
4741
4842 describe ( 'fetchCode' , ( ) => {
4943 it ( 'fetches code from API and returns it' , async ( ) => {
50- mockFetchResponse ( apiResponse ) ;
44+ mockFetchResponse ( { code : mplCode } ) ;
5145 const { result } = renderHook ( ( ) => useCodeFetch ( ) ) ;
5246
5347 let code : string | null = null ;
5448 await act ( async ( ) => {
5549 code = await result . current . fetchCode ( 'scatter-basic' , 'matplotlib' ) ;
5650 } ) ;
5751
58- expect ( code ) . toBe ( 'import matplotlib\nfig, ax = plt.subplots()' ) ;
52+ expect ( code ) . toBe ( mplCode ) ;
5953 expect ( globalThis . fetch ) . toHaveBeenCalledWith (
60- expect . stringContaining ( '/specs/scatter-basic' )
54+ expect . stringContaining ( '/specs/scatter-basic/matplotlib/code ' )
6155 ) ;
6256 } ) ;
6357
6458 it ( 'returns cached result on second call without re-fetching' , async ( ) => {
65- mockFetchResponse ( apiResponse ) ;
59+ mockFetchResponse ( { code : mplCode } ) ;
6660 const { result } = renderHook ( ( ) => useCodeFetch ( ) ) ;
6761
6862 await act ( async ( ) => {
@@ -74,12 +68,12 @@ describe('useCodeFetch', () => {
7468 code = await result . current . fetchCode ( 'scatter-basic' , 'matplotlib' ) ;
7569 } ) ;
7670
77- expect ( code ) . toBe ( 'import matplotlib\nfig, ax = plt.subplots()' ) ;
71+ expect ( code ) . toBe ( mplCode ) ;
7872 expect ( globalThis . fetch ) . toHaveBeenCalledTimes ( 1 ) ;
7973 } ) ;
8074
8175 it ( 'deduplicates concurrent requests for the same key' , async ( ) => {
82- mockFetchResponse ( apiResponse ) ;
76+ mockFetchResponse ( { code : mplCode } ) ;
8377 const { result } = renderHook ( ( ) => useCodeFetch ( ) ) ;
8478
8579 await act ( async ( ) => {
@@ -105,8 +99,8 @@ describe('useCodeFetch', () => {
10599 expect ( code ) . toBeNull ( ) ;
106100 } ) ;
107101
108- it ( 'returns null when implementation is not found for library ' , async ( ) => {
109- mockFetchResponse ( { implementations : [ { library_id : 'plotly' , code : 'plotly code' } ] } ) ;
102+ it ( 'returns null when code field is missing ' , async ( ) => {
103+ mockFetchResponse ( { } ) ;
110104 const { result } = renderHook ( ( ) => useCodeFetch ( ) ) ;
111105
112106 let code : string | null = 'initial' ;
@@ -135,7 +129,7 @@ describe('useCodeFetch', () => {
135129 } ) ;
136130
137131 it ( 'manages isLoading state during fetch' , async ( ) => {
138- mockFetchResponse ( apiResponse ) ;
132+ mockFetchResponse ( { code : mplCode } ) ;
139133 const { result } = renderHook ( ( ) => useCodeFetch ( ) ) ;
140134
141135 expect ( result . current . isLoading ) . toBe ( false ) ;
@@ -148,21 +142,21 @@ describe('useCodeFetch', () => {
148142 } ) ;
149143
150144 it ( 'fetches different libraries independently' , async ( ) => {
151- mockFetchResponse ( apiResponse ) ;
152- mockFetchResponse ( apiResponse ) ;
145+ mockFetchResponse ( { code : mplCode } ) ;
146+ mockFetchResponse ( { code : snsCode } ) ;
153147 const { result } = renderHook ( ( ) => useCodeFetch ( ) ) ;
154148
155- let mplCode : string | null = null ;
156- let snsCode : string | null = null ;
149+ let mplResult : string | null = null ;
150+ let snsResult : string | null = null ;
157151 await act ( async ( ) => {
158- mplCode = await result . current . fetchCode ( 'scatter-basic' , 'matplotlib' ) ;
152+ mplResult = await result . current . fetchCode ( 'scatter-basic' , 'matplotlib' ) ;
159153 } ) ;
160154 await act ( async ( ) => {
161- snsCode = await result . current . fetchCode ( 'scatter-basic' , 'seaborn' ) ;
155+ snsResult = await result . current . fetchCode ( 'scatter-basic' , 'seaborn' ) ;
162156 } ) ;
163157
164- expect ( mplCode ) . toContain ( 'matplotlib' ) ;
165- expect ( snsCode ) . toContain ( 'seaborn' ) ;
158+ expect ( mplResult ) . toContain ( 'matplotlib' ) ;
159+ expect ( snsResult ) . toContain ( 'seaborn' ) ;
166160 expect ( globalThis . fetch ) . toHaveBeenCalledTimes ( 2 ) ;
167161 } ) ;
168162 } ) ;
0 commit comments