Skip to content
Snippets Groups Projects
Commit cb29f9be authored by Jack Reed's avatar Jack Reed
Browse files

add tests to fetchManifest actions

parent 51111fd5
Branches
Tags
No related merge requests found
This diff is collapsed.
......@@ -32,6 +32,13 @@
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"jest": "^22.4.2",
"jest-fetch-mock": "^1.5.0",
"redux-mock-store": "^1.5.1",
"webpack": "3.10.0"
},
"jest": {
"setupFiles": [
"./setupJest.js"
]
}
}
// Setup Jest to mock fetch
import fetch from 'jest-fetch-mock'; // eslint-disable-line import/no-extraneous-dependencies
jest.setMock('node-fetch', fetch);
global.fetch = require('jest-fetch-mock'); // eslint-disable-line import/no-extraneous-dependencies
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as actions from '../../actions/index';
import ActionTypes from '../../action-types';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('actions', () => {
describe('addWindow', () => {
......@@ -80,4 +85,43 @@ describe('actions', () => {
expect(actions.receiveManifest(id, json)).toEqual(expectedAction);
});
});
describe('fetchManifest', () => {
let store = null;
beforeEach(() => {
store = mockStore({});
});
describe('success response', () => {
beforeEach(() => {
fetch.mockResponseOnce(JSON.stringify({ data: '12345' })); // eslint-disable-line no-undef
});
it('dispatches the REQUEST_MANIFEST action', () => {
store.dispatch(actions.fetchManifest('https://purl.stanford.edu/sn904cj3429/iiif/manifest'));
expect(store.getActions()).toEqual([
{ manifestId: 'https://purl.stanford.edu/sn904cj3429/iiif/manifest', type: 'REQUEST_MANIFEST' },
]);
});
it('dispatches the REQUEST_MANIFEST and then RECEIVE_MANIFEST', () => {
store.dispatch(actions.fetchManifest('https://purl.stanford.edu/sn904cj3429/iiif/manifest'))
.then(() => {
const expectedActions = store.getActions();
expect(expectedActions).toEqual([
{ manifestId: 'https://purl.stanford.edu/sn904cj3429/iiif/manifest', type: 'REQUEST_MANIFEST' },
{ manifestId: 'https://purl.stanford.edu/sn904cj3429/iiif/manifest', manifestJson: { data: '12345' }, type: 'RECEIVE_MANIFEST' },
]);
});
});
});
describe('error response', () => {
it('dispatches the REQUEST_MANIFEST and then RECEIVE_MANIFEST', () => {
store.dispatch(actions.fetchManifest('https://purl.stanford.edu/sn904cj3429/iiif/manifest'))
.then(() => {
const expectedActions = store.getActions();
expect(expectedActions).toEqual([
{ manifestId: 'https://purl.stanford.edu/sn904cj3429/iiif/manifest', type: 'REQUEST_MANIFEST' },
{ manifestId: 'https://purl.stanford.edu/sn904cj3429/iiif/manifest', error: new Error('invalid json response body at undefined reason: Unexpected end of JSON input'), type: 'RECEIVE_MANIFEST_FAILURE' },
]);
});
});
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment