File size: 2,511 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
import {
	AUTOMATED_TRANSFER_ELIGIBILITY_UPDATE as ELIGIBILITY_UPDATE,
	AUTOMATED_TRANSFER_STATUS_REQUEST as REQUEST_STATUS,
	AUTOMATED_TRANSFER_STATUS_REQUEST_FAILURE as REQUEST_STATUS_FAILURE,
} from 'calypso/state/action-types';
import { serialize, deserialize } from 'calypso/state/utils';
import { transferStates } from '../constants';
import reducer, { status, fetchingStatus } from '../reducer';

describe( 'state', () => {
	describe( 'automated-transfer', () => {
		describe( 'reducer', () => {
			describe( 'eligibility', () => {
				const update = { type: ELIGIBILITY_UPDATE };

				test( 'should set inquiring status when first polling eligibility', () => {
					expect( status( null, update ) ).toEqual( transferStates.INQUIRING );
				} );

				test( 'should not overwrite the status when a valid state already exists', () => {
					expect( status( transferStates.START, update ) ).toEqual( transferStates.START );
				} );
			} );

			describe( 'fetchingStatus', () => {
				test( 'should be false when irrelevant action is supplied', () => {
					expect( fetchingStatus( false, { type: ELIGIBILITY_UPDATE } ) ).toBe( false );
				} );

				test( 'should be false when fetching the status is unsuccessful', () => {
					expect( fetchingStatus( true, { type: REQUEST_STATUS_FAILURE } ) ).toBe( false );
				} );

				test( 'should be truthy when fetching transfer status', () => {
					const action = { type: REQUEST_STATUS };

					expect( fetchingStatus( null, action ) ).toBe( true );
				} );
			} );

			test( 'should persist all state keys except fetchingStatus', () => {
				const SITE_ID = 12345;
				const AT_STATE = {
					[ SITE_ID ]: {
						status: 'backfilling',
						eligibility: {
							eligibilityHolds: [],
							eligibilityWarnings: [],
							lastUpdate: 1000000000,
						},
						fetchingStatus: true,
					},
				};

				const serialized = serialize( reducer, AT_STATE ).root();
				expect( serialized[ SITE_ID ] ).toHaveProperty( 'status' );
				expect( serialized[ SITE_ID ] ).toHaveProperty( 'eligibility' );
				expect( serialized[ SITE_ID ] ).not.toHaveProperty( 'fetchingStatus' );

				const deserialized = deserialize( reducer, AT_STATE );
				expect( deserialized[ SITE_ID ] ).toHaveProperty( 'status' );
				expect( deserialized[ SITE_ID ] ).toHaveProperty( 'eligibility' );
				// The non-persisted property has default value, persisted value is ignored
				expect( deserialized[ SITE_ID ] ).toHaveProperty( 'fetchingStatus', false );
			} );
		} );
	} );
} );