From f291d0978517c60e29dd03e6268d6bf180337fa2 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Sun, 10 Sep 2023 17:12:24 +0200 Subject: [PATCH] update tests --- .../src/query/tests/buildHooks.test.tsx | 5 +++- .../src/query/tests/buildMiddleware.test.tsx | 11 ++++++-- .../src/query/tests/cacheCollection.test.ts | 25 ++++++++++++++----- .../src/query/tests/cacheLifecycle.test.ts | 11 ++++++-- .../toolkit/src/query/tests/cleanup.test.tsx | 1 + .../src/query/tests/fetchBaseQuery.test.tsx | 14 ++++++----- packages/toolkit/src/query/tests/helpers.tsx | 4 ++- 7 files changed, 53 insertions(+), 18 deletions(-) diff --git a/packages/toolkit/src/query/tests/buildHooks.test.tsx b/packages/toolkit/src/query/tests/buildHooks.test.tsx index 39f93d145f..eafdfa6b6c 100644 --- a/packages/toolkit/src/query/tests/buildHooks.test.tsx +++ b/packages/toolkit/src/query/tests/buildHooks.test.tsx @@ -1820,10 +1820,13 @@ describe('hooks tests', () => { checkSession.matchPending, api.internalActions.subscriptionsUpdated.match, checkSession.matchRejected, + api.internalActions.subscriptionsUpdated.match, login.matchPending, login.matchFulfilled, checkSession.matchPending, - checkSession.matchFulfilled + api.internalActions.subscriptionsUpdated.match, + checkSession.matchFulfilled, + api.internalActions.subscriptionsUpdated.match ) }) }) diff --git a/packages/toolkit/src/query/tests/buildMiddleware.test.tsx b/packages/toolkit/src/query/tests/buildMiddleware.test.tsx index cd7ac0fb95..0434cb775e 100644 --- a/packages/toolkit/src/query/tests/buildMiddleware.test.tsx +++ b/packages/toolkit/src/query/tests/buildMiddleware.test.tsx @@ -38,7 +38,8 @@ it('invalidates the specified tags', async () => { api.internalActions.middlewareRegistered.match, getBanana.matchPending, api.internalActions.subscriptionsUpdated.match, - getBanana.matchFulfilled + getBanana.matchFulfilled, + api.internalActions.subscriptionsUpdated.match ) await storeRef.store.dispatch(api.util.invalidateTags(['Banana', 'Bread'])) @@ -51,9 +52,12 @@ it('invalidates the specified tags', async () => { getBanana.matchPending, api.internalActions.subscriptionsUpdated.match, getBanana.matchFulfilled, + api.internalActions.subscriptionsUpdated.match, api.util.invalidateTags.match, getBanana.matchPending, + api.internalActions.subscriptionsUpdated.match, getBanana.matchFulfilled, + api.internalActions.subscriptionsUpdated.match, ] expect(storeRef.store.getState().actions).toMatchSequence(...firstSequence) @@ -67,9 +71,12 @@ it('invalidates the specified tags', async () => { getBread.matchPending, api.internalActions.subscriptionsUpdated.match, getBread.matchFulfilled, + api.internalActions.subscriptionsUpdated.match, api.util.invalidateTags.match, getBread.matchPending, - getBread.matchFulfilled + api.internalActions.subscriptionsUpdated.match, + getBread.matchFulfilled, + api.internalActions.subscriptionsUpdated.match ) }) diff --git a/packages/toolkit/src/query/tests/cacheCollection.test.ts b/packages/toolkit/src/query/tests/cacheCollection.test.ts index 68500c9d97..1890be42f3 100644 --- a/packages/toolkit/src/query/tests/cacheCollection.test.ts +++ b/packages/toolkit/src/query/tests/cacheCollection.test.ts @@ -29,7 +29,9 @@ test(`query: await cleanup, defaults`, async () => { }) ) - store.dispatch(api.endpoints.query.initiate('arg')).unsubscribe() + const promise = store.dispatch(api.endpoints.query.initiate('arg')) + await promise + promise.unsubscribe() vi.advanceTimersByTime(59000) expect(onCleanup).not.toHaveBeenCalled() vi.advanceTimersByTime(2000) @@ -49,7 +51,9 @@ test(`query: await cleanup, keepUnusedDataFor set`, async () => { }) ) - store.dispatch(api.endpoints.query.initiate('arg')).unsubscribe() + const promise = store.dispatch(api.endpoints.query.initiate('arg')) + await promise + promise.unsubscribe() vi.advanceTimersByTime(28000) expect(onCleanup).not.toHaveBeenCalled() vi.advanceTimersByTime(2000) @@ -69,7 +73,9 @@ test(`query: handles large keepUnuseDataFor values over 32-bit ms`, async () => }) ) - store.dispatch(api.endpoints.query.initiate('arg')).unsubscribe() + const promise = store.dispatch(api.endpoints.query.initiate('arg')) + await promise + promise.unsubscribe() // Shouldn't have been called right away vi.advanceTimersByTime(1000) @@ -110,7 +116,9 @@ describe(`query: await cleanup, keepUnusedDataFor set`, () => { ) test('global keepUnusedDataFor', async () => { - store.dispatch(api.endpoints.query.initiate('arg')).unsubscribe() + const promise = store.dispatch(api.endpoints.query.initiate('arg')) + await promise + promise.unsubscribe() vi.advanceTimersByTime(28000) expect(onCleanup).not.toHaveBeenCalled() vi.advanceTimersByTime(2000) @@ -118,7 +126,10 @@ describe(`query: await cleanup, keepUnusedDataFor set`, () => { }) test('endpoint keepUnusedDataFor', async () => { - store.dispatch(api.endpoints.query2.initiate('arg')).unsubscribe() + const promise = store.dispatch(api.endpoints.query2.initiate('arg')) + await promise + promise.unsubscribe() + vi.advanceTimersByTime(34000) expect(onCleanup).not.toHaveBeenCalled() vi.advanceTimersByTime(2000) @@ -127,7 +138,9 @@ describe(`query: await cleanup, keepUnusedDataFor set`, () => { test('endpoint keepUnusedDataFor: 0 ', async () => { expect(onCleanup).not.toHaveBeenCalled() - store.dispatch(api.endpoints.query3.initiate('arg')).unsubscribe() + const promise = store.dispatch(api.endpoints.query3.initiate('arg')) + await promise + promise.unsubscribe() expect(onCleanup).not.toHaveBeenCalled() vi.advanceTimersByTime(1) expect(onCleanup).toHaveBeenCalled() diff --git a/packages/toolkit/src/query/tests/cacheLifecycle.test.ts b/packages/toolkit/src/query/tests/cacheLifecycle.test.ts index 778000ce5c..fa505f895d 100644 --- a/packages/toolkit/src/query/tests/cacheLifecycle.test.ts +++ b/packages/toolkit/src/query/tests/cacheLifecycle.test.ts @@ -76,6 +76,7 @@ describe.each([['query'], ['mutation']] as const)( expect(onNewCacheEntry).toHaveBeenCalledWith('arg') expect(onCleanup).not.toHaveBeenCalled() + await promise if (type === 'mutation') { promise.reset() } else { @@ -219,6 +220,7 @@ describe.each([['query'], ['mutation']] as const)( ) expect(onNewCacheEntry).toHaveBeenCalledWith('arg') + await promise if (type === 'mutation') { promise.reset() } else { @@ -270,6 +272,7 @@ describe.each([['query'], ['mutation']] as const)( ) expect(onNewCacheEntry).toHaveBeenCalledWith('arg') + await promise if (type === 'mutation') { promise.reset() @@ -321,6 +324,7 @@ describe.each([['query'], ['mutation']] as const)( expect(onNewCacheEntry).toHaveBeenCalledWith('arg') + await promise if (type === 'mutation') { promise.reset() } else { @@ -370,6 +374,7 @@ test(`query: getCacheEntry`, async () => { const promise = storeRef.store.dispatch( extended.endpoints.injected.initiate('arg') ) + await promise promise.unsubscribe() await fakeTimerWaitFor(() => { @@ -539,6 +544,7 @@ test('updateCachedData', async () => { const promise = storeRef.store.dispatch( extended.endpoints.injected.initiate('arg') ) + await promise promise.unsubscribe() await fakeTimerWaitFor(() => { @@ -576,7 +582,7 @@ test('dispatching further actions does not trigger another lifecycle', async () expect(onNewCacheEntry).toHaveBeenCalledTimes(1) }) -test('dispatching a query initializer with `subscribe: false` does not start a lifecycle', async () => { +test('dispatching a query initializer with `subscribe: false` does also start a lifecycle', async () => { const extended = api.injectEndpoints({ overrideExisting: true, endpoints: (build) => ({ @@ -591,8 +597,9 @@ test('dispatching a query initializer with `subscribe: false` does not start a l await storeRef.store.dispatch( extended.endpoints.injected.initiate(undefined, { subscribe: false }) ) - expect(onNewCacheEntry).toHaveBeenCalledTimes(0) + expect(onNewCacheEntry).toHaveBeenCalledTimes(1) + // will not be called a second time though await storeRef.store.dispatch(extended.endpoints.injected.initiate(undefined)) expect(onNewCacheEntry).toHaveBeenCalledTimes(1) }) diff --git a/packages/toolkit/src/query/tests/cleanup.test.tsx b/packages/toolkit/src/query/tests/cleanup.test.tsx index 36870e11db..f3c14af08f 100644 --- a/packages/toolkit/src/query/tests/cleanup.test.tsx +++ b/packages/toolkit/src/query/tests/cleanup.test.tsx @@ -203,5 +203,6 @@ test('Minimizes the number of subscription dispatches when multiple components a 'api/executeQuery/pending', 'api/internalSubscriptions/subscriptionsUpdated', 'api/executeQuery/fulfilled', + 'api/internalSubscriptions/subscriptionsUpdated', ]) }, 25000) diff --git a/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx b/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx index e2f9334f7b..3a6ecbba9f 100644 --- a/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx +++ b/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx @@ -176,7 +176,8 @@ describe('fetchBaseQuery', () => { expect(res.meta?.response).toBeInstanceOf(Object) expect(res.error).toEqual({ status: 'PARSING_ERROR', - error: 'SyntaxError: Unexpected token h in JSON at position 1', + error: + 'SyntaxError: Unexpected token \'h\', "this is not json!" is not valid JSON', originalStatus: 200, data: `this is not json!`, }) @@ -334,7 +335,8 @@ describe('fetchBaseQuery', () => { expect(res.meta?.response).toBeInstanceOf(Object) expect(res.error).toEqual({ status: 'PARSING_ERROR', - error: 'SyntaxError: Unexpected token h in JSON at position 1', + error: + 'SyntaxError: Unexpected token \'h\', "this is not json!" is not valid JSON', originalStatus: 500, data: `this is not json!`, }) @@ -435,7 +437,7 @@ describe('fetchBaseQuery', () => { it('supports a custom jsonReplacer', async () => { const body = { - items: new Set(["A", "B", "C"]) + items: new Set(['A', 'B', 'C']), } let request: any @@ -456,7 +458,8 @@ describe('fetchBaseQuery', () => { const baseQueryWithReplacer = fetchBaseQuery({ baseUrl, fetchFn: fetchFn as any, - jsonReplacer: (key, value) => value instanceof Set ? [...value] : value + jsonReplacer: (key, value) => + value instanceof Set ? [...value] : value, }) ;({ data: request } = await baseQueryWithReplacer( @@ -470,8 +473,7 @@ describe('fetchBaseQuery', () => { )) expect(request.headers['content-type']).toBe('application/json') - expect(request.body).toEqual({ items: ["A", "B", "C"] }) // Set is marshalled correctly by jsonReplacer - + expect(request.body).toEqual({ items: ['A', 'B', 'C'] }) // Set is marshalled correctly by jsonReplacer }) }) diff --git a/packages/toolkit/src/query/tests/helpers.tsx b/packages/toolkit/src/query/tests/helpers.tsx index 01c2a2260c..0c27a7c9b2 100644 --- a/packages/toolkit/src/query/tests/helpers.tsx +++ b/packages/toolkit/src/query/tests/helpers.tsx @@ -118,7 +118,9 @@ expect.extend({ if (!matchers[i](actions[i])) { return { message: () => - `Action ${actions[i].type} does not match sequence at position ${i}.`, + `Action ${actions[i].type} does not match sequence at position ${i}. +All actions: +${actions.map((a) => a.type).join('\n')}`, pass: false, } }