1
1
// eslint-env jest
2
2
import { MemoCache } from '@data-client/normalizr' ;
3
+ import { useQuery , useSuspense } from '@data-client/react' ;
4
+ import { RestEndpoint } from '@data-client/rest' ;
3
5
import { IDEntity } from '__tests__/new' ;
4
6
import { fromJS } from 'immutable' ;
5
7
@@ -188,7 +190,6 @@ describe('top level schema', () => {
188
190
const sortedUsers = new schema . Query (
189
191
new schema . Collection ( [ User ] ) ,
190
192
( results , { asc } = { asc : false } , ...args ) => {
191
- if ( ! results ) return results ;
192
193
const sorted = [ ...results ] . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
193
194
if ( asc ) return sorted ;
194
195
return sorted . reverse ( ) ;
@@ -214,6 +215,49 @@ describe('top level schema', () => {
214
215
expect ( users ) . toMatchSnapshot ( ) ;
215
216
} ) ;
216
217
218
+ test ( 'works if base entity suspends' , ( ) => {
219
+ const entities = {
220
+ User : {
221
+ 1 : { id : '1' , name : 'Milo' } ,
222
+ 2 : { id : '2' , name : 'Jake' } ,
223
+ 3 : { id : '3' , name : 'Zeta' } ,
224
+ 4 : { id : '4' , name : 'Alpha' } ,
225
+ } ,
226
+ } ;
227
+ const users = new MemoCache ( ) . query ( '' , sortedUsers , [ ] , entities , { } ) ;
228
+ expect ( users ) . toBeUndefined ( ) ;
229
+ } ) ;
230
+
231
+ test ( 'works if base entity suspends' , ( ) => {
232
+ const allSortedUsers = new schema . Query (
233
+ new schema . All ( User ) ,
234
+ ( results , { asc } = { asc : false } , ...args ) => {
235
+ const sorted = [ ...results ] . sort ( ( a , b ) =>
236
+ a . name . localeCompare ( b . name ) ,
237
+ ) ;
238
+ if ( asc ) return sorted ;
239
+ return sorted . reverse ( ) ;
240
+ } ,
241
+ ) ;
242
+ const users = new MemoCache ( ) . query ( '' , allSortedUsers , [ ] , { } , { } ) ;
243
+ expect ( users ) . toBeUndefined ( ) ;
244
+ } ) ;
245
+
246
+ test ( 'works with nested schemas' , ( ) => {
247
+ const allSortedUsers = new schema . Query (
248
+ new schema . All ( User ) ,
249
+ ( results , { asc } = { asc : false } , ...args ) => {
250
+ const sorted = [ ...results ] . sort ( ( a , b ) =>
251
+ a . name . localeCompare ( b . name ) ,
252
+ ) ;
253
+ if ( asc ) return sorted ;
254
+ return sorted . reverse ( ) ;
255
+ } ,
256
+ ) ;
257
+ const users = new MemoCache ( ) . query ( '' , allSortedUsers , [ ] , { } , { } ) ;
258
+ expect ( users ) . toBeUndefined ( ) ;
259
+ } ) ;
260
+
217
261
test ( 'denormalizes should not be found when no entities are present' , ( ) => {
218
262
const entities = {
219
263
DOG : {
@@ -226,4 +270,20 @@ describe('top level schema', () => {
226
270
227
271
expect ( value ) . toEqual ( undefined ) ;
228
272
} ) ;
273
+
274
+ test ( '' , ( ) => {
275
+ const getUsers = new RestEndpoint ( {
276
+ path : '/users' ,
277
+ searchParams : { } as { asc ?: boolean } ,
278
+ schema : sortedUsers ,
279
+ } ) ;
280
+ ( ) => {
281
+ const users = useSuspense ( getUsers , { asc : true } ) ;
282
+ users . map ( user => user . name ) ;
283
+ const others = useQuery ( getUsers . schema , { asc : true } ) ;
284
+ // @ts -expect-error
285
+ others . map ( user => user . name ) ;
286
+ others ?. map ( user => user . name ) ;
287
+ } ;
288
+ } ) ;
229
289
} ) ;
0 commit comments