Skip to content

Commit f291d09

Browse files
phryneasmarkerikson
authored andcommitted
update tests
1 parent e0c69b9 commit f291d09

File tree

7 files changed

+53
-18
lines changed

7 files changed

+53
-18
lines changed

packages/toolkit/src/query/tests/buildHooks.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1820,10 +1820,13 @@ describe('hooks tests', () => {
18201820
checkSession.matchPending,
18211821
api.internalActions.subscriptionsUpdated.match,
18221822
checkSession.matchRejected,
1823+
api.internalActions.subscriptionsUpdated.match,
18231824
login.matchPending,
18241825
login.matchFulfilled,
18251826
checkSession.matchPending,
1826-
checkSession.matchFulfilled
1827+
api.internalActions.subscriptionsUpdated.match,
1828+
checkSession.matchFulfilled,
1829+
api.internalActions.subscriptionsUpdated.match
18271830
)
18281831
})
18291832
})

packages/toolkit/src/query/tests/buildMiddleware.test.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ it('invalidates the specified tags', async () => {
3838
api.internalActions.middlewareRegistered.match,
3939
getBanana.matchPending,
4040
api.internalActions.subscriptionsUpdated.match,
41-
getBanana.matchFulfilled
41+
getBanana.matchFulfilled,
42+
api.internalActions.subscriptionsUpdated.match
4243
)
4344

4445
await storeRef.store.dispatch(api.util.invalidateTags(['Banana', 'Bread']))
@@ -51,9 +52,12 @@ it('invalidates the specified tags', async () => {
5152
getBanana.matchPending,
5253
api.internalActions.subscriptionsUpdated.match,
5354
getBanana.matchFulfilled,
55+
api.internalActions.subscriptionsUpdated.match,
5456
api.util.invalidateTags.match,
5557
getBanana.matchPending,
58+
api.internalActions.subscriptionsUpdated.match,
5659
getBanana.matchFulfilled,
60+
api.internalActions.subscriptionsUpdated.match,
5761
]
5862
expect(storeRef.store.getState().actions).toMatchSequence(...firstSequence)
5963

@@ -67,9 +71,12 @@ it('invalidates the specified tags', async () => {
6771
getBread.matchPending,
6872
api.internalActions.subscriptionsUpdated.match,
6973
getBread.matchFulfilled,
74+
api.internalActions.subscriptionsUpdated.match,
7075
api.util.invalidateTags.match,
7176
getBread.matchPending,
72-
getBread.matchFulfilled
77+
api.internalActions.subscriptionsUpdated.match,
78+
getBread.matchFulfilled,
79+
api.internalActions.subscriptionsUpdated.match
7380
)
7481
})
7582

packages/toolkit/src/query/tests/cacheCollection.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ test(`query: await cleanup, defaults`, async () => {
2929
})
3030
)
3131

32-
store.dispatch(api.endpoints.query.initiate('arg')).unsubscribe()
32+
const promise = store.dispatch(api.endpoints.query.initiate('arg'))
33+
await promise
34+
promise.unsubscribe()
3335
vi.advanceTimersByTime(59000)
3436
expect(onCleanup).not.toHaveBeenCalled()
3537
vi.advanceTimersByTime(2000)
@@ -49,7 +51,9 @@ test(`query: await cleanup, keepUnusedDataFor set`, async () => {
4951
})
5052
)
5153

52-
store.dispatch(api.endpoints.query.initiate('arg')).unsubscribe()
54+
const promise = store.dispatch(api.endpoints.query.initiate('arg'))
55+
await promise
56+
promise.unsubscribe()
5357
vi.advanceTimersByTime(28000)
5458
expect(onCleanup).not.toHaveBeenCalled()
5559
vi.advanceTimersByTime(2000)
@@ -69,7 +73,9 @@ test(`query: handles large keepUnuseDataFor values over 32-bit ms`, async () =>
6973
})
7074
)
7175

72-
store.dispatch(api.endpoints.query.initiate('arg')).unsubscribe()
76+
const promise = store.dispatch(api.endpoints.query.initiate('arg'))
77+
await promise
78+
promise.unsubscribe()
7379

7480
// Shouldn't have been called right away
7581
vi.advanceTimersByTime(1000)
@@ -110,15 +116,20 @@ describe(`query: await cleanup, keepUnusedDataFor set`, () => {
110116
)
111117

112118
test('global keepUnusedDataFor', async () => {
113-
store.dispatch(api.endpoints.query.initiate('arg')).unsubscribe()
119+
const promise = store.dispatch(api.endpoints.query.initiate('arg'))
120+
await promise
121+
promise.unsubscribe()
114122
vi.advanceTimersByTime(28000)
115123
expect(onCleanup).not.toHaveBeenCalled()
116124
vi.advanceTimersByTime(2000)
117125
expect(onCleanup).toHaveBeenCalled()
118126
})
119127

120128
test('endpoint keepUnusedDataFor', async () => {
121-
store.dispatch(api.endpoints.query2.initiate('arg')).unsubscribe()
129+
const promise = store.dispatch(api.endpoints.query2.initiate('arg'))
130+
await promise
131+
promise.unsubscribe()
132+
122133
vi.advanceTimersByTime(34000)
123134
expect(onCleanup).not.toHaveBeenCalled()
124135
vi.advanceTimersByTime(2000)
@@ -127,7 +138,9 @@ describe(`query: await cleanup, keepUnusedDataFor set`, () => {
127138

128139
test('endpoint keepUnusedDataFor: 0 ', async () => {
129140
expect(onCleanup).not.toHaveBeenCalled()
130-
store.dispatch(api.endpoints.query3.initiate('arg')).unsubscribe()
141+
const promise = store.dispatch(api.endpoints.query3.initiate('arg'))
142+
await promise
143+
promise.unsubscribe()
131144
expect(onCleanup).not.toHaveBeenCalled()
132145
vi.advanceTimersByTime(1)
133146
expect(onCleanup).toHaveBeenCalled()

packages/toolkit/src/query/tests/cacheLifecycle.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe.each([['query'], ['mutation']] as const)(
7676
expect(onNewCacheEntry).toHaveBeenCalledWith('arg')
7777
expect(onCleanup).not.toHaveBeenCalled()
7878

79+
await promise
7980
if (type === 'mutation') {
8081
promise.reset()
8182
} else {
@@ -219,6 +220,7 @@ describe.each([['query'], ['mutation']] as const)(
219220
)
220221

221222
expect(onNewCacheEntry).toHaveBeenCalledWith('arg')
223+
await promise
222224
if (type === 'mutation') {
223225
promise.reset()
224226
} else {
@@ -270,6 +272,7 @@ describe.each([['query'], ['mutation']] as const)(
270272
)
271273

272274
expect(onNewCacheEntry).toHaveBeenCalledWith('arg')
275+
await promise
273276

274277
if (type === 'mutation') {
275278
promise.reset()
@@ -321,6 +324,7 @@ describe.each([['query'], ['mutation']] as const)(
321324

322325
expect(onNewCacheEntry).toHaveBeenCalledWith('arg')
323326

327+
await promise
324328
if (type === 'mutation') {
325329
promise.reset()
326330
} else {
@@ -370,6 +374,7 @@ test(`query: getCacheEntry`, async () => {
370374
const promise = storeRef.store.dispatch(
371375
extended.endpoints.injected.initiate('arg')
372376
)
377+
await promise
373378
promise.unsubscribe()
374379

375380
await fakeTimerWaitFor(() => {
@@ -539,6 +544,7 @@ test('updateCachedData', async () => {
539544
const promise = storeRef.store.dispatch(
540545
extended.endpoints.injected.initiate('arg')
541546
)
547+
await promise
542548
promise.unsubscribe()
543549

544550
await fakeTimerWaitFor(() => {
@@ -576,7 +582,7 @@ test('dispatching further actions does not trigger another lifecycle', async ()
576582
expect(onNewCacheEntry).toHaveBeenCalledTimes(1)
577583
})
578584

579-
test('dispatching a query initializer with `subscribe: false` does not start a lifecycle', async () => {
585+
test('dispatching a query initializer with `subscribe: false` does also start a lifecycle', async () => {
580586
const extended = api.injectEndpoints({
581587
overrideExisting: true,
582588
endpoints: (build) => ({
@@ -591,8 +597,9 @@ test('dispatching a query initializer with `subscribe: false` does not start a l
591597
await storeRef.store.dispatch(
592598
extended.endpoints.injected.initiate(undefined, { subscribe: false })
593599
)
594-
expect(onNewCacheEntry).toHaveBeenCalledTimes(0)
600+
expect(onNewCacheEntry).toHaveBeenCalledTimes(1)
595601

602+
// will not be called a second time though
596603
await storeRef.store.dispatch(extended.endpoints.injected.initiate(undefined))
597604
expect(onNewCacheEntry).toHaveBeenCalledTimes(1)
598605
})

packages/toolkit/src/query/tests/cleanup.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,6 @@ test('Minimizes the number of subscription dispatches when multiple components a
203203
'api/executeQuery/pending',
204204
'api/internalSubscriptions/subscriptionsUpdated',
205205
'api/executeQuery/fulfilled',
206+
'api/internalSubscriptions/subscriptionsUpdated',
206207
])
207208
}, 25000)

packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ describe('fetchBaseQuery', () => {
176176
expect(res.meta?.response).toBeInstanceOf(Object)
177177
expect(res.error).toEqual({
178178
status: 'PARSING_ERROR',
179-
error: 'SyntaxError: Unexpected token h in JSON at position 1',
179+
error:
180+
'SyntaxError: Unexpected token \'h\', "this is not json!" is not valid JSON',
180181
originalStatus: 200,
181182
data: `this is not json!`,
182183
})
@@ -334,7 +335,8 @@ describe('fetchBaseQuery', () => {
334335
expect(res.meta?.response).toBeInstanceOf(Object)
335336
expect(res.error).toEqual({
336337
status: 'PARSING_ERROR',
337-
error: 'SyntaxError: Unexpected token h in JSON at position 1',
338+
error:
339+
'SyntaxError: Unexpected token \'h\', "this is not json!" is not valid JSON',
338340
originalStatus: 500,
339341
data: `this is not json!`,
340342
})
@@ -435,7 +437,7 @@ describe('fetchBaseQuery', () => {
435437

436438
it('supports a custom jsonReplacer', async () => {
437439
const body = {
438-
items: new Set(["A", "B", "C"])
440+
items: new Set(['A', 'B', 'C']),
439441
}
440442

441443
let request: any
@@ -456,7 +458,8 @@ describe('fetchBaseQuery', () => {
456458
const baseQueryWithReplacer = fetchBaseQuery({
457459
baseUrl,
458460
fetchFn: fetchFn as any,
459-
jsonReplacer: (key, value) => value instanceof Set ? [...value] : value
461+
jsonReplacer: (key, value) =>
462+
value instanceof Set ? [...value] : value,
460463
})
461464

462465
;({ data: request } = await baseQueryWithReplacer(
@@ -470,8 +473,7 @@ describe('fetchBaseQuery', () => {
470473
))
471474

472475
expect(request.headers['content-type']).toBe('application/json')
473-
expect(request.body).toEqual({ items: ["A", "B", "C"] }) // Set is marshalled correctly by jsonReplacer
474-
476+
expect(request.body).toEqual({ items: ['A', 'B', 'C'] }) // Set is marshalled correctly by jsonReplacer
475477
})
476478
})
477479

packages/toolkit/src/query/tests/helpers.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ expect.extend({
118118
if (!matchers[i](actions[i])) {
119119
return {
120120
message: () =>
121-
`Action ${actions[i].type} does not match sequence at position ${i}.`,
121+
`Action ${actions[i].type} does not match sequence at position ${i}.
122+
All actions:
123+
${actions.map((a) => a.type).join('\n')}`,
122124
pass: false,
123125
}
124126
}

0 commit comments

Comments
 (0)