Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas authored and markerikson committed Oct 19, 2023
1 parent e0c69b9 commit f291d09
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 @@ -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
)
})
})
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()
vi.advanceTimersByTime(59000)
expect(onCleanup).not.toHaveBeenCalled()
vi.advanceTimersByTime(2000)
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()
vi.advanceTimersByTime(28000)
expect(onCleanup).not.toHaveBeenCalled()
vi.advanceTimersByTime(2000)
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
vi.advanceTimersByTime(1000)
Expand Down Expand Up @@ -110,15 +116,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()
vi.advanceTimersByTime(28000)
expect(onCleanup).not.toHaveBeenCalled()
vi.advanceTimersByTime(2000)
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()

vi.advanceTimersByTime(34000)
expect(onCleanup).not.toHaveBeenCalled()
vi.advanceTimersByTime(2000)
Expand All @@ -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()
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 @@ -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 {
Expand Down Expand Up @@ -219,6 +220,7 @@ describe.each([['query'], ['mutation']] as const)(
)

expect(onNewCacheEntry).toHaveBeenCalledWith('arg')
await promise
if (type === 'mutation') {
promise.reset()
} else {
Expand Down Expand Up @@ -270,6 +272,7 @@ describe.each([['query'], ['mutation']] as const)(
)

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

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

expect(onNewCacheEntry).toHaveBeenCalledWith('arg')

await promise
if (type === 'mutation') {
promise.reset()
} else {
Expand Down Expand Up @@ -370,6 +374,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 @@ -539,6 +544,7 @@ test('updateCachedData', async () => {
const promise = storeRef.store.dispatch(
extended.endpoints.injected.initiate('arg')
)
await promise
promise.unsubscribe()

await fakeTimerWaitFor(() => {
Expand Down Expand Up @@ -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) => ({
Expand All @@ -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)
})
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 @@ -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)
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 @@ -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!`,
})
Expand Down Expand Up @@ -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!`,
})
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
})
})

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 @@ -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,
}
}
Expand Down

0 comments on commit f291d09

Please sign in to comment.