Created
October 31, 2018 16:31
-
-
Save felixmeziere/bad7c6b8b01351efff189752545eb2c6 to your computer and use it in GitHub Desktop.
Sagas test v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* eslint-disable max-lines */ | |
| import SagaTester from 'redux-saga-tester'; | |
| import { | |
| addToBasketRequest, | |
| addToBasketSuccess, | |
| addToBasketFailure, | |
| removeOverlay, | |
| refreshNumberBasketItemsRequest, | |
| refreshNumberBasketItemsSuccess, | |
| } from 'hoc/containers/Config/actions'; | |
| import { REFRESH_NUMBER_OF_ITEMS, REMOVE_OVERLAY } from 'hoc/containers/Config/constants'; | |
| import rootSaga from '..'; | |
| import { mockUserStateResponse } from 'mockData'; | |
| import configReducer, { initialState } from 'hoc/containers/Config/reducer'; | |
| import { MAGENTO_ADDRESS } from '@env'; | |
| jest.mock('react-native-cookies', () => ({ | |
| addEventListener: jest.fn(), | |
| removeEventListener: jest.fn(), | |
| openURL: jest.fn(), | |
| canOpenURL: jest.fn(), | |
| getInitialURL: jest.fn(), | |
| get: () => Promise.resolve(null), | |
| getFromResponse: () => ({ frontend: 'blablabla' }), | |
| })); | |
| jest.mock('services/getLocale', () => ({ | |
| __esModule: true, | |
| default: () => 'fr-FR', | |
| })); | |
| jest.mock('react-native-permissions', () => ({ | |
| __esModule: true, | |
| default: { | |
| request: mockValueToBeOutput => mockValueToBeOutput, | |
| }, | |
| })); | |
| jest.mock('react-native', () => ({ | |
| Vibration: { vibrate: jest.fn() }, | |
| })); | |
| const mockSuccessFetch = jest | |
| .fn() | |
| .mockImplementationOnce(() => | |
| Promise.resolve({ | |
| json: () => ({}), | |
| }), | |
| ) | |
| .mockImplementationOnce(() => | |
| Promise.resolve({ | |
| json: () => mockUserStateResponse, | |
| }), | |
| ); | |
| const mockFailureFetch = jest.fn().mockImplementationOnce(() => Promise.reject()); | |
| /* eslint-disable max-lines-per-function */ | |
| describe('addToBasketRequestSaga', () => { | |
| let sagaTester; | |
| global.fetch = mockSuccessFetch; | |
| it('should request an item be added to the basket and update basketQuantity', async () => { | |
| // Configure black box | |
| sagaTester = new SagaTester({ | |
| initialState: { config: initialState }, | |
| reducers: { config: configReducer }, | |
| }); | |
| sagaTester.start(rootSaga); | |
| // Check initial state | |
| expect(sagaTester.getState().config.basketQuantity).toEqual(0); | |
| // Run the saga | |
| sagaTester.dispatch(addToBasketRequest('BEDROS008BLU-UK')); | |
| await sagaTester.waitFor(REFRESH_NUMBER_OF_ITEMS.SUCCESS); | |
| // Check expected actions have been dispatched | |
| const calledActions = sagaTester.getCalledActions(); | |
| expect(calledActions[1]).toEqual(addToBasketSuccess()); | |
| expect(calledActions[2]).toEqual(removeOverlay()); | |
| expect(calledActions[3]).toEqual(refreshNumberBasketItemsRequest()); | |
| expect(calledActions[4]).toEqual(refreshNumberBasketItemsSuccess(1)); | |
| // Check final state | |
| expect(sagaTester.getState().config.basketQuantity).toEqual(1); | |
| // check endpoint with current sku is called | |
| expect(mockSuccessFetch.mock.calls[0][0]).toEqual( | |
| `${MAGENTO_ADDRESS}/checkout/cart/add/product/BEDROS008BLU-UK`, | |
| ); | |
| }); | |
| it('should request an item be added to the basket and deal with failure', async () => { | |
| global.fetch = mockFailureFetch; | |
| // Configure black box | |
| sagaTester = new SagaTester({ | |
| initialState: { config: initialState }, | |
| reducers: { config: configReducer }, | |
| }); | |
| sagaTester.start(rootSaga); | |
| // Check initial state | |
| expect(sagaTester.getState().config.basketQuantity).toEqual(0); | |
| // Run the saga | |
| sagaTester.dispatch(addToBasketRequest('CHAMGT001YEL-UK')); | |
| await sagaTester.waitFor(REMOVE_OVERLAY); | |
| // Check expected actions have been dispatched | |
| const calledActions = sagaTester.getCalledActions(); | |
| expect(calledActions[1]).toEqual(addToBasketFailure()); | |
| expect(calledActions[2]).toEqual(removeOverlay()); | |
| // Check final state | |
| expect(sagaTester.getState().config.basketQuantity).toEqual(0); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment