source: node_modules/@reduxjs/toolkit/src/query/tests/errorHandling.test-d.tsx

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

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