Skip to content

Commit

Permalink
Fix console spy related issues in queryFn.test.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Dec 3, 2024
1 parent f08469a commit b61f8fb
Showing 1 changed file with 83 additions and 45 deletions.
128 changes: 83 additions & 45 deletions packages/toolkit/src/query/tests/queryFn.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { noop } from '@internal/listenerMiddleware/utils'
import type { QuerySubState } from '@internal/query/core/apiState'
import type { Post } from '@internal/query/tests/mocks/handlers'
import { posts } from '@internal/query/tests/mocks/handlers'
Expand Down Expand Up @@ -185,16 +186,25 @@ describe('queryFn base implementation tests', () => {
['withAsyncErrorQueryFn', withAsyncErrorQueryFn, 'error'],
['withAsyncThrowingQueryFn', withAsyncThrowingQueryFn, 'throw'],
])('%s', async (endpointName, endpoint, expectedResult) => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)

const thunk = endpoint.initiate(endpointName)
let result: undefined | QuerySubState<any> = undefined
await expect(async () => {
result = await store.dispatch(thunk)
}).toHaveConsoleOutput(
endpointName.includes('Throw')
? `An unhandled error occurred processing a request for the endpoint "${endpointName}".
In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: resultFrom(${endpointName})]`
: '',
)

const result = (await store.dispatch(thunk)) satisfies
| undefined
| QuerySubState<any>

if (endpointName.includes('Throw')) {
expect(consoleErrorSpy).toHaveBeenCalledOnce()

expect(consoleErrorSpy).toHaveBeenLastCalledWith(
`An unhandled error occurred processing a request for the endpoint "${endpointName}".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
Error(`resultFrom(${endpointName})`),
)
} else {
expect(consoleErrorSpy).not.toHaveBeenCalled()
}

if (expectedResult === 'data') {
expect(result).toEqual(
expect.objectContaining({
Expand All @@ -216,6 +226,8 @@ describe('queryFn base implementation tests', () => {
}),
)
}

consoleErrorSpy.mockRestore()
})

test.each([
Expand All @@ -230,19 +242,25 @@ describe('queryFn base implementation tests', () => {
'throw',
],
])('%s', async (endpointName, endpoint, expectedResult) => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)

const thunk = endpoint.initiate(endpointName)
let result:

const result = (await store.dispatch(thunk)) satisfies
| undefined
| { data: string }
| { error: string | SerializedError } = undefined
await expect(async () => {
result = await store.dispatch(thunk)
}).toHaveConsoleOutput(
endpointName.includes('Throw')
? `An unhandled error occurred processing a request for the endpoint "${endpointName}".
In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: resultFrom(${endpointName})]`
: '',
)
| { error: string | SerializedError }

if (endpointName.includes('Throw')) {
expect(consoleErrorSpy).toHaveBeenCalledOnce()

expect(consoleErrorSpy).toHaveBeenLastCalledWith(
`An unhandled error occurred processing a request for the endpoint "${endpointName}".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
Error(`resultFrom(${endpointName})`),
)
} else {
expect(consoleErrorSpy).not.toHaveBeenCalled()
}

if (expectedResult === 'data') {
expect(result).toEqual(
Expand All @@ -265,42 +283,56 @@ describe('queryFn base implementation tests', () => {
}),
)
}

consoleErrorSpy.mockRestore()
})

test('neither provided', async () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)

{
const thunk = withNeither.initiate('withNeither')
let result: QuerySubState<any>
await expect(async () => {
result = await store.dispatch(thunk)
}).toHaveConsoleOutput(
`An unhandled error occurred processing a request for the endpoint "withNeither".
In the case of an unhandled error, no tags will be "provided" or "invalidated". [TypeError: endpointDefinition.queryFn is not a function]`,

const result = (await store.dispatch(thunk)) satisfies QuerySubState<any>

expect(consoleErrorSpy).toHaveBeenCalledOnce()

expect(consoleErrorSpy).toHaveBeenLastCalledWith(
`An unhandled error occurred processing a request for the endpoint "withNeither".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
TypeError('endpointDefinition.queryFn is not a function'),
)
expect(result!.error).toEqual(

expect(result.error).toEqual(
expect.objectContaining({
message: 'endpointDefinition.queryFn is not a function',
}),
)

consoleErrorSpy.mockClear()
}
{
let result:
const thunk = mutationWithNeither.initiate('mutationWithNeither')

const result = (await store.dispatch(thunk)) satisfies
| undefined
| { data: string }
| { error: string | SerializedError } = undefined
const thunk = mutationWithNeither.initiate('mutationWithNeither')
await expect(async () => {
result = await store.dispatch(thunk)
}).toHaveConsoleOutput(
`An unhandled error occurred processing a request for the endpoint "mutationWithNeither".
In the case of an unhandled error, no tags will be "provided" or "invalidated". [TypeError: endpointDefinition.queryFn is not a function]`,
| { error: string | SerializedError }

expect(consoleErrorSpy).toHaveBeenCalledOnce()

expect(consoleErrorSpy).toHaveBeenLastCalledWith(
`An unhandled error occurred processing a request for the endpoint "mutationWithNeither".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
TypeError('endpointDefinition.queryFn is not a function'),
)
expect((result as any).error).toEqual(

expect(result.error).toEqual(
expect.objectContaining({
message: 'endpointDefinition.queryFn is not a function',
}),
)
}

consoleErrorSpy.mockRestore()
})
})

Expand Down Expand Up @@ -385,21 +417,27 @@ describe('usage scenario tests', () => {
})

it('can wrap a service like Firebase and handle errors', async () => {
let result: QuerySubState<any>
await expect(async () => {
result = await storeRef.store.dispatch(
api.endpoints.getMissingFirebaseUser.initiate(1),
)
})
.toHaveConsoleOutput(`An unhandled error occurred processing a request for the endpoint "getMissingFirebaseUser".
In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: Missing user]`)
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)

const result = (await storeRef.store.dispatch(
api.endpoints.getMissingFirebaseUser.initiate(1),
)) satisfies QuerySubState<any>

expect(result!.data).toBeUndefined()
expect(result!.error).toEqual(
expect(consoleErrorSpy).toHaveBeenCalledOnce()

expect(consoleErrorSpy).toHaveBeenLastCalledWith(
`An unhandled error occurred processing a request for the endpoint "getMissingFirebaseUser".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
Error('Missing user'),
)

expect(result.data).toBeUndefined()
expect(result.error).toEqual(
expect.objectContaining({
message: 'Missing user',
name: 'Error',
}),
)

consoleErrorSpy.mockRestore()
})
})

0 comments on commit b61f8fb

Please sign in to comment.