Skip to content

Commit

Permalink
chore: Payment State Polling Reducer Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grmartin committed Jul 5, 2023
1 parent e9d2272 commit 24bde53
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/payment/data/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,19 @@ const paymentState = (state = basketInitialState, action = null) => {
},
};

case pollPaymentState.RECEIVED:
case pollPaymentState.RECEIVED: {
const isHttpError = action.payload.state === PAYMENT_STATE.HTTP_ERROR;
const currErrorCount = (isHttpError ? state.paymentStatePolling.errorCount - 1 : maxErrors);
return {
...state,
paymentState: action.payload.state,
paymentStatePolling: {
...state.paymentStatePolling,
keepPolling: shouldPoll(action.payload.state),
errorCount: (action.payload.state === PAYMENT_STATE.HTTP_ERROR
? state.paymentStatePolling.errorCount - 1 : maxErrors),
keepPolling: currErrorCount > 0 && shouldPoll(action.payload.state),
errorCount: currErrorCount,
},
};
}

default:
}
Expand Down
46 changes: 43 additions & 3 deletions src/payment/data/redux.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,10 @@ describe('redux tests', () => {
});
});

describe('pollPaymentState actions', () => {
it('Round Trip', () => {
const triggerStore = createStore(
describe('pollPaymentState actions + reducers', () => {
let triggerStore;
beforeEach(() => {
triggerStore = createStore(
combineReducers({
payment: reducer,
}),
Expand All @@ -252,7 +253,11 @@ describe('redux tests', () => {
},
},
);
});

afterEach(() => { triggerStore = undefined; });

it('Round Trip (No Error)', () => {
triggerStore.dispatch(pollPaymentState());
expect(triggerStore.getState().payment.basket.paymentStatePolling.keepPolling).toBe(true);
expect(triggerStore.getState().payment.basket.paymentState).toBe(PAYMENT_STATE.PENDING);
Expand All @@ -265,6 +270,41 @@ describe('redux tests', () => {
expect(triggerStore.getState().payment.basket.paymentStatePolling.keepPolling).toBe(false);
expect(triggerStore.getState().payment.basket.paymentState === PAYMENT_STATE.PENDING).toBe(false);
});

it('Round Trip (Fatal Error)', () => {
triggerStore.dispatch(pollPaymentState());
expect(triggerStore.getState().payment.basket.paymentStatePolling.keepPolling).toBe(true);
expect(triggerStore.getState().payment.basket.paymentState).toBe(PAYMENT_STATE.PENDING);

triggerStore.dispatch(pollPaymentState.failure(Error('Something broke!')));
expect(triggerStore.getState().payment.basket.paymentStatePolling.keepPolling).toBe(false);
expect(triggerStore.getState().payment.basket.paymentState).toBe(null);
});

it('Round Trip (Max HTTP Error)', () => {
const pollingMaxErrors = DEFAULT_PAYMENT_STATE_POLLING_MAX_ERRORS;

triggerStore.dispatch(pollPaymentState());
expect(triggerStore.getState().payment.basket.paymentStatePolling.keepPolling).toBe(true);
expect(triggerStore.getState().payment.basket.paymentState).toBe(PAYMENT_STATE.PENDING);

for (let i = 0; pollingMaxErrors > i; i++) {
const expectedCount = pollingMaxErrors - 1 - i;
triggerStore.dispatch(pollPaymentState.received({ state: PAYMENT_STATE.HTTP_ERROR }));
expect(triggerStore.getState().payment.basket.paymentStatePolling.keepPolling)
.toBe(expectedCount > 0);
expect(triggerStore.getState().payment.basket.paymentStatePolling.errorCount)
.toBe(expectedCount);
}

expect(triggerStore.getState().payment.basket.paymentStatePolling.keepPolling).toBe(false);
expect(triggerStore.getState().payment.basket.paymentStatePolling.errorCount)
.toBe(0);

triggerStore.dispatch(pollPaymentState.failure(Error('Too many HTTP errors!')));
expect(triggerStore.getState().payment.basket.paymentStatePolling.keepPolling).toBe(false);
expect(triggerStore.getState().payment.basket.paymentState).toBe(null);
});
});
});
});

0 comments on commit 24bde53

Please sign in to comment.