-
Notifications
You must be signed in to change notification settings - Fork 17
/
back-link-helper.test.js
104 lines (83 loc) · 3.55 KB
/
back-link-helper.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-env jest */
import BackLinkHelper from './back-link-helper';
describe('/components/back-link-helper', () => {
const mockWindow = {
history: {
replaceState: jest.fn(),
back: jest.fn(),
},
location: {
href: 'https://tax.service.gov.uk/current/url',
host: 'https://tax.service.gov.uk',
},
};
const mockDocument = {
referrer: 'https://tax.service.gov.uk/some/referrer',
};
const mockAnchorTag = {
addEventListener: jest.fn(),
classList: {
add: jest.fn(),
},
};
const mockEvent = {
preventDefault: jest.fn(),
};
beforeEach(() => {
mockWindow.history.back.mockReset();
mockWindow.history.replaceState.mockReset();
mockAnchorTag.addEventListener.mockReset();
mockEvent.preventDefault.mockReset();
});
describe('init', () => {
it('updates the window history with the current URL', () => {
const sut = new BackLinkHelper(mockAnchorTag, mockWindow, mockDocument);
sut.init();
expect(mockWindow.history.replaceState)
.toHaveBeenCalledWith(null, null, 'https://tax.service.gov.uk/current/url');
});
it('does not throw if window.history API is absent', () => {
const ancientBrowser = {};
const sut = new BackLinkHelper(mockAnchorTag, ancientBrowser, mockDocument);
expect(() => sut.init()).not.toThrow();
});
it('does not throw if window.history.replaceState is not implemented', () => {
const ancientBrowser = { history: {}, location: { host: 'https://tax.service.gov.uk' } };
const sut = new BackLinkHelper(mockAnchorTag, ancientBrowser, mockDocument);
expect(() => sut.init()).not.toThrow();
});
it('adds a click event handler which delegates to window.history.back()', () => {
const sut = new BackLinkHelper(mockAnchorTag, mockWindow, mockDocument);
sut.init();
expect(mockAnchorTag.addEventListener)
.toHaveBeenCalledWith('click', expect.any(Function));
expect(mockWindow.history.back).not.toHaveBeenCalled();
const callbackFn = mockAnchorTag.addEventListener.mock.calls[0][1];
callbackFn(mockEvent);
expect(mockEvent.preventDefault).toHaveBeenCalled();
expect(mockWindow.history.back).toHaveBeenCalled();
});
it('adds .hmrc-hidden-backlink class to backlink when document referer is empty', () => {
const sut = new BackLinkHelper(mockAnchorTag, mockWindow, { referrer: '' });
sut.init();
expect(mockAnchorTag.classList.add.mock.calls[0][0]).toBe('hmrc-hidden-backlink');
});
it('adds .hmrc-hidden-backlink class to backlink when document referer is null', () => {
const sut = new BackLinkHelper(mockAnchorTag, mockWindow, { referrer: null });
sut.init();
expect(mockAnchorTag.classList.add.mock.calls[0][0]).toBe('hmrc-hidden-backlink');
});
it('adds .hmrc-hidden-backlink class to backlink if document referer is on a different domain', () => {
const sut = new BackLinkHelper(mockAnchorTag, mockWindow, { referrer: 'https://some.other.site/some/referrer' });
sut.init();
expect(mockAnchorTag.classList.add.mock.calls[0][0]).toBe('hmrc-hidden-backlink');
});
it('does not throw if window.history.back() is not implemented', () => {
const ancientBrowser = { history: {}, location: { host: 'https://tax.service.gov.uk' } };
const sut = new BackLinkHelper(mockAnchorTag, ancientBrowser, mockDocument);
sut.init();
const callbackFn = mockAnchorTag.addEventListener.mock.calls[0][1];
expect(() => callbackFn(mockEvent)).not.toThrow();
});
});
});