Skip to content

Commit 5180a77

Browse files
author
Ray Schamp
committed
Fix ProjectFetcherHOC tests
1 parent 35435bd commit 5180a77

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/lib/project-fetcher-hoc.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,13 @@ const ProjectFetcherHOC = function (WrappedComponent) {
126126
dispatch(onFetchedProjectData(projectData, loadingState)),
127127
setProjectId: projectId => dispatch(setProjectId(projectId))
128128
});
129+
const mergeProps = (stateProps, dispatchProps, ownProps) => Object.assign(
130+
{}, stateProps, dispatchProps, ownProps
131+
);
129132
return injectIntl(connect(
130133
mapStateToProps,
131-
mapDispatchToProps
134+
mapDispatchToProps,
135+
mergeProps
132136
)(ProjectFetcherComponent));
133137
};
134138

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import React from 'react';
22
import configureStore from 'redux-mock-store';
3+
4+
import {mountWithIntl} from '../../helpers/intl-helpers.jsx';
5+
36
import ProjectFetcherHOC from '../../../src/lib/project-fetcher-hoc.jsx';
47
import storage from '../../../src/lib/storage';
5-
import {mountWithIntl} from '../../helpers/intl-helpers.jsx';
8+
import {LoadingState} from '../../../src/reducers/project-state';
69

710
jest.mock('react-ga');
811

@@ -11,36 +14,41 @@ describe('ProjectFetcherHOC', () => {
1114
let store;
1215

1316
beforeEach(() => {
14-
store = mockStore({scratchGui: {}});
17+
store = mockStore({
18+
scratchGui: {
19+
projectState: {}
20+
}
21+
});
1522
});
1623

17-
test('when there is an id, it tries to load that project', () => {
18-
const Component = ({projectData}) => <div>{projectData}</div>;
24+
test('when there is an id, it tries to update the store with that id', () => {
25+
const Component = ({projectId}) => <div>{projectId}</div>;
1926
const WrappedComponent = ProjectFetcherHOC(Component);
20-
const originalLoad = storage.load;
21-
storage.load = jest.fn((type, id) => Promise.resolve({data: id}));
22-
const mounted = mountWithIntl(
27+
const mockSetProjectIdFunc = jest.fn();
28+
mountWithIntl(
2329
<WrappedComponent
2430
projectId="100"
31+
setProjectId={mockSetProjectIdFunc}
2532
store={store}
2633
/>
2734
);
28-
expect(mounted.props().projectId).toEqual('100');
29-
expect(storage.load).toHaveBeenLastCalledWith(
30-
storage.AssetType.Project, '100', storage.DataFormat.JSON
31-
);
32-
storage.load = originalLoad;
35+
expect(mockSetProjectIdFunc.mock.calls[0][0]).toBe('100');
3336
});
34-
35-
test('when there is no project data, it renders null', () => {
36-
const Component = ({projectData}) => <div>{projectData}</div>;
37-
const WrappedComponent = ProjectFetcherHOC(Component);
37+
test('when there is a reduxProjectId and isFetchingWithProjectId is true, it loads the project', () => {
3838
const originalLoad = storage.load;
39-
storage.load = jest.fn(() => Promise.resolve(null));
39+
storage.load = jest.fn((type, id) => Promise.resolve({data: id}));
40+
const Component = ({projectId}) => <div>{projectId}</div>;
41+
const WrappedComponent = ProjectFetcherHOC(Component);
4042
const mounted = mountWithIntl(<WrappedComponent store={store} />);
43+
mounted.setProps({
44+
reduxProjectId: '100',
45+
isFetchingWithId: true,
46+
loadingState: LoadingState.FETCHING_WITH_ID
47+
});
48+
mounted.update();
49+
expect(storage.load).toHaveBeenLastCalledWith(
50+
storage.AssetType.Project, '100', storage.DataFormat.JSON
51+
);
4152
storage.load = originalLoad;
42-
const mountedDiv = mounted.find('div');
43-
expect(mountedDiv.exists()).toEqual(false);
4453
});
45-
4654
});

0 commit comments

Comments
 (0)