Skip to content

Commit bf74b3e

Browse files
author
Ray Schamp
committed
Fix HashParserHOC tests
1 parent 91028a0 commit bf74b3e

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

src/lib/hash-parser-hoc.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ const HashParserHOC = function (WrappedComponent) {
7979
const mapDispatchToProps = dispatch => ({
8080
setProjectId: projectId => dispatch(setProjectId(projectId))
8181
});
82+
const mergeProps = (stateProps, dispatchProps, ownProps) => Object.assign(
83+
{}, stateProps, dispatchProps, ownProps
84+
);
8285
return connect(
8386
mapStateToProps,
84-
mapDispatchToProps
87+
mapDispatchToProps,
88+
mergeProps
8589
)(HashParserComponent);
8690
};
8791

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,78 @@
11
import React from 'react';
2-
import HashParserHOC from '../../../src/lib/hash-parser-hoc.jsx';
2+
import configureStore from 'redux-mock-store';
33
import {mount} from 'enzyme';
44

5+
import HashParserHOC from '../../../src/lib/hash-parser-hoc.jsx';
6+
57
jest.mock('react-ga');
68

79
describe('HashParserHOC', () => {
10+
const mockStore = configureStore();
11+
let store;
12+
13+
beforeEach(() => {
14+
store = mockStore({
15+
scratchGui: {
16+
projectState: {}
17+
}
18+
});
19+
});
20+
821
test('when there is a hash, it passes the hash as projectId', () => {
922
const Component = ({projectId}) => <div>{projectId}</div>;
1023
const WrappedComponent = HashParserHOC(Component);
1124
window.location.hash = '#1234567';
12-
const mounted = mount(<WrappedComponent />);
13-
expect(mounted.state().projectId).toEqual('1234567');
25+
const mockSetProjectIdFunc = jest.fn();
26+
mount(
27+
<WrappedComponent
28+
setProjectId={mockSetProjectIdFunc}
29+
store={store}
30+
/>
31+
);
32+
expect(mockSetProjectIdFunc.mock.calls[0][0]).toBe('1234567');
1433
});
1534

1635
test('when there is no hash, it passes 0 as the projectId', () => {
1736
const Component = ({projectId}) => <div>{projectId}</div>;
1837
const WrappedComponent = HashParserHOC(Component);
1938
window.location.hash = '';
20-
const mounted = mount(<WrappedComponent />);
21-
expect(mounted.state().projectId).toEqual(0);
39+
const mockSetProjectIdFunc = jest.fn();
40+
mount(
41+
<WrappedComponent
42+
setProjectId={mockSetProjectIdFunc}
43+
store={store}
44+
/>
45+
);
46+
expect(mockSetProjectIdFunc.mock.calls[0][0]).toBe(0);
2247
});
2348

2449
test('when the hash is not a number, it passes 0 as projectId', () => {
2550
const Component = ({projectId}) => <div>{projectId}</div>;
2651
const WrappedComponent = HashParserHOC(Component);
2752
window.location.hash = '#winning';
28-
const mounted = mount(<WrappedComponent />);
29-
expect(mounted.state().projectId).toEqual(0);
53+
const mockSetProjectIdFunc = jest.fn();
54+
mount(
55+
<WrappedComponent
56+
setProjectId={mockSetProjectIdFunc}
57+
store={store}
58+
/>
59+
);
60+
expect(mockSetProjectIdFunc.mock.calls[0][0]).toBe(0);
3061
});
3162

3263
test('when hash change happens, the projectId state is changed', () => {
3364
const Component = ({projectId}) => <div>{projectId}</div>;
3465
const WrappedComponent = HashParserHOC(Component);
3566
window.location.hash = '';
36-
const mounted = mount(<WrappedComponent />);
37-
expect(mounted.state().projectId).toEqual(0);
67+
const mockSetProjectIdFunc = jest.fn();
68+
const mounted = mount(
69+
<WrappedComponent
70+
setProjectId={mockSetProjectIdFunc}
71+
store={store}
72+
/>
73+
);
3874
window.location.hash = '#1234567';
39-
mounted.instance().handleHashChange();
40-
expect(mounted.state().projectId).toEqual('1234567');
75+
mounted.childAt(0).instance().handleHashChange();
76+
expect(mockSetProjectIdFunc.mock.calls.length).toBe(2);
4177
});
4278
});

0 commit comments

Comments
 (0)