Created
October 31, 2018 16:30
-
-
Save felixmeziere/9023c4967c10821e3864a621474ae648 to your computer and use it in GitHub Desktop.
Sagas test v1
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 { | |
| addToBasketRequest, | |
| addToBasketSuccess, | |
| addToBasketFailure, | |
| removeOverlay, | |
| refreshNumberBasketItemsRequest, | |
| refreshNumberBasketItemsSuccess, | |
| } from 'hoc/containers/Config/actions'; | |
| import rootSaga from '..'; | |
| import { mockUserStateResponse } from 'mockData'; | |
| import configReducer, { initialState as configInitialState } from 'hoc/containers/Config/reducer'; | |
| import { MAGENTO_ADDRESS } from '@env'; | |
| import { call } from 'redux-saga/effects'; | |
| import { delay } from 'redux-saga'; | |
| import { expectSaga } from 'redux-saga-test-plan'; | |
| import { throwError } from 'redux-saga-test-plan/providers'; | |
| import * as matchers from 'redux-saga-test-plan/matchers'; | |
| import { Vibration } from 'react-native'; | |
| jest.mock('react-native-cookies', () => {}); | |
| const mockSuccessFetch = jest.fn(); | |
| global.fetch = mockSuccessFetch; | |
| const sku = 'BEDROS008BLU-UK'; | |
| /* eslint-disable max-lines-per-function */ | |
| describe('addToBasketRequestSaga', () => { | |
| it('should request an item be added to the basket and update basketQuantity', () => { | |
| const finalState = { ...configInitialState }; | |
| return ( | |
| expectSaga(rootSaga) | |
| // Setup mocks | |
| .provide([ | |
| [call(global.fetch, `${MAGENTO_ADDRESS}/checkout/cart/add/product/${sku}`)], | |
| [matchers.call.fn(Vibration.vibrate)], | |
| [matchers.call.fn(delay)], | |
| [ | |
| call(global.fetch, `${MAGENTO_ADDRESS}/ajaxcalls/customerState`), | |
| { json: () => mockUserStateResponse }, | |
| ], | |
| ]) | |
| // Setup reducer with initial state | |
| .withReducer(configReducer, configInitialState) | |
| // Dispatch initial action | |
| .dispatch(addToBasketRequest(sku)) | |
| // Check that expected stuff has happened (order doesn't matter) | |
| .call(global.fetch, `${MAGENTO_ADDRESS}/checkout/cart/add/product/${sku}`) | |
| .put(addToBasketSuccess()) | |
| .call(Vibration.vibrate, 1000) | |
| .call(delay, 1000) | |
| .put(removeOverlay()) | |
| .put(refreshNumberBasketItemsRequest()) | |
| .call(global.fetch, `${MAGENTO_ADDRESS}/ajaxcalls/customerState`) | |
| .put(refreshNumberBasketItemsSuccess(1)) | |
| // Check final state | |
| .hasFinalState(finalState) | |
| .run() | |
| ); | |
| }); | |
| it('should request an item be added to the basket and deal with failure', () => | |
| expectSaga(rootSaga) | |
| // Setup mocks | |
| .provide([ | |
| [ | |
| call(global.fetch, `${MAGENTO_ADDRESS}/checkout/cart/add/product/${sku}`), | |
| throwError('400'), | |
| ], | |
| [matchers.call.fn(Vibration.vibrate)], | |
| [matchers.call.fn(delay)], | |
| ]) | |
| // Setup reducer with initial state | |
| .withReducer(configReducer, configInitialState) | |
| // Dispatch initial action | |
| .dispatch(addToBasketRequest(sku)) | |
| // Check that expected stuff has happened (order doesn't matter) | |
| .call(global.fetch, `${MAGENTO_ADDRESS}/checkout/cart/add/product/${sku}`) | |
| .put(addToBasketFailure()) | |
| .call(delay, 1000) | |
| .put(removeOverlay()) | |
| // Check final state | |
| .hasFinalState(configInitialState) | |
| .run()); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment