Spaces:
Sleeping
Sleeping
File size: 1,550 Bytes
b593f0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { useState } from 'react'
const mockSuccessResponse = { value: 'success' }
const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
endpoints: (build) => ({
update: build.mutation<typeof mockSuccessResponse, any>({
query: () => ({ url: 'success' }),
}),
failedUpdate: build.mutation<typeof mockSuccessResponse, any>({
query: () => ({ url: 'error' }),
}),
}),
})
describe('type tests', () => {
test('a mutation is unwrappable and has the correct types', () => {
function User() {
const [manualError, setManualError] = useState<any>()
const [update, { isLoading, data, error }] =
api.endpoints.update.useMutation()
return (
<div>
<div data-testid="isLoading">{String(isLoading)}</div>
<div data-testid="data">{JSON.stringify(data)}</div>
<div data-testid="error">{JSON.stringify(error)}</div>
<div data-testid="manuallySetError">
{JSON.stringify(manualError)}
</div>
<button
onClick={() => {
update({ name: 'hello' })
.unwrap()
.then((result) => {
expectTypeOf(result).toEqualTypeOf(mockSuccessResponse)
setManualError(undefined)
})
.catch(setManualError)
}}
>
Update User
</button>
</div>
)
}
})
})
|