Skip to content
Snippets Groups Projects
Commit 37f98f60 authored by chrisgarrity's avatar chrisgarrity
Browse files

Fix up comments

- better split of hash-parser/project-loader unit tests.
parent 0fa62fc0
No related branches found
No related tags found
No related merge requests found
import React from 'react'; import React from 'react';
import bindAll from 'lodash.bindall';
/* Higher Order Component to get the project id from location.hash /* Higher Order Component to get the project id from location.hash
* @param {React.Component} WrappedComponent component to receive projectData prop * @param {React.Component} WrappedComponent component to receive projectData prop
...@@ -8,24 +9,22 @@ const HashParserHOC = function (WrappedComponent) { ...@@ -8,24 +9,22 @@ const HashParserHOC = function (WrappedComponent) {
class HashParserComponent extends React.Component { class HashParserComponent extends React.Component {
constructor (props) { constructor (props) {
super(props); super(props);
this.fetchProjectId = this.fetchProjectId.bind(this); bindAll(this, [
this.updateProject = this.updateProject.bind(this); 'handleHashChange'
]);
this.state = { this.state = {
projectId: null projectId: null
}; };
} }
componentDidMount () { componentDidMount () {
window.addEventListener('hashchange', this.updateProject); window.addEventListener('hashchange', this.handleHashChange);
this.updateProject(); this.handleHashChange();
} }
componentWillUnmount () { componentWillUnmount () {
window.removeEventListener('hashchange', this.updateProject); window.removeEventListener('hashchange', this.handleHashChange);
} }
fetchProjectId () { handleHashChange () {
return window.location.hash.substring(1); let projectId = window.location.hash.substring(1);
}
updateProject () {
let projectId = this.fetchProjectId();
if (projectId !== this.state.projectId) { if (projectId !== this.state.projectId) {
if (projectId.length < 1) projectId = 0; if (projectId.length < 1) projectId = 0;
this.setState({projectId: projectId}); this.setState({projectId: projectId});
......
import React from 'react'; import React from 'react';
import ProjectLoaderHOC from '../../../src/lib/project-loader-hoc.jsx';
import HashParserHOC from '../../../src/lib/hash-parser-hoc.jsx'; import HashParserHOC from '../../../src/lib/hash-parser-hoc.jsx';
import storage from '../../../src/lib/storage';
import {mount} from 'enzyme'; import {mount} from 'enzyme';
jest.mock('react-ga'); jest.mock('react-ga');
describe('Hash/ProjectLoaderHOC', () => { describe('HashParserHOC', () => {
test('when there is no project data, it renders null', () => { test('when there is a hash, it passes the hash as projectId', () => {
const Component = ({projectData}) => <div>{projectData}</div>; const Component = ({projectId}) => <div>{projectId}</div>;
const WrappedComponent = HashParserHOC(ProjectLoaderHOC(Component)); const WrappedComponent = HashParserHOC(Component);
window.location.hash = '#winning'; window.location.hash = '#winning';
const originalLoad = storage.load;
storage.load = jest.fn(() => Promise.resolve(null));
const mounted = mount(<WrappedComponent />); const mounted = mount(<WrappedComponent />);
storage.load = originalLoad; expect(mounted.state().projectId).toEqual('winning');
window.location.hash = '';
const mountedDiv = mounted.find('div');
expect(mountedDiv.exists()).toEqual(false);
}); });
test('when there is no hash, it loads the default project', () => { test('when there is no hash, it passes 0 as the projectId', () => {
const Component = ({projectData}) => <div>{projectData}</div>; const Component = ({projectId}) => <div>{projectId}</div>;
const WrappedComponent = HashParserHOC(ProjectLoaderHOC(Component)); const WrappedComponent = HashParserHOC(Component);
window.location.hash = ''; window.location.hash = '';
const originalLoad = storage.load;
storage.load = jest.fn((type, id) => Promise.resolve(id));
const mounted = mount(<WrappedComponent />); const mounted = mount(<WrappedComponent />);
expect(mounted.state().projectId).toEqual(0); expect(mounted.state().projectId).toEqual(0);
expect(storage.load).toHaveBeenCalledWith(
storage.AssetType.Project, 0, storage.DataFormat.JSON
);
storage.load = originalLoad;
});
test('when there is a hash, it tries to load that project', () => {
const Component = ({projectData}) => <div>{projectData}</div>;
const WrappedComponent = HashParserHOC(ProjectLoaderHOC(Component));
window.location.hash = '#winning';
const originalLoad = storage.load;
storage.load = jest.fn((type, id) => Promise.resolve({data: id}));
const mounted = mount(<WrappedComponent />);
expect(mounted.state().projectId).toEqual('winning');
expect(storage.load).toHaveBeenLastCalledWith(
storage.AssetType.Project, 'winning', storage.DataFormat.JSON
);
storage.load = originalLoad;
}); });
test('when hash change happens, the project data state is changed', () => { test('when hash change happens, the projectId state is changed', () => {
const Component = ({projectData}) => <div>{projectData}</div>; const Component = ({projectId}) => <div>{projectId}</div>;
const WrappedComponent = HashParserHOC(ProjectLoaderHOC(Component)); const WrappedComponent = HashParserHOC(Component);
window.location.hash = ''; window.location.hash = '';
const mounted = mount(<WrappedComponent />); const mounted = mount(<WrappedComponent />);
expect(mounted.state().projectId).toEqual(0); expect(mounted.state().projectId).toEqual(0);
const originalLoad = storage.load;
storage.load = jest.fn((type, id) => Promise.resolve({data: id}));
window.location.hash = '#winning'; window.location.hash = '#winning';
mounted.instance().updateProject(); mounted.instance().handleHashChange();
expect(mounted.state().projectId).toEqual('winning'); expect(mounted.state().projectId).toEqual('winning');
storage.load = originalLoad;
}); });
}); });
...@@ -33,4 +33,15 @@ describe('ProjectLoaderHOC', () => { ...@@ -33,4 +33,15 @@ describe('ProjectLoaderHOC', () => {
storage.load = originalLoad; storage.load = originalLoad;
}); });
test('when there is no project data, it renders null', () => {
const Component = ({projectData}) => <div>{projectData}</div>;
const WrappedComponent = ProjectLoaderHOC(Component);
const originalLoad = storage.load;
storage.load = jest.fn(() => Promise.resolve(null));
const mounted = mount(<WrappedComponent />);
storage.load = originalLoad;
const mountedDiv = mounted.find('div');
expect(mountedDiv.exists()).toEqual(false);
});
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment