File size: 3,331 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
78
import nock from 'nock';
import { createStore, combineReducers, applyMiddleware } from 'redux'; // eslint-disable-line no-restricted-imports
import { thunk } from 'redux-thunk';
import { fetchImporterState, lockImport } from 'calypso/state/imports/actions';
import { appStates } from 'calypso/state/imports/constants';
import imports from 'calypso/state/imports/reducer';
import {
	isImporterStatusHydrated,
	getImporterStatus,
	getImporterStatusForSiteId,
} from 'calypso/state/imports/selectors';

const createReduxStore = () => {
	return createStore( combineReducers( { imports } ), applyMiddleware( thunk ) );
};

const testSiteId = 'en.blog.wordpress.com';

const queuePayload = ( payload ) =>
	nock( 'https://public-api.wordpress.com:443' )
		.get( `/rest/v1.1/sites/${ testSiteId }/imports/` )
		.replyWithFile( 200, `${ __dirname }/api-payloads/${ payload }.json`, {
			'Content-Type': 'application/json',
		} );

describe( 'Importer store', () => {
	describe( 'API integration', () => {
		test( 'should hydrate if the API returns a blank body', async () => {
			const store = createReduxStore();
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( false );
			queuePayload( 'no-imports' );
			await store.dispatch( fetchImporterState( testSiteId ) );
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( true );
			expect( getImporterStatusForSiteId( store.getState(), 0 ) ).toEqual( [] );
		} );

		test( 'should hydrate if the API returns a defunct importer', async () => {
			const store = createReduxStore();
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( false );
			queuePayload( 'defunct-importer' );
			await store.dispatch( fetchImporterState( testSiteId ) );
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( true );
			expect( getImporterStatusForSiteId( store.getState(), 0 ) ).toEqual( [] );
		} );

		test( 'should hydrate if the API returns an expired importer', async () => {
			const store = createReduxStore();
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( false );
			queuePayload( 'expired-importer' );
			await store.dispatch( fetchImporterState( testSiteId ) );
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( true );
			expect( getImporterStatusForSiteId( store.getState(), 0 ) ).toEqual( [] );
		} );

		test( 'should hydrate if the API returns a running importer', async () => {
			const store = createReduxStore();
			const testImporterId = 'runningImporter';
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( false );
			queuePayload( 'running-importer' );
			await store.dispatch( fetchImporterState( testSiteId ) );
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( true );
			expect( getImporterStatus( store.getState(), testImporterId )?.importerState ).toBe(
				appStates.IMPORTING
			);
		} );

		test( 'should ignore an update to importer that is locked', async () => {
			const store = createReduxStore();
			const testImporterId = 'runningImporter';
			queuePayload( 'running-importer' );
			store.dispatch( lockImport( testImporterId ) );
			await store.dispatch( fetchImporterState( testSiteId ) );
			expect( isImporterStatusHydrated( store.getState() ) ).toBe( true );
			expect( getImporterStatusForSiteId( store.getState(), 0 ) ).toEqual( [] );
		} );
	} );
} );