2022-09-22 15:43:48 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import reducer from '../reducers';
|
|
|
|
import { defaultState } from '../default-state';
|
|
|
|
import { STATUS } from '../constants';
|
|
|
|
import * as actions from '../actions';
|
|
|
|
|
|
|
|
describe.only( 'Checkout Store Reducer', () => {
|
|
|
|
it( 'should return the initial state', () => {
|
|
|
|
expect( reducer( undefined, {} ) ).toEqual( defaultState );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_IDLE', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
};
|
|
|
|
|
2022-10-05 10:04:16 +00:00
|
|
|
expect( reducer( defaultState, actions.__internalSetIdle() ) ).toEqual(
|
2022-09-22 15:43:48 +00:00
|
|
|
expectedState
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_REDIRECT_URL', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
redirectUrl: 'https://example.com',
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
|
|
|
reducer(
|
|
|
|
defaultState,
|
2022-10-05 10:04:16 +00:00
|
|
|
actions.__internalSetRedirectUrl( 'https://example.com' )
|
2022-09-22 15:43:48 +00:00
|
|
|
)
|
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_COMPLETE', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.COMPLETE,
|
|
|
|
redirectUrl: 'https://example.com',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
|
|
|
reducer(
|
|
|
|
defaultState,
|
2022-10-05 10:04:16 +00:00
|
|
|
actions.__internalSetComplete( {
|
2022-09-22 15:43:48 +00:00
|
|
|
redirectUrl: 'https://example.com',
|
|
|
|
} )
|
|
|
|
)
|
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_PROCESSING', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.PROCESSING,
|
|
|
|
};
|
|
|
|
|
2022-10-05 10:04:16 +00:00
|
|
|
expect(
|
|
|
|
reducer( defaultState, actions.__internalSetProcessing() )
|
|
|
|
).toEqual( expectedState );
|
2022-09-22 15:43:48 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_HAS_ERROR when status is PROCESSING', () => {
|
|
|
|
const initialState = { ...defaultState, status: STATUS.PROCESSING };
|
|
|
|
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
hasError: true,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
};
|
|
|
|
|
2022-10-05 10:04:16 +00:00
|
|
|
expect(
|
|
|
|
reducer( initialState, actions.__internalSetHasError( true ) )
|
|
|
|
).toEqual( expectedState );
|
2022-09-22 15:43:48 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_HAS_ERROR when status is BEFORE_PROCESSING', () => {
|
|
|
|
const initialState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.BEFORE_PROCESSING,
|
|
|
|
};
|
|
|
|
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
hasError: true,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
};
|
|
|
|
|
2022-10-05 10:04:16 +00:00
|
|
|
expect(
|
|
|
|
reducer( initialState, actions.__internalSetHasError( true ) )
|
|
|
|
).toEqual( expectedState );
|
2022-09-22 15:43:48 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_HAS_ERROR when status is anything else', () => {
|
|
|
|
const initialState = { ...defaultState, status: STATUS.PRISTINE };
|
|
|
|
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
hasError: false,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
};
|
|
|
|
|
2022-10-05 10:04:16 +00:00
|
|
|
expect(
|
|
|
|
reducer( initialState, actions.__internalSetHasError( false ) )
|
|
|
|
).toEqual( expectedState );
|
2022-09-22 15:43:48 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_BEFORE_PROCESSING', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.BEFORE_PROCESSING,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
2022-10-05 10:04:16 +00:00
|
|
|
reducer( defaultState, actions.__internalSetBeforeProcessing() )
|
2022-09-22 15:43:48 +00:00
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_AFTER_PROCESSING', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.AFTER_PROCESSING,
|
|
|
|
};
|
|
|
|
|
2022-10-05 10:04:16 +00:00
|
|
|
expect(
|
|
|
|
reducer( defaultState, actions.__internalSetAfterProcessing() )
|
|
|
|
).toEqual( expectedState );
|
2022-09-22 15:43:48 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle INCREMENT_CALCULATING', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
calculatingCount: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
2022-10-05 10:04:16 +00:00
|
|
|
reducer( defaultState, actions.__internalIncrementCalculating() )
|
2022-09-22 15:43:48 +00:00
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle DECREMENT_CALCULATING', () => {
|
|
|
|
const initialState = {
|
|
|
|
...defaultState,
|
|
|
|
calculatingCount: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
calculatingCount: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
2022-10-05 10:04:16 +00:00
|
|
|
reducer( initialState, actions.__internalDecrementCalculating() )
|
2022-09-22 15:43:48 +00:00
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_CUSTOMER_ID', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
customerId: 1,
|
|
|
|
};
|
|
|
|
|
2022-10-05 10:04:16 +00:00
|
|
|
expect(
|
|
|
|
reducer( defaultState, actions.__internalSetCustomerId( 1 ) )
|
|
|
|
).toEqual( expectedState );
|
2022-09-22 15:43:48 +00:00
|
|
|
} );
|
|
|
|
|
2022-11-21 14:04:53 +00:00
|
|
|
it( 'should handle SET_USE_SHIPPING_AS_BILLING', () => {
|
2022-09-22 15:43:48 +00:00
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
useShippingAsBilling: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
2022-10-05 10:04:16 +00:00
|
|
|
reducer(
|
|
|
|
defaultState,
|
|
|
|
actions.__internalSetUseShippingAsBilling( false )
|
|
|
|
)
|
2022-09-22 15:43:48 +00:00
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_SHOULD_CREATE_ACCOUNT', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
shouldCreateAccount: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
2022-10-05 10:04:16 +00:00
|
|
|
reducer(
|
|
|
|
defaultState,
|
|
|
|
actions.__internalSetShouldCreateAccount( true )
|
|
|
|
)
|
2022-09-22 15:43:48 +00:00
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should handle SET_ORDER_NOTES', () => {
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
orderNotes: 'test',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
2022-10-05 10:04:16 +00:00
|
|
|
reducer( defaultState, actions.__internalSetOrderNotes( 'test' ) )
|
2022-09-22 15:43:48 +00:00
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
|
2023-02-03 11:18:18 +00:00
|
|
|
describe( 'should handle SET_EXTENSION_DATA', () => {
|
|
|
|
it( 'should set data under a namespace', () => {
|
|
|
|
const mockExtensionData = {
|
|
|
|
extensionNamespace: {
|
|
|
|
testKey: 'test-value',
|
|
|
|
testKey2: 'test-value-2',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
extensionData: mockExtensionData,
|
|
|
|
};
|
|
|
|
expect(
|
|
|
|
reducer(
|
|
|
|
defaultState,
|
|
|
|
actions.__internalSetExtensionData(
|
|
|
|
'extensionNamespace',
|
|
|
|
mockExtensionData.extensionNamespace
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
it( 'should append data under a namespace', () => {
|
|
|
|
const mockExtensionData = {
|
|
|
|
extensionNamespace: {
|
|
|
|
testKey: 'test-value',
|
|
|
|
testKey2: 'test-value-2',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
extensionData: mockExtensionData,
|
|
|
|
};
|
|
|
|
const firstState = reducer(
|
2022-09-22 15:43:48 +00:00
|
|
|
defaultState,
|
2023-02-03 11:18:18 +00:00
|
|
|
actions.__internalSetExtensionData( 'extensionNamespace', {
|
|
|
|
testKey: 'test-value',
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
const secondState = reducer(
|
|
|
|
firstState,
|
|
|
|
actions.__internalSetExtensionData( 'extensionNamespace', {
|
|
|
|
testKey2: 'test-value-2',
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
expect( secondState ).toEqual( expectedState );
|
|
|
|
} );
|
|
|
|
it( 'support replacing data under a namespace', () => {
|
|
|
|
const mockExtensionData = {
|
|
|
|
extensionNamespace: {
|
|
|
|
testKey: 'test-value',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const expectedState = {
|
|
|
|
...defaultState,
|
|
|
|
status: STATUS.IDLE,
|
|
|
|
extensionData: mockExtensionData,
|
|
|
|
};
|
|
|
|
const firstState = reducer(
|
|
|
|
defaultState,
|
|
|
|
actions.__internalSetExtensionData( 'extensionNamespace', {
|
|
|
|
testKeyOld: 'test-value',
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
const secondState = reducer(
|
|
|
|
firstState,
|
|
|
|
actions.__internalSetExtensionData(
|
|
|
|
'extensionNamespace',
|
|
|
|
{ testKey: 'test-value' },
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
expect( secondState ).toEqual( expectedState );
|
|
|
|
} );
|
2022-09-22 15:43:48 +00:00
|
|
|
} );
|
|
|
|
} );
|