project-fetcher-hoc.test.jsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React from 'react';
  2. import configureStore from 'redux-mock-store';
  3. import {mountWithIntl} from '../../helpers/intl-helpers.jsx';
  4. import ProjectFetcherHOC from '../../../src/lib/project-fetcher-hoc.jsx';
  5. import storage from '../../../src/lib/storage';
  6. import {LoadingState} from '../../../src/reducers/project-state';
  7. jest.mock('react-ga');
  8. describe('ProjectFetcherHOC', () => {
  9. const mockStore = configureStore();
  10. let store;
  11. beforeEach(() => {
  12. store = mockStore({
  13. scratchGui: {
  14. projectState: {}
  15. }
  16. });
  17. });
  18. test('when there is an id, it tries to update the store with that id', () => {
  19. const Component = ({projectId}) => <div>{projectId}</div>;
  20. const WrappedComponent = ProjectFetcherHOC(Component);
  21. const mockSetProjectIdFunc = jest.fn();
  22. mountWithIntl(
  23. <WrappedComponent
  24. projectId="100"
  25. setProjectId={mockSetProjectIdFunc}
  26. store={store}
  27. />
  28. );
  29. expect(mockSetProjectIdFunc.mock.calls[0][0]).toBe('100');
  30. });
  31. test('when there is a reduxProjectId and isFetchingWithProjectId is true, it loads the project', () => {
  32. const mockedOnFetchedProject = jest.fn();
  33. const originalLoad = storage.load;
  34. storage.load = jest.fn((type, id) => Promise.resolve({data: id}));
  35. const Component = ({projectId}) => <div>{projectId}</div>;
  36. const WrappedComponent = ProjectFetcherHOC(Component);
  37. const mounted = mountWithIntl(
  38. <WrappedComponent
  39. store={store}
  40. onFetchedProjectData={mockedOnFetchedProject}
  41. />
  42. );
  43. mounted.setProps({
  44. reduxProjectId: '100',
  45. isFetchingWithId: true,
  46. loadingState: LoadingState.FETCHING_WITH_ID
  47. });
  48. expect(storage.load).toHaveBeenLastCalledWith(
  49. storage.AssetType.Project, '100', storage.DataFormat.JSON
  50. );
  51. storage.load = originalLoad;
  52. // nextTick needed since storage.load is async, and onFetchedProject is called in its then()
  53. process.nextTick(
  54. () => expect(mockedOnFetchedProject)
  55. .toHaveBeenLastCalledWith('100', LoadingState.FETCHING_WITH_ID)
  56. );
  57. });
  58. });