Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Sep 10, 2023
1 parent 397359d commit 56d4644
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
5 changes: 4 additions & 1 deletion packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1819,10 +1819,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
)
})
})
Expand Down
11 changes: 9 additions & 2 deletions packages/toolkit/src/query/tests/buildMiddleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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']))
Expand All @@ -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)

Expand All @@ -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
)
})

Expand Down
25 changes: 19 additions & 6 deletions packages/toolkit/src/query/tests/cacheCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
jest.advanceTimersByTime(59000), await waitMs()
expect(onCleanup).not.toHaveBeenCalled()
jest.advanceTimersByTime(2000), await waitMs()
Expand All @@ -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()
jest.advanceTimersByTime(28000), await waitMs()
expect(onCleanup).not.toHaveBeenCalled()
jest.advanceTimersByTime(2000), await waitMs()
Expand All @@ -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
jest.advanceTimersByTime(1000), await waitMs()
Expand Down Expand Up @@ -111,15 +117,20 @@ 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()
jest.advanceTimersByTime(28000), await waitMs()
expect(onCleanup).not.toHaveBeenCalled()
jest.advanceTimersByTime(2000), await waitMs()
expect(onCleanup).toHaveBeenCalled()
})

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()

jest.advanceTimersByTime(34000), await waitMs()
expect(onCleanup).not.toHaveBeenCalled()
jest.advanceTimersByTime(2000), await waitMs()
Expand All @@ -128,7 +139,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()
jest.advanceTimersByTime(1)
await waitMs()
Expand Down
11 changes: 9 additions & 2 deletions packages/toolkit/src/query/tests/cacheLifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe.each([['query'], ['mutation']] as const)(
expect(onNewCacheEntry).toHaveBeenCalledWith('arg')
expect(onCleanup).not.toHaveBeenCalled()

await promise
promise.unsubscribe(), await waitMs()
if (type === 'query') {
jest.advanceTimersByTime(59000), await waitMs()
Expand Down Expand Up @@ -196,6 +197,7 @@ describe.each([['query'], ['mutation']] as const)(
)

expect(onNewCacheEntry).toHaveBeenCalledWith('arg')
await promise
promise.unsubscribe(), await waitMs()
if (type === 'query') {
jest.advanceTimersByTime(59000), await waitMs()
Expand Down Expand Up @@ -241,6 +243,7 @@ describe.each([['query'], ['mutation']] as const)(
)

expect(onNewCacheEntry).toHaveBeenCalledWith('arg')
await promise

promise.unsubscribe(), await waitMs()
if (type === 'query') {
Expand Down Expand Up @@ -287,6 +290,7 @@ describe.each([['query'], ['mutation']] as const)(

expect(onNewCacheEntry).toHaveBeenCalledWith('arg')

await promise
promise.unsubscribe(), await waitMs()
if (type === 'query') {
jest.advanceTimersByTime(59000), await waitMs()
Expand Down Expand Up @@ -331,6 +335,7 @@ test(`query: getCacheEntry`, async () => {
const promise = storeRef.store.dispatch(
extended.endpoints.injected.initiate('arg')
)
await promise
promise.unsubscribe()

await fakeTimerWaitFor(() => {
Expand Down Expand Up @@ -499,6 +504,7 @@ test('updateCachedData', async () => {
const promise = storeRef.store.dispatch(
extended.endpoints.injected.initiate('arg')
)
await promise
promise.unsubscribe()

await fakeTimerWaitFor(() => {
Expand Down Expand Up @@ -536,7 +542,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) => ({
Expand All @@ -551,8 +557,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)
})
Expand Down
1 change: 1 addition & 0 deletions packages/toolkit/src/query/tests/cleanup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,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)
14 changes: 8 additions & 6 deletions packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,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!`,
})
Expand Down Expand Up @@ -333,7 +334,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!`,
})
Expand Down Expand Up @@ -434,7 +436,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
Expand All @@ -455,7 +457,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(
Expand All @@ -469,8 +472,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
})
})

Expand Down
4 changes: 3 additions & 1 deletion packages/toolkit/src/query/tests/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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,
}
}
Expand Down

0 comments on commit 56d4644

Please sign in to comment.