Skip to content
Snippets Groups Projects
Commit bf74b3ef authored by Ray Schamp's avatar Ray Schamp
Browse files

Fix HashParserHOC tests

parent 91028a0a
Branches
Tags
No related merge requests found
......@@ -79,9 +79,13 @@ const HashParserHOC = function (WrappedComponent) {
const mapDispatchToProps = dispatch => ({
setProjectId: projectId => dispatch(setProjectId(projectId))
});
const mergeProps = (stateProps, dispatchProps, ownProps) => Object.assign(
{}, stateProps, dispatchProps, ownProps
);
return connect(
mapStateToProps,
mapDispatchToProps
mapDispatchToProps,
mergeProps
)(HashParserComponent);
};
......
import React from 'react';
import HashParserHOC from '../../../src/lib/hash-parser-hoc.jsx';
import configureStore from 'redux-mock-store';
import {mount} from 'enzyme';
import HashParserHOC from '../../../src/lib/hash-parser-hoc.jsx';
jest.mock('react-ga');
describe('HashParserHOC', () => {
const mockStore = configureStore();
let store;
beforeEach(() => {
store = mockStore({
scratchGui: {
projectState: {}
}
});
});
test('when there is a hash, it passes the hash as projectId', () => {
const Component = ({projectId}) => <div>{projectId}</div>;
const WrappedComponent = HashParserHOC(Component);
window.location.hash = '#1234567';
const mounted = mount(<WrappedComponent />);
expect(mounted.state().projectId).toEqual('1234567');
const mockSetProjectIdFunc = jest.fn();
mount(
<WrappedComponent
setProjectId={mockSetProjectIdFunc}
store={store}
/>
);
expect(mockSetProjectIdFunc.mock.calls[0][0]).toBe('1234567');
});
test('when there is no hash, it passes 0 as the projectId', () => {
const Component = ({projectId}) => <div>{projectId}</div>;
const WrappedComponent = HashParserHOC(Component);
window.location.hash = '';
const mounted = mount(<WrappedComponent />);
expect(mounted.state().projectId).toEqual(0);
const mockSetProjectIdFunc = jest.fn();
mount(
<WrappedComponent
setProjectId={mockSetProjectIdFunc}
store={store}
/>
);
expect(mockSetProjectIdFunc.mock.calls[0][0]).toBe(0);
});
test('when the hash is not a number, it passes 0 as projectId', () => {
const Component = ({projectId}) => <div>{projectId}</div>;
const WrappedComponent = HashParserHOC(Component);
window.location.hash = '#winning';
const mounted = mount(<WrappedComponent />);
expect(mounted.state().projectId).toEqual(0);
const mockSetProjectIdFunc = jest.fn();
mount(
<WrappedComponent
setProjectId={mockSetProjectIdFunc}
store={store}
/>
);
expect(mockSetProjectIdFunc.mock.calls[0][0]).toBe(0);
});
test('when hash change happens, the projectId state is changed', () => {
const Component = ({projectId}) => <div>{projectId}</div>;
const WrappedComponent = HashParserHOC(Component);
window.location.hash = '';
const mounted = mount(<WrappedComponent />);
expect(mounted.state().projectId).toEqual(0);
const mockSetProjectIdFunc = jest.fn();
const mounted = mount(
<WrappedComponent
setProjectId={mockSetProjectIdFunc}
store={store}
/>
);
window.location.hash = '#1234567';
mounted.instance().handleHashChange();
expect(mounted.state().projectId).toEqual('1234567');
mounted.childAt(0).instance().handleHashChange();
expect(mockSetProjectIdFunc.mock.calls.length).toBe(2);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment