Skip to content

Commit 59c83c1

Browse files
committed
Further enhancement of unit tests
New coverage thresholds set: Branches: 79.75%; Functions: 89.00%; Lines: 91.75%; Statements: 91.00%.
1 parent 7a2e5f9 commit 59c83c1

File tree

20 files changed

+890
-78
lines changed

20 files changed

+890
-78
lines changed

__tests__/client/index.jsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ window.ISTATE = 'Initial state of Redux store';
2424
/* Mock of browser-cookies */
2525

2626
let tokenV2;
27-
jest.setMock('browser-cookies', {
28-
erase: () => {},
27+
const mockCookies = {
28+
erase: jest.fn(),
2929
get: (name) => {
3030
switch (name) {
3131
case 'tcjwt': return tokenV2;
3232
default: return undefined;
3333
}
3434
},
3535
set: () => {},
36-
});
36+
};
37+
jest.setMock('browser-cookies', mockCookies);
3738

3839
/* Mock of react-redux module */
3940

@@ -81,11 +82,12 @@ jest.setMock('react-router-dom', {
8182
/* Mock of tc-accounts */
8283

8384
let tokenV3;
84-
jest.setMock('tc-accounts', {
85+
const mockTcAccounts = {
8586
configureConnector: () => undefined,
8687
decodeToken: () => 'Decoded user object',
8788
getFreshToken: () => Promise.resolve(tokenV3),
88-
});
89+
};
90+
jest.setMock('tc-accounts', mockTcAccounts);
8991

9092
/* Mock auth actions */
9193

@@ -170,5 +172,17 @@ describe('Properly starts with process.env.FRONT_ENV evaluating true', () => {
170172
});
171173
}),
172174
);
175+
176+
test('Clears auth cookies when authorization fails', () =>
177+
new Promise((done) => {
178+
mockTcAccounts.getFreshToken = () => Promise.reject('');
179+
require(MODULE);
180+
setImmediate(() => {
181+
expect(mockCookies.erase).toHaveBeenCalledWith('tcjwt');
182+
expect(mockCookies.erase).toHaveBeenCalledWith('tctV3');
183+
done();
184+
});
185+
}),
186+
);
173187
});
174188

__tests__/server/server.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
import _ from 'lodash';
22

3+
const MODULE = require.resolve('server/server');
4+
5+
jest.setMock('webpack', _.noop);
6+
jest.setMock('../../config/webpack/development', {
7+
output: {
8+
publicPath: '',
9+
},
10+
});
311
jest.setMock('../../src/server/renderer', _.noop);
412

5-
test('Should not throw', () => {
6-
expect(() => require('server/server')).not.toThrow();
13+
jest.setMock('webpack-dev-middleware', () =>
14+
(req, res, next) => next && next(),
15+
);
16+
17+
jest.setMock('webpack-hot-middleware', () =>
18+
(req, res, next) => next && next(),
19+
);
20+
21+
afterAll(() => {
22+
delete process.env.FRONT_END;
23+
process.env.NODE_ENV = 'test';
24+
});
25+
26+
beforeEach(() => {
27+
jest.resetModules();
28+
});
29+
30+
test('Throws when executed at front end', () => {
31+
process.env.FRONT_END = true;
32+
expect(() => require(MODULE)).toThrow();
33+
delete process.env.FRONT_END;
34+
});
35+
36+
test('Does not throw when executed at the back end', () => {
37+
expect(() => require(MODULE)).not.toThrow();
38+
});
39+
40+
test('Does not throw when executed at the back end in dev', () => {
41+
process.env.NODE_ENV = 'development';
42+
expect(() => require(MODULE)).not.toThrow();
43+
process.env.NODE_ENV = 'test';
744
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Snapshot match 1`] = `
4+
<div>
5+
<Routes />
6+
</div>
7+
`;
8+
9+
exports[`Snapshot match 2`] = `
10+
<div>
11+
<Routes />
12+
<DevTools />
13+
</div>
14+
`;
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import Button from 'components/Button';
22
import React from 'react';
3-
import ReactShallowRenderer from 'react-test-renderer/shallow';
3+
import Rnd from 'react-test-renderer/shallow';
4+
import TU from 'react-dom/test-utils';
45

5-
test('renders correctly', () => {
6-
const renderer = new ReactShallowRenderer();
7-
renderer.render(<Button />);
8-
const button = renderer.getRenderOutput();
6+
const rnd = new Rnd();
7+
8+
test('Snapshot match', () => {
9+
rnd.render(<Button />);
10+
const button = rnd.getRenderOutput();
911
expect(button).toMatchSnapshot();
1012
});
13+
14+
class ButtonClass extends React.Component {
15+
componentDidMount() {}
16+
17+
render() {
18+
return <Button {...this.props} />;
19+
}
20+
}
21+
22+
const mockOnClick = jest.fn();
23+
24+
const render = TU.renderIntoDocument((
25+
<ButtonClass
26+
onClick={mockOnClick}
27+
/>
28+
));
29+
30+
test('onClick', () => {
31+
const obj = TU.findRenderedDOMComponentWithClass(render, 'tc-btn');
32+
TU.Simulate.click(obj);
33+
expect(mockOnClick).toHaveBeenCalled();
34+
});

__tests__/shared/components/Modal.jsx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
import Modal from 'components/Modal';
22
import React from 'react';
3-
import Shallow from 'react-test-renderer/shallow';
3+
import Rnd from 'react-test-renderer/shallow';
4+
import TU from 'react-dom/test-utils';
45

5-
test('Matches shallow shapshot', () => {
6-
const renderer = new Shallow();
7-
renderer.render(<Modal />);
8-
expect(renderer.getRenderOutput()).toMatchSnapshot();
6+
test('Snapshot match', () => {
7+
const rnd = new Rnd();
8+
rnd.render(<Modal />);
9+
expect(rnd.getRenderOutput()).toMatchSnapshot();
10+
});
11+
12+
class ModalClass extends React.Component {
13+
componentDidMount() {}
14+
15+
render() {
16+
return <Modal {...this.props} />;
17+
}
18+
}
19+
20+
const mockOnCancel = jest.fn();
21+
22+
const render = TU.renderIntoDocument((
23+
<ModalClass
24+
onCancel={mockOnCancel}
25+
/>
26+
));
27+
28+
test('onCancel', () => {
29+
const obj = TU.findAllInRenderedTree(render, item =>
30+
item && item.className && item.className.match('bg-overlay'))[0];
31+
TU.Simulate.click(obj);
32+
expect(mockOnCancel).toHaveBeenCalled();
933
});
Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,72 @@
11
import React from 'react';
2-
import Renderer from 'react-test-renderer/shallow';
2+
import Rnd from 'react-test-renderer/shallow';
33
import ScreeningDetails from 'components/SubmissionManagement/ScreeningDetails';
44

5-
test('Matches shallow shapshot', () => {
6-
const renderer = new Renderer();
7-
renderer.render(<ScreeningDetails />);
8-
expect(renderer.getRenderOutput()).toMatchSnapshot();
5+
const rnd = new Rnd();
6+
7+
test('Snapshot match', () => {
8+
rnd.render((
9+
<ScreeningDetails
10+
screeningObject={{
11+
}}
12+
/>
13+
));
14+
expect(rnd.getRenderOutput()).toMatchSnapshot();
15+
16+
rnd.render((
17+
<ScreeningDetails
18+
screeningObject={{
19+
status: 'failed',
20+
}}
21+
/>
22+
));
23+
expect(rnd.getRenderOutput()).toMatchSnapshot();
24+
25+
rnd.render((
26+
<ScreeningDetails
27+
screeningObject={{
28+
status: 'failed',
29+
warnings: [{
30+
brief: 'Brief #0',
31+
details: 'Details #0',
32+
}],
33+
}}
34+
/>
35+
));
36+
expect(rnd.getRenderOutput()).toMatchSnapshot();
37+
38+
rnd.render((
39+
<ScreeningDetails
40+
screeningObject={{
41+
status: 'passed',
42+
}}
43+
/>
44+
));
45+
expect(rnd.getRenderOutput()).toMatchSnapshot();
46+
47+
rnd.render((
48+
<ScreeningDetails
49+
screeningObject={{
50+
status: 'passed',
51+
warnings: [{
52+
brief: 'Brief #0',
53+
details: 'Details #0',
54+
}],
55+
}}
56+
/>
57+
));
58+
expect(rnd.getRenderOutput()).toMatchSnapshot();
59+
60+
rnd.render((
61+
<ScreeningDetails
62+
screeningObject={{
63+
status: 'pending',
64+
warnings: [{
65+
brief: 'Brief #0',
66+
details: 'Details #0',
67+
}],
68+
}}
69+
/>
70+
));
71+
expect(rnd.getRenderOutput()).toMatchSnapshot();
972
});
Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,64 @@
11
import React from 'react';
2-
import Renderer from 'react-test-renderer/shallow';
2+
import Rnd from 'react-test-renderer/shallow';
33
import ScreeningStatus from 'components/SubmissionManagement/ScreeningStatus';
44

5-
test('Matches shallow shapshot', () => {
6-
const renderer = new Renderer();
7-
renderer.render((
5+
const rnd = new Rnd();
6+
7+
test('Snapshot match', () => {
8+
rnd.render((
9+
<ScreeningStatus
10+
screeningObject={{
11+
status: 'failed',
12+
}}
13+
submissionId="12345"
14+
/>
15+
));
16+
expect(rnd.getRenderOutput()).toMatchSnapshot();
17+
18+
rnd.render((
19+
<ScreeningStatus
20+
screeningObject={{
21+
status: 'failed',
22+
warnings: [],
23+
}}
24+
submissionId="12345"
25+
/>
26+
));
27+
expect(rnd.getRenderOutput()).toMatchSnapshot();
28+
29+
rnd.render((
30+
<ScreeningStatus
31+
screeningObject={{
32+
status: 'passed',
33+
}}
34+
submissionId="12345"
35+
/>
36+
));
37+
expect(rnd.getRenderOutput()).toMatchSnapshot();
38+
39+
rnd.render((
40+
<ScreeningStatus
41+
screeningObject={{
42+
status: 'passed',
43+
warnings: [],
44+
}}
45+
submissionId="12345"
46+
/>
47+
));
48+
expect(rnd.getRenderOutput()).toMatchSnapshot();
49+
50+
rnd.render((
51+
<ScreeningStatus
52+
screeningObject={{
53+
status: 'pending',
54+
warnings: [],
55+
}}
56+
submissionId="12345"
57+
/>
58+
));
59+
expect(rnd.getRenderOutput()).toMatchSnapshot();
60+
61+
rnd.render((
862
<ScreeningStatus
963
screeningObject={{
1064
status: 'Screening Status',
@@ -13,5 +67,5 @@ test('Matches shallow shapshot', () => {
1367
submissionId="12345"
1468
/>
1569
));
16-
expect(renderer.getRenderOutput()).toMatchSnapshot();
70+
expect(rnd.getRenderOutput()).toMatchSnapshot();
1771
});

0 commit comments

Comments
 (0)