@@ -21,18 +21,37 @@ const TestSuspense = ({ children }) => {
2121 return < Suspense fallback = { < div > Loading...</ div > } > { children } </ Suspense > ;
2222} ;
2323
24+ const trackerVariants = [
25+ {
26+ label : 'default' ,
27+ useTrackerFn : ( key , fn , skipUpdate ) => useTracker ( key , fn , skipUpdate ) ,
28+ } ,
29+ {
30+ label : 'with deps' ,
31+ useTrackerFn : ( key , fn , skipUpdate ) => useTracker ( key , fn , [ ] , skipUpdate ) ,
32+ } ,
33+ ] ;
34+
35+ const runForVariants = ( name , testBody ) => {
36+ trackerVariants . forEach ( ( { label, useTrackerFn } ) => {
37+ Tinytest . addAsync ( `${ name } [${ label } ]` , ( test ) =>
38+ testBody ( test , useTrackerFn )
39+ ) ;
40+ } ) ;
41+ } ;
42+
2443/**
2544 * Test for useTracker with Suspense
2645 */
27- Tinytest . addAsync (
46+ runForVariants (
2847 'suspense/useTracker - Data query validation' ,
29- async ( test ) => {
48+ async ( test , useTrackerFn ) => {
3049 const { simpleFetch } = setupTest ( ) ;
3150
3251 let returnValue ;
3352
3453 const Test = ( ) => {
35- returnValue = useTracker ( 'TestDocs' , simpleFetch ) ;
54+ returnValue = useTrackerFn ( 'TestDocs' , simpleFetch ) ;
3655
3756 return null ;
3857 } ;
@@ -66,15 +85,15 @@ Tinytest.addAsync(
6685 }
6786) ;
6887
69- Tinytest . addAsync (
88+ Meteor . isServer && runForVariants (
7089 'suspense/useTracker - Test proper cache invalidation' ,
71- async function ( test ) {
90+ async function ( test , useTrackerFn ) {
7291 const { Coll, simpleFetch } = setupTest ( ) ;
7392
7493 let returnValue ;
7594
7695 const Test = ( ) => {
77- returnValue = useTracker ( 'TestDocs' , simpleFetch ) ;
96+ returnValue = useTrackerFn ( 'TestDocs' , simpleFetch ) ;
7897 return null ;
7998 } ;
8099
@@ -144,16 +163,84 @@ Tinytest.addAsync(
144163 }
145164) ;
146165
166+ Meteor . isClient && runForVariants (
167+ 'suspense/useTracker - Test responsive behavior' ,
168+ async function ( test , useTrackerFn ) {
169+ const { Coll, simpleFetch } = setupTest ( ) ;
170+
171+ let returnValue ;
172+
173+ const Test = ( ) => {
174+ returnValue = useTrackerFn ( 'TestDocs' , simpleFetch ) ;
175+ return null ;
176+ } ;
177+
178+ // first return promise
179+ renderToString (
180+ < TestSuspense >
181+ < Test />
182+ </ TestSuspense >
183+ ) ;
184+ // wait promise
185+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
186+ // return data
187+ renderToString (
188+ < TestSuspense >
189+ < Test />
190+ </ TestSuspense >
191+ ) ;
192+
193+ test . equal (
194+ returnValue [ 0 ] . updated ,
195+ 0 ,
196+ 'Return value should be an array with initial value as find promise resolved'
197+ ) ;
198+
199+ Coll . updateAsync ( { id : 0 } , { $inc : { updated : 1 } } ) ;
200+
201+ // second await promise
202+ renderToString (
203+ < TestSuspense >
204+ < Test />
205+ </ TestSuspense >
206+ ) ;
207+
208+ test . equal (
209+ returnValue [ 0 ] . updated ,
210+ 0 ,
211+ 'Return value should still not updated as second find promise unresolved'
212+ ) ;
213+
214+ // wait promise
215+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
216+
217+ // return data
218+ renderToString (
219+ < TestSuspense >
220+ < Test />
221+ </ TestSuspense >
222+ ) ;
223+
224+ test . equal (
225+ returnValue [ 0 ] . updated ,
226+ 1 ,
227+ 'Return value should be an array with one document with value updated'
228+ ) ;
229+
230+ await clearCache ( ) ;
231+ }
232+ ) ;
233+
147234Meteor . isClient &&
148- Tinytest . addAsync (
235+ runForVariants (
149236 'suspense/useTracker - Test useTracker with skipUpdate' ,
150- async function ( test ) {
237+ async function ( test , useTrackerFn ) {
151238 const { Coll, simpleFetch } = setupTest ( { id : 0 , updated : 0 , other : 0 } ) ;
152239
153240 let returnValue ;
154241
155242 const Test = ( ) => {
156- returnValue = useTracker ( 'TestDocs' , simpleFetch , ( prev , next ) => {
243+ returnValue = useTrackerFn ( 'TestDocs' , simpleFetch , ( prev , next ) => {
157244 // Skip update if the document has not changed
158245 return prev [ 0 ] . updated === next [ 0 ] . updated ;
159246 } ) ;
@@ -212,9 +299,9 @@ Meteor.isClient &&
212299
213300// https://github.com/meteor/react-packages/issues/454
214301Meteor . isClient &&
215- Tinytest . addAsync (
302+ runForVariants (
216303 'suspense/useTracker - Testing performance with multiple Trackers' ,
217- async ( test ) => {
304+ async ( test , useTrackerFn ) => {
218305 const TestCollections = [ ] ;
219306 let returnDocs = new Map ( ) ;
220307
@@ -229,7 +316,7 @@ Meteor.isClient &&
229316 }
230317
231318 const Test = ( { collection, index } ) => {
232- const docsCount = useTracker ( `TestDocs${ index } ` , ( ) =>
319+ const docsCount = useTrackerFn ( `TestDocs${ index } ` , ( ) =>
233320 collection . find ( ) . fetchAsync ( )
234321 ) . length ;
235322
@@ -268,15 +355,15 @@ Meteor.isClient &&
268355 ) ;
269356
270357Meteor . isServer &&
271- Tinytest . addAsync (
358+ runForVariants (
272359 'suspense/useTracker - Test no memory leaks' ,
273- async function ( test ) {
360+ async function ( test , useTrackerFn ) {
274361 const { simpleFetch } = setupTest ( ) ;
275362
276363 let returnValue ;
277364
278365 const Test = ( ) => {
279- returnValue = useTracker ( 'TestDocs' , simpleFetch ) ;
366+ returnValue = useTrackerFn ( 'TestDocs' , simpleFetch ) ;
280367
281368 return null ;
282369 } ;
@@ -307,13 +394,13 @@ Meteor.isServer &&
307394 ) ;
308395
309396Meteor . isClient &&
310- Tinytest . addAsync (
397+ runForVariants (
311398 'suspense/useTracker - Test no memory leaks' ,
312- async function ( test ) {
399+ async function ( test , useTrackerFn ) {
313400 const { simpleFetch } = setupTest ( { id : 0 , name : 'a' } ) ;
314401
315402 const Test = ( ) => {
316- const docs = useTracker ( 'TestDocs' , simpleFetch ) ;
403+ const docs = useTrackerFn ( 'TestDocs' , simpleFetch ) ;
317404
318405 return < div > { docs [ 0 ] ?. name } </ div > ;
319406 } ;
0 commit comments