Skip to content

Commit

Permalink
Show an error message when the page fails to load. fixes edgi-govdata…
Browse files Browse the repository at this point in the history
  • Loading branch information
steryereo committed Mar 25, 2020
1 parent 01495ba commit 0f0954e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
32 changes: 26 additions & 6 deletions src/components/__tests__/page-details.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ describe('page-details', () => {
simplePage.versions.forEach(version => {
version.capture_time = new Date(version.capture_time);
});
const match = { params: { pageId: simplePage.uuid }};
const createMockApi = () => {
return Object.assign(Object.create(WebMonitoringDb.prototype), {
const match = {params: {pageId: simplePage.uuid}};

const createMockApi = overrides => Object.assign(
Object.create(WebMonitoringDb.prototype),
{
getPage: jest.fn().mockResolvedValue(simplePage),
getVersions: jest.fn().mockResolvedValue(simplePage.versions)
});
};
getVersions: jest.fn().mockResolvedValue(simplePage.versions),
},
overrides
);

it('can render', () => {
const mockApi = createMockApi();
Expand Down Expand Up @@ -49,4 +52,21 @@ describe('page-details', () => {
pageDetails.unmount();
expect(document.title).toBe('Scanner');
});

it('shows an error message if api.getPage throws an error', async () => {
const error = new Error('Page does not exist');
const mockApi = createMockApi({getPage: jest.fn().mockRejectedValue(error)});

const pageDetails = shallow(
<PageDetails
match={match}
/>,
{context: {api: mockApi}}
);

await expect(mockApi.getPage.mock.results[0].value).rejects.toThrow();
await Promise.resolve(); // wait a tick for the error message to render

expect(pageDetails.find('[className*="danger"]').text()).toBe(error.message);
});
});
18 changes: 10 additions & 8 deletions src/components/page-details/page-details.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from 'prop-types';
import React from 'react';
import { Link, Redirect } from 'react-router-dom';
import {Link, Redirect} from 'react-router-dom';
import WebMonitoringDb from '../../services/web-monitoring-db';
import ChangeView from '../change-view/change-view';
import Loading from '../loading';
Expand Down Expand Up @@ -28,16 +28,18 @@ export default class PageDetails extends React.Component {
// Clear existing content when switching pages
if (state.page && state.page.uuid !== props.match.params.pageId) {
return {
page: null,
error: null
page: null
};
}
return null;
}

constructor(props) {
super(props);
this.state = { page: null };
this.state = {
page: null,
error: null
};
this._annotateChange = this._annotateChange.bind(this);
this._navigateToChange = this._navigateToChange.bind(this);
}
Expand Down Expand Up @@ -244,7 +246,7 @@ export default class PageDetails extends React.Component {
}
}

return { from, to, shouldRedirect };
return {from, to, shouldRedirect};
}

_loadPage(pageId) {
Expand All @@ -263,14 +265,14 @@ export default class PageDetails extends React.Component {
this._loadVersions(page, cutoffDate, '')
.then(versions => {
page.versions = versions;
this.setState({ page });
this.setState({page});
});
})
.catch(error => this.setState({ error }));
.catch(error => this.setState({error}));
}

_loadVersions(page, dateFrom, dateTo) {
const capture_time = { 'capture_time': `${dateFrom}..${dateTo}` };
const capture_time = {'capture_time': `${dateFrom}..${dateTo}`};
return this.context.api.getVersions(page.uuid, capture_time, Infinity);
}

Expand Down

0 comments on commit 0f0954e

Please sign in to comment.