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

Fix tests

Previously we tested the ProjectLoader helper object, but now all loading is confined to storage. Unfortunately I wasn't able to get enzyme to wait for the storage promise to resolve within componentDidUpdate, so I can't test the actual results of storage.load, just what it was called with.
parent 42cf12e3
Branches
Tags
No related merge requests found
import React from 'react';
import ProjectLoaderHOC, {ProjectLoader} from '../../../src/lib/project-loader-hoc.jsx';
import ProjectLoaderHOC from '../../../src/lib/project-loader-hoc.jsx';
import storage from '../../../src/lib/storage';
import {mount} from 'enzyme';
describe('ProjectLoaderHOC', () => {
......@@ -7,34 +8,38 @@ describe('ProjectLoaderHOC', () => {
const Component = ({projectData}) => <div>{projectData}</div>;
const WrappedComponent = ProjectLoaderHOC(Component);
window.location.hash = '#winning';
ProjectLoader.load = jest.fn((id, cb) => cb(null, null));
storage.load = jest.fn(() => Promise.resolve(null));
const mounted = mount(<WrappedComponent />);
ProjectLoader.load.mockRestore();
storage.load.mockRestore();
window.location.hash = '';
expect(mounted.find('div').exists()).toEqual(false);
const mountedDiv = mounted.find('div');
expect(mountedDiv.exists()).toEqual(false);
});
test('when there is no hash, it loads the default project', () => {
const Component = ({projectData}) => <div>{projectData}</div>;
const WrappedComponent = ProjectLoaderHOC(Component);
window.location.hash = '';
storage.load = jest.fn((type, id) => Promise.resolve(id));
const mounted = mount(<WrappedComponent />);
expect(mounted.find('div').text()).toEqual(JSON.stringify(ProjectLoader.DEFAULT_PROJECT_DATA));
expect(mounted.state().projectId).toEqual(0);
expect(storage.load).toHaveBeenCalledWith(
storage.AssetType.Project, 0, storage.DataFormat.JSON
);
storage.load.mockRestore();
});
test('when there is a hash, it tries to load that project', () => {
const Component = ({projectData}) => <div>{projectData}</div>;
const WrappedComponent = ProjectLoaderHOC(Component);
window.location.hash = '#winning';
ProjectLoader.load = jest.fn((id, cb) => cb(null, id));
storage.load = jest.fn((type, id) => Promise.resolve({data: id}));
const mounted = mount(<WrappedComponent />);
mounted.update();
ProjectLoader.load.mockRestore();
window.location.hash = '';
expect(mounted
.find('div')
.text()
).toEqual('winning');
expect(mounted.state().projectId).toEqual('winning');
expect(storage.load).toHaveBeenLastCalledWith(
storage.AssetType.Project, 'winning', storage.DataFormat.JSON
);
storage.load.mockRestore();
});
test('when hash change happens, the project data state is changed', () => {
......@@ -42,10 +47,11 @@ describe('ProjectLoaderHOC', () => {
const WrappedComponent = ProjectLoaderHOC(Component);
window.location.hash = '';
const mounted = mount(<WrappedComponent />);
const before = mounted.find('div').text();
ProjectLoader.load = jest.fn((id, cb) => cb(null, id));
window.location.hash = `#winning`;
expect(mounted.state().projectId).toEqual(0);
storage.load = jest.fn((type, id) => Promise.resolve({data: id}));
window.location.hash = '#winning';
mounted.instance().updateProject();
expect(mounted.find('div').text()).not.toEqual(before);
expect(mounted.state().projectId).toEqual('winning');
storage.load.mockRestore();
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment