File size: 2,142 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
import { createReduxStore } from 'calypso/state';
import { JETPACK_SCAN_REQUEST } from 'calypso/state/action-types';
import * as wpcomHttpActions from 'calypso/state/data-layer/wpcom-http/actions';
import * as jetpackScanActions from 'calypso/state/jetpack-scan/actions';

jest.mock( 'calypso/state/data-layer/wpcom-http/actions', () => ( {
	http: jest.fn(),
} ) );

function setup( siteId ) {
	// Set spy on action creator to verify it gets called when the component renders.
	const requestScanStatusActionSpy = jest.spyOn( jetpackScanActions, 'requestScanStatus' );

	const store = createReduxStore(
		{
			jetpackScan: { requestStatus: siteId },
		},
		( state ) => {
			return state;
		}
	);

	const dispatchSpy = jest.spyOn( store, 'dispatch' );

	return { store, dispatchSpy, requestScanStatusActionSpy };
}

describe( 'Jetpack Scan data-layer', () => {
	beforeAll( () => {
		wpcomHttpActions.http.mockImplementation( () => ( {
			type: JETPACK_SCAN_REQUEST,
			meta: { dataLayer: { data: 'Fake data!' } },
		} ) );
	} );

	afterEach( () => {
		jest.clearAllMocks();
	} );

	it( 'should reach the fetch handler middleware', () => {
		const siteId = 9999;
		const { store, dispatchSpy, requestScanStatusActionSpy } = setup( siteId );

		// Dispatch action to fetch Jetpack Scan status
		store.dispatch( jetpackScanActions.requestScanStatus( siteId ) );
		expect( requestScanStatusActionSpy ).toHaveBeenCalled();

		// Verify the dispatch function was called with the right argument
		expect( dispatchSpy ).toHaveBeenCalled();

		const expectedAction = {
			type: JETPACK_SCAN_REQUEST,
			siteId: siteId,
		};

		expect( dispatchSpy ).toHaveBeenCalledWith( expect.objectContaining( expectedAction ) );

		// Check the http util method was called from the fetch handler
		expect( wpcomHttpActions.http ).toHaveBeenCalled();

		// Check the http was called with the right arguments (target, action)
		expect( wpcomHttpActions.http ).toHaveBeenCalledWith(
			expect.objectContaining( {
				apiNamespace: 'wpcom/v2',
				method: 'GET',
				path: `/sites/${ siteId }/scan`,
			} ),
			expect.objectContaining( expectedAction )
		);
	} );
} );