Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #### Store setup
  2. ```js
  3. import { createStore } from 'redux';
  4. const storeFactory = (stateOverrides) => createStore(rootReducer, _.merge({}, initialState, stateOverrides), middleware);
  5. ```
  6.  
  7. #### Wait for API calls to complete
  8. Listen to store events.
  9. ```js
  10. const waitForAction = (targetStore, conditionFn, timeout = 2000) =>
  11. new Promise((resolve, reject) => {
  12. const unsubscribe = targetStore.subscribe(() => {
  13. const state = targetStore.getState();
  14. if (conditionFn(state)) {
  15. unsubscribe();
  16. resolve(state);
  17. }
  18. setTimeout(() => {
  19. unsubscribe();
  20. reject(`No action found after ${timeout / 1000}sec`);
  21. }, timeout);
  22. });
  23. });
  24. ```
  25.  
  26. #### Component setup for testing
  27. ```js
  28. import { Provider } from 'react-redux';
  29. import { mount } from 'enzyme';
  30. const setup = (Component, extraProps = {}, state = {}, renderer = mount) => {
  31. const store = storeFactory(state);
  32. const wrapper = renderer(
  33. <Provider store={store}>
  34. <Component {...extraProps} />
  35. </Provider>
  36. );
  37.  
  38. return {
  39. wrapper,
  40. store,
  41. };
  42. };
  43. ```
  44.  
  45. #### Sample test
  46. ```js
  47. import MyComponent from '/path/to/component';
  48.  
  49. describe('<MyComponent/>', () => {
  50. before(() => {
  51. fetchMock.get('/some-service/api/v1/some-entity', {
  52. ok: true,
  53. content: [/*some data*/],
  54. });
  55. });
  56. it('render succesfully', async () => {
  57. const {wrapper, store} = setup(MyComponent, customProps, initialStateOverrides);
  58. // wait till the store's state is updated with items received from the mocked API
  59. await waitForAction(store, (state) => state.allItem.length > 0);
  60. const foundItems = wrapper.find('Items');
  61. expeect(foundItems.length).to.eql(3);
  62. // ... more expectations
  63. });
  64. });
  65. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement