File size: 2,324 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
/**
 * @jest-environment jsdom
 */

import { render } from '@testing-library/react';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import QueryJetpackScanHistory from 'calypso/components/data/query-jetpack-scan-history';
import { JETPACK_SCAN_HISTORY_REQUEST } from 'calypso/state/action-types';
import * as jetpackScanActions from 'calypso/state/jetpack-scan/history/actions';

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

	const initialState = {
		jetpackScan: { history: { requestStatus: siteId } },
	};

	const store = createStore( ( state ) => state, initialState );

	// eslint-disable-next-line no-shadow
	const renderUI = ( siteId ) => (
		<Provider store={ store }>
			<QueryJetpackScanHistory siteId={ siteId } />
		</Provider>
	);

	const utils = render( renderUI( siteId ) );

	return { renderUI, utils, requestJetpackScanHistoryActionSpy };
}

describe( 'QueryJetpackScanHistory', () => {
	it( 'should not dispatch the action if the siteId is null', () => {
		const siteId = null;
		const { requestJetpackScanHistoryActionSpy } = setup( siteId );
		expect( requestJetpackScanHistoryActionSpy ).not.toHaveBeenCalled();
	} );

	it( 'should request Jetpack Scan History every time siteId changes', () => {
		const siteId = 9999;
		const { renderUI, requestJetpackScanHistoryActionSpy, utils } = setup( siteId );
		expect( requestJetpackScanHistoryActionSpy ).toHaveBeenCalled();
		expect( requestJetpackScanHistoryActionSpy ).toHaveBeenCalledWith( siteId );
		expect( requestJetpackScanHistoryActionSpy ).toHaveBeenCalledTimes( 1 );
		expect( requestJetpackScanHistoryActionSpy ).toHaveReturnedWith(
			expect.objectContaining( {
				type: JETPACK_SCAN_HISTORY_REQUEST,
				siteId,
			} )
		);

		const newSiteId = 10000;
		utils.rerender( renderUI( newSiteId ) );
		expect( requestJetpackScanHistoryActionSpy ).toHaveBeenCalledWith( newSiteId );
		expect( requestJetpackScanHistoryActionSpy ).toHaveBeenCalledTimes( 2 );
		expect( requestJetpackScanHistoryActionSpy ).toHaveReturnedWith(
			expect.objectContaining( {
				type: JETPACK_SCAN_HISTORY_REQUEST,
				siteId: newSiteId,
			} )
		);
	} );
} );