File size: 2,023 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * @jest-environment jsdom
 */
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { waitFor } from '@testing-library/dom';
import { renderHook } from '@testing-library/react';
import React from 'react';
import { act } from 'react-dom/test-utils';
import wpcomRequest from 'wpcom-proxy-request';
import { useSiteResetMutation } from '../use-site-reset-mutation';

jest.mock( 'wpcom-proxy-request', () => ( {
	__esModule: true,
	default: jest.fn(),
} ) );

describe( 'use-site-reset-mutation hook', () => {
	beforeEach( () => {
		jest.clearAllMocks();
	} );

	test( 'returns success from the api', async () => {
		const queryClient = new QueryClient( { defaultOptions: { queries: { retry: false } } } );
		const wrapper = ( { children } ) => (
			<QueryClientProvider client={ queryClient }>{ children }</QueryClientProvider>
		);

		const expected = {
			success: true,
		};

		( wpcomRequest as jest.Mock ).mockImplementation( () => Promise.resolve( expected ) );

		const { result } = renderHook( () => useSiteResetMutation(), {
			wrapper,
		} );

		expect( result.current.isSuccess ).toBe( false );

		act( () => {
			result.current.resetSite( 123 );
		} );

		await waitFor( () => {
			expect( result.current.isSuccess ).toBe( true );
			expect( result.current.data ).toEqual( expected );
		} );
	} );

	test( 'returns failure from the api', async () => {
		const queryClient = new QueryClient();
		const wrapper = ( { children } ) => (
			<QueryClientProvider client={ queryClient }>{ children }</QueryClientProvider>
		);

		const error = {
			code: 'Unauthorized',
			message: 'Something went wrong',
		};

		( wpcomRequest as jest.Mock ).mockImplementation( () => Promise.reject( error ) );

		const { result } = renderHook( () => useSiteResetMutation(), {
			wrapper,
		} );

		act( () => {
			result.current.resetSite( 123 );
		} );

		await waitFor( () => {
			expect( result.current.isError ).toBe( true );
			expect( result.current.error ).toEqual( error );
		} );
	} );
} );