@@ -60,24 +60,18 @@ describe('vue useCache()', () => {
6060 } ) ;
6161
6262 it ( 'returns undefined on empty store (no fetch)' , async ( ) => {
63- const { result, waitForNextUpdate , cleanup } = renderDataCompose ( ( ) =>
63+ const { result, cleanup } = await renderDataCompose ( ( ) =>
6464 useCache ( CoolerArticleResource . get , { id : payload . id } ) ,
6565 ) ;
6666
67- // Wait for initial render
68- await waitForNextUpdate ( ) ;
69-
70- // Unlike useSuspense, useCache should return a ComputedRef immediately
71- expect ( result . current ) . toBeDefined ( ) ;
72-
7367 // The value should be undefined since we haven't fetched yet
74- expect ( result . current ! . value ) . toBeUndefined ( ) ;
68+ expect ( result . value ) . toBeUndefined ( ) ;
7569
7670 cleanup ( ) ;
7771 } ) ;
7872
7973 it ( 'returns data when already in cache' , async ( ) => {
80- const { result, waitForNextUpdate, cleanup } = renderDataCompose (
74+ const { result, waitForNextUpdate, cleanup } = await renderDataCompose (
8175 ( ) => useCache ( CoolerArticleResource . get , { id : payload . id } ) ,
8276 {
8377 initialFixtures : [
@@ -93,19 +87,17 @@ describe('vue useCache()', () => {
9387 // Wait for initial render
9488 await waitForNextUpdate ( ) ;
9589
96- const articleRef = result . current ! ;
97-
9890 // The value should be the cached data
99- expect ( articleRef . value ) . toBeDefined ( ) ;
100- expect ( articleRef . value ! . title ) . toBe ( payload . title ) ;
101- expect ( articleRef . value ! . content ) . toBe ( payload . content ) ;
91+ expect ( result . value ) . toBeDefined ( ) ;
92+ expect ( result . value ? .title ) . toBe ( payload . title ) ;
93+ expect ( result . value ? .content ) . toBe ( payload . content ) ;
10294
10395 cleanup ( ) ;
10496 } ) ;
10597
10698 it ( 're-renders when controller.setResponse() updates data' , async ( ) => {
10799 const { result, controller, waitForNextUpdate, cleanup } =
108- renderDataCompose (
100+ await renderDataCompose (
109101 ( ) => useCache ( CoolerArticleResource . get , { id : payload . id } ) ,
110102 {
111103 initialFixtures : [
@@ -121,11 +113,9 @@ describe('vue useCache()', () => {
121113 // Wait for initial render
122114 await waitForNextUpdate ( ) ;
123115
124- const articleRef = result . current ! ;
125-
126116 // Verify initial values
127- expect ( articleRef . value ! . title ) . toBe ( payload . title ) ;
128- expect ( articleRef . value ! . content ) . toBe ( payload . content ) ;
117+ expect ( result . value ? .title ) . toBe ( payload . title ) ;
118+ expect ( result . value ? .content ) . toBe ( payload . content ) ;
129119
130120 // Update the store using controller.setResponse
131121 const newTitle = payload . title + ' updated' ;
@@ -140,15 +130,15 @@ describe('vue useCache()', () => {
140130 await nextTick ( ) ;
141131
142132 // The ComputedRef should now have updated values (it's reactive!)
143- expect ( articleRef . value ! . title ) . toBe ( newTitle ) ;
144- expect ( articleRef . value ! . content ) . toBe ( newContent ) ;
133+ expect ( result . value ? .title ) . toBe ( newTitle ) ;
134+ expect ( result . value ? .content ) . toBe ( newContent ) ;
145135
146136 cleanup ( ) ;
147137 } ) ;
148138
149139 it ( 're-renders when controller.fetch() mutates data' , async ( ) => {
150140 const { result, controller, waitForNextUpdate, cleanup } =
151- renderDataCompose (
141+ await renderDataCompose (
152142 ( ) => useCache ( CoolerArticleResource . get , { id : payload . id } ) ,
153143 {
154144 initialFixtures : [
@@ -164,11 +154,9 @@ describe('vue useCache()', () => {
164154 // Wait for initial render
165155 await waitForNextUpdate ( ) ;
166156
167- const articleRef = result . current ! ;
168-
169157 // Verify initial values
170- expect ( articleRef . value ! . title ) . toBe ( payload . title ) ;
171- expect ( articleRef . value ! . content ) . toBe ( payload . content ) ;
158+ expect ( result . value ? .title ) . toBe ( payload . title ) ;
159+ expect ( result . value ? .content ) . toBe ( payload . content ) ;
172160
173161 // Mutate the data using controller.fetch with update endpoint
174162 const updatedTitle = payload . title + ' mutated' ;
@@ -184,8 +172,8 @@ describe('vue useCache()', () => {
184172 await nextTick ( ) ;
185173
186174 // The ComputedRef should now have updated values (it's reactive!)
187- expect ( articleRef . value ! . title ) . toBe ( updatedTitle ) ;
188- expect ( articleRef . value ! . content ) . toBe ( updatedContent ) ;
175+ expect ( result . value ? .title ) . toBe ( updatedTitle ) ;
176+ expect ( result . value ? .content ) . toBe ( updatedContent ) ;
189177
190178 cleanup ( ) ;
191179 } ) ;
@@ -297,7 +285,7 @@ describe('vue useCache()', () => {
297285
298286 it ( 'should handle null args by returning undefined' , async ( ) => {
299287 const props = reactive ( { id : payload . id as number | null } ) ;
300- const { result, waitForNextUpdate, cleanup } = renderDataCompose (
288+ const { result, waitForNextUpdate, cleanup } = await renderDataCompose (
301289 ( props : { id : number | null } ) =>
302290 useCache (
303291 CoolerArticleResource . get ,
@@ -323,36 +311,34 @@ describe('vue useCache()', () => {
323311 // Wait for initial render
324312 await waitForNextUpdate ( ) ;
325313
326- const articleRef = result . current ! ;
327-
328- expect ( articleRef ) . toBeDefined ( ) ;
314+ expect ( result . value ) . toBeDefined ( ) ;
329315
330316 // Verify initial values
331- expect ( articleRef . value ?. title ) . toBe ( payload . title ) ;
332- expect ( articleRef . value ?. content ) . toBe ( payload . content ) ;
317+ expect ( result . value ?. title ) . toBe ( payload . title ) ;
318+ expect ( result . value ?. content ) . toBe ( payload . content ) ;
333319
334320 // Change to null - the ComputedRef should reactively become undefined
335321 props . id = null ;
336322 await nextTick ( ) ;
337323
338324 // The same ComputedRef should now have undefined value
339- expect ( articleRef . value ) . toBeUndefined ( ) ;
325+ expect ( result . value ) . toBeUndefined ( ) ;
340326
341327 // Change back to valid id - should get cached data
342328 props . id = payload2 . id ;
343329 await nextTick ( ) ;
344330
345331 // The ComputedRef should now have the new article data
346- expect ( articleRef ) . toBeDefined ( ) ;
347- expect ( articleRef ? .value ?. title ) . toBe ( payload2 . title ) ;
348- expect ( articleRef . value ?. content ) . toBe ( payload2 . content ) ;
332+ expect ( result . value ) . toBeDefined ( ) ;
333+ expect ( result . value ?. title ) . toBe ( payload2 . title ) ;
334+ expect ( result . value ?. content ) . toBe ( payload2 . content ) ;
349335
350336 cleanup ( ) ;
351337 } ) ;
352338
353339 it ( 'returns undefined for stale data when invalidIfStale is true' , async ( ) => {
354340 const { result, controller, waitForNextUpdate, cleanup } =
355- renderDataCompose (
341+ await renderDataCompose (
356342 ( ) => useCache ( CoolerArticleResource . get , { id : payload . id } ) ,
357343 {
358344 initialFixtures : [
@@ -373,16 +359,14 @@ describe('vue useCache()', () => {
373359
374360 await nextTick ( ) ;
375361
376- const articleRef = result . current ! ;
377-
378362 // The value should be undefined since the data is invalid
379- expect ( articleRef . value ) . toBeUndefined ( ) ;
363+ expect ( result . value ) . toBeUndefined ( ) ;
380364
381365 cleanup ( ) ;
382366 } ) ;
383367
384368 it ( 'returns cached data even if expired when expiryStatus is Valid' , async ( ) => {
385- const { result, waitForNextUpdate, cleanup } = renderDataCompose (
369+ const { result, waitForNextUpdate, cleanup } = await renderDataCompose (
386370 ( ) => useCache ( CoolerArticleResource . get , { id : payload . id } ) ,
387371 {
388372 initialFixtures : [
@@ -398,11 +382,9 @@ describe('vue useCache()', () => {
398382 // Wait for initial render
399383 await waitForNextUpdate ( ) ;
400384
401- const articleRef = result . current ! ;
402-
403385 // Even though data might be expired, if it's Valid it should be returned
404- expect ( articleRef . value ) . toBeDefined ( ) ;
405- expect ( articleRef . value ! . title ) . toBe ( payload . title ) ;
386+ expect ( result . value ) . toBeDefined ( ) ;
387+ expect ( result . value ? .title ) . toBe ( payload . title ) ;
406388
407389 cleanup ( ) ;
408390 } ) ;
0 commit comments