File size: 1,670 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
/**
 * @jest-environment jsdom
 */
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, waitFor } from '@testing-library/react';
import React, { useEffect } from 'react';
import { EmailDeliveryFrequency } from '../../constants';
import { callApi } from '../../helpers';
import useSiteDeliveryFrequencyMutation from '../../mutations/use-site-delivery-frequency-mutation';

// Mock the useIsLoggedIn function
jest.mock( '../../hooks', () => ( {
	useIsLoggedIn: jest.fn().mockReturnValue( { isLoggedIn: true } ),
	useCacheKey: jest.fn(),
} ) );

// Mock the entire Helpers module
jest.mock( '../../helpers', () => ( {
	callApi: jest.fn(),
	applyCallbackToPages: jest.fn(),
	buildQueryKey: jest.fn(),
} ) );

const client = new QueryClient();
const Parent = ( { children } ) => (
	<QueryClientProvider client={ client }>{ children }</QueryClientProvider>
);

describe( 'useSiteDeliveryFrequencyMutation()', () => {
	it( 'calls the right API', async () => {
		const Skeleton = () => {
			const { mutate } = useSiteDeliveryFrequencyMutation();
			useEffect( () => {
				mutate( {
					delivery_frequency: EmailDeliveryFrequency.Daily,
					blog_id: '123',
					subscriptionId: 456,
				} );
			}, [ mutate ] );

			return <p></p>;
		};

		( callApi as jest.Mock ).mockResolvedValue( {
			success: true,
		} );

		render(
			<Parent>
				<Skeleton />
			</Parent>
		);

		await waitFor( () =>
			expect( callApi ).toHaveBeenCalledWith( {
				apiVersion: '1.2',
				path: '/read/site/123/post_email_subscriptions/update',
				body: {
					delivery_frequency: 'daily',
				},
				isLoggedIn: true,
				method: 'POST',
			} )
		);
	} );
} );