/
Header.test.js
197 lines (176 loc) · 5.66 KB
/
Header.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { shallow } from 'enzyme';
import React from 'react';
import { plain as Header, mapStateToProps } from './Header';
describe('Header component', () => {
it('renders correctly when the dropdown is closed and the page is top level', () => {
const component = shallow(
<Header
authenticated
currentUser={{
role: 'admin',
state: { id: 'wa', name: 'Washington' },
username: 'frasiercrane@kacl.com'
}}
isAdmin
pushRoute={() => {}}
ariaExpanded={false}
showSiteTitle
/>
);
expect(component).toMatchSnapshot();
});
it('opens and closes the dropdown when clicked ', () => {
const component = shallow(
<Header
authenticated
currentUser={{
role: 'admin',
state: { id: 'wa', name: 'Washington' },
username: 'frasiercrane@kacl.com'
}}
isAdmin
pushRoute={() => {}}
ariaExpanded={false}
showSiteTitle
/>
);
jest.spyOn(document, 'addEventListener');
jest.spyOn(document, 'removeEventListener');
component.find('.nav--dropdown__trigger').simulate('click');
expect(component).toMatchSnapshot();
expect(component.state().ariaExpanded).toBeTruthy();
expect(document.addEventListener).toHaveBeenCalled();
expect(document.addEventListener.mock.calls[0][0]).toEqual('click');
const handler = document.addEventListener.mock.calls[0][1];
component.find('.nav--dropdown__trigger').simulate('click');
expect(component.state().ariaExpanded).toBeFalsy();
expect(document.removeEventListener).toHaveBeenCalled();
expect(document.removeEventListener.mock.calls[0][0]).toEqual('click');
expect(document.removeEventListener.mock.calls[0][1]).toEqual(handler);
});
it('handles outside click events', () => {
// Create an instance of the component object but don't mount it. This
// breaks us out of all the React requirements, which is handy because
// we don't actually need them for these tests.
const component = new Header({
authenticated: true,
currentUser: {
role: 'admin',
state: { id: 'wa', name: 'Washington' },
username: 'frasiercrane@kacl.com'
},
isAdmin: true,
pushRoute: () => {},
ariaExpanded: false,
showSiteTitle: true
});
// React sets the node for us, so since we're not Reacting, do it manually.
// In the first case, make the contains fn return false.
component.node = { current: { contains: jest.fn(() => false) } };
jest.spyOn(document, 'removeEventListener');
// By default, spyOn calls the real method as well, but that'll cause a
// React error here because the component isn't mounted
jest.spyOn(component, 'setState').mockImplementation(() => {});
// Call the method under test
component.handleOutsideClick({});
// Make sure the state was set and the event handler was removed.
expect(component.setState).toHaveBeenCalled();
expect(component.setState.mock.calls[0][0]).toEqual({
ariaExpanded: false
});
expect(document.removeEventListener).toHaveBeenCalled();
expect(document.removeEventListener.mock.calls[0][0]).toEqual('click');
// we can't check the function because of weird binding
// Reset the mocks
document.removeEventListener.mockClear();
component.setState.mockClear();
// Change the node contains function to return true this time.
component.node.current.contains = jest.fn(() => true);
component.handleOutsideClick({});
expect(component.setState).not.toHaveBeenCalled();
expect(document.removeEventListener).not.toHaveBeenCalled();
});
it('renders the admin home title when an admin user is at a secondary page', () => {
const component = shallow(
<Header
authenticated
currentUser={{
role: 'admin',
state: { id: 'wa', name: 'Washington' },
username: 'frasiercrane@kacl.com'
}}
isAdmin
pushRoute={() => {}}
ariaExpanded={false}
showSiteTitle={false}
/>
);
expect(component).toMatchSnapshot();
});
it('renders the state user home title when a state user is at a secondary page', () => {
// Not saving
expect(
shallow(
<Header
ariaExpanded={false}
authenticated
currentUser={{
role: 'admin',
state: { id: 'wa', name: 'Washington' },
username: 'frasiercrane@kacl.com'
}}
isAdmin={false}
isSaving={false}
lastSaved="2020-01-01T12:00:00.000Z"
pushRoute={() => {}}
showSiteTitle={false}
/>
)
).toMatchSnapshot();
// Saving
expect(
shallow(
<Header
ariaExpanded={false}
authenticated
currentUser={{
role: 'admin',
state: { id: 'wa', name: 'Washington' },
username: 'frasiercrane@kacl.com'
}}
isAdmin={false}
isSaving
lastSaved="2020-01-01T17:00:00.000Z"
pushRoute={() => {}}
showSiteTitle={false}
/>
)
).toMatchSnapshot();
});
it('maps state to props', () => {
const state = {
auth: {
authenticated: 'some value',
user: {
role: 'admin'
}
},
saving: {
lastSaved: 'blorp',
saving: 'bloop'
},
user: {
data: {
role: 'admin'
}
}
};
expect(mapStateToProps(state)).toEqual({
authenticated: 'some value',
currentUser: { role: 'admin' },
isAdmin: true,
isSaving: 'bloop',
lastSaved: 'blorp'
});
});
});