File size: 4,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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
import deepFreeze from 'deep-freeze';
import {
BILLING_TRANSACTIONS_RECEIVE,
BILLING_TRANSACTIONS_REQUEST,
BILLING_TRANSACTIONS_REQUEST_FAILURE,
BILLING_TRANSACTIONS_REQUEST_SUCCESS,
} from 'calypso/state/action-types';
import { serialize, deserialize } from 'calypso/state/utils';
import reducer, { requesting, items } from '../reducer';
describe( 'reducer', () => {
jest.spyOn( console, 'warn' ).mockImplementation();
test( 'should include expected keys in return value', () => {
expect( Object.keys( reducer( undefined, {} ) ) ).toEqual(
expect.arrayContaining( [ 'requesting', 'items', 'individualTransactions' ] )
);
} );
describe( '#requesting()', () => {
test( 'should default to an false', () => {
const state = requesting( undefined, {} );
expect( state ).toBe( false );
} );
test( 'should set requesting to true value if a request is initiated', () => {
const state = requesting( undefined, {
type: BILLING_TRANSACTIONS_REQUEST,
} );
expect( state ).toBe( true );
} );
test( 'should set requesting to false if request finishes successfully', () => {
const state = requesting( true, {
type: BILLING_TRANSACTIONS_REQUEST_SUCCESS,
} );
expect( state ).toEqual( false );
} );
test( 'should set requesting to false if request finishes unsuccessfully', () => {
const state = requesting( true, {
type: BILLING_TRANSACTIONS_REQUEST_FAILURE,
} );
expect( state ).toEqual( false );
} );
} );
describe( '#items()', () => {
const billingTransactions = {
past: [
{
id: '12345678',
amount: '$1.23',
},
],
upcoming: [
{
id: '87654321',
amount: '$4.56',
},
],
};
test( 'should default to empty object', () => {
const state = items( undefined, {} );
expect( state ).toEqual( {} );
} );
test( 'should store the billing transactions properly', () => {
const state = items( null, {
type: BILLING_TRANSACTIONS_RECEIVE,
...billingTransactions,
} );
expect( state ).toEqual( billingTransactions );
} );
test( 'should override previous billing transactions', () => {
const state = items(
deepFreeze( {
past: [
{
id: '11223344',
amount: '$3.43',
desc: 'test',
},
],
upcoming: [
{
id: '88776655',
amount: '$1.11',
product: 'example',
},
],
} ),
{
type: BILLING_TRANSACTIONS_RECEIVE,
...billingTransactions,
}
);
expect( state ).toEqual( billingTransactions );
} );
test( 'should keep previous billing transactions if query filtered', () => {
const prevState = {
past: [
{
id: '11223344',
amount: '$3.43',
desc: 'test',
},
],
upcoming: [
{
id: '88776655',
amount: '$1.11',
product: 'example',
},
],
};
// Keep previous upcoming transactions if only updating past transactions
const stateWithUpdatedPast = items( deepFreeze( prevState ), {
type: BILLING_TRANSACTIONS_RECEIVE,
past: billingTransactions.past,
} );
expect( stateWithUpdatedPast ).toEqual( {
upcoming: prevState.upcoming,
past: billingTransactions.past,
} );
// Keep previous past transactions if only updating upcoming transactions
const stateWIthUpdatedUpcoming = items( deepFreeze( prevState ), {
type: BILLING_TRANSACTIONS_RECEIVE,
upcoming: billingTransactions.upcoming,
} );
expect( stateWIthUpdatedUpcoming ).toEqual( {
past: prevState.past,
upcoming: billingTransactions.upcoming,
} );
} );
test( 'should persist state', () => {
const state = serialize( items, deepFreeze( billingTransactions ) );
expect( state ).toEqual( billingTransactions );
} );
test( 'should load valid persisted state', () => {
const state = deserialize( items, deepFreeze( billingTransactions ) );
expect( state ).toEqual( billingTransactions );
} );
test( 'should not load invalid persisted state', () => {
const state = deserialize( items, deepFreeze( { example: 'test' } ) );
expect( state ).toEqual( {} );
} );
} );
} );
|