-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
nav_control_component.test.tsx
184 lines (156 loc) · 6.83 KB
/
nav_control_component.test.tsx
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { EuiHeaderSectionItemButton, EuiPopover } from '@elastic/eui';
import React from 'react';
import { BehaviorSubject } from 'rxjs';
import { findTestSubject, mountWithIntl, nextTick, shallowWithIntl } from '@kbn/test/jest';
import type { AuthenticatedUser } from '../../common/model';
import { mockAuthenticatedUser } from '../../common/model/authenticated_user.mock';
import { SecurityNavControl } from './nav_control_component';
describe('SecurityNavControl', () => {
it(`renders a loading spinner when the user promise hasn't resolved yet.`, async () => {
const props = {
user: new Promise<AuthenticatedUser>(() => mockAuthenticatedUser()),
editProfileUrl: '',
logoutUrl: '',
userMenuLinks$: new BehaviorSubject([]),
};
const wrapper = shallowWithIntl(<SecurityNavControl {...props} />);
const { button } = wrapper.find(EuiPopover).props();
expect(button).toMatchInlineSnapshot(`
<EuiHeaderSectionItemButton
aria-controls="headerUserMenu"
aria-expanded={false}
aria-haspopup="true"
aria-label="Account menu"
data-test-subj="userMenuButton"
onClick={[Function]}
>
<EuiLoadingSpinner
size="m"
/>
</EuiHeaderSectionItemButton>
`);
});
it(`renders an avatar after the user promise resolves.`, async () => {
const props = {
user: Promise.resolve(mockAuthenticatedUser({ full_name: 'foo' })),
editProfileUrl: '',
logoutUrl: '',
userMenuLinks$: new BehaviorSubject([]),
};
const wrapper = shallowWithIntl(<SecurityNavControl {...props} />);
await nextTick();
wrapper.update();
const { button } = wrapper.find(EuiPopover).props();
expect(button).toMatchInlineSnapshot(`
<EuiHeaderSectionItemButton
aria-controls="headerUserMenu"
aria-expanded={false}
aria-haspopup="true"
aria-label="Account menu"
data-test-subj="userMenuButton"
onClick={[Function]}
>
<EuiAvatar
name="foo"
size="s"
/>
</EuiHeaderSectionItemButton>
`);
});
it(`doesn't render the popover when the user hasn't been loaded yet`, async () => {
const props = {
user: Promise.resolve(mockAuthenticatedUser({ full_name: 'foo' })),
editProfileUrl: '',
logoutUrl: '',
userMenuLinks$: new BehaviorSubject([]),
};
const wrapper = mountWithIntl(<SecurityNavControl {...props} />);
// not awaiting the user promise
expect(findTestSubject(wrapper, 'userMenu')).toHaveLength(0);
expect(findTestSubject(wrapper, 'profileLink')).toHaveLength(0);
expect(findTestSubject(wrapper, 'logoutLink')).toHaveLength(0);
wrapper.find(EuiHeaderSectionItemButton).simulate('click');
expect(findTestSubject(wrapper, 'userMenu')).toHaveLength(0);
expect(findTestSubject(wrapper, 'profileLink')).toHaveLength(0);
expect(findTestSubject(wrapper, 'logoutLink')).toHaveLength(0);
});
it('renders a popover when the avatar is clicked.', async () => {
const props = {
user: Promise.resolve(mockAuthenticatedUser({ full_name: 'foo' })),
editProfileUrl: '',
logoutUrl: '',
userMenuLinks$: new BehaviorSubject([]),
};
const wrapper = mountWithIntl(<SecurityNavControl {...props} />);
await nextTick();
wrapper.update();
expect(findTestSubject(wrapper, 'userMenu')).toHaveLength(0);
expect(findTestSubject(wrapper, 'profileLink')).toHaveLength(0);
expect(findTestSubject(wrapper, 'logoutLink')).toHaveLength(0);
wrapper.find(EuiHeaderSectionItemButton).simulate('click');
expect(findTestSubject(wrapper, 'userMenu')).toHaveLength(1);
expect(findTestSubject(wrapper, 'profileLink')).toHaveLength(1);
expect(findTestSubject(wrapper, 'logoutLink')).toHaveLength(1);
});
it('renders a popover with additional user menu links registered by other plugins', async () => {
const props = {
user: Promise.resolve(mockAuthenticatedUser({ full_name: 'foo' })),
editProfileUrl: '',
logoutUrl: '',
userMenuLinks$: new BehaviorSubject([
{ label: 'link1', href: 'path-to-link-1', iconType: 'empty', order: 1 },
{ label: 'link2', href: 'path-to-link-2', iconType: 'empty', order: 2 },
{ label: 'link3', href: 'path-to-link-3', iconType: 'empty', order: 3 },
]),
};
const wrapper = mountWithIntl(<SecurityNavControl {...props} />);
await nextTick();
wrapper.update();
expect(findTestSubject(wrapper, 'userMenu')).toHaveLength(0);
expect(findTestSubject(wrapper, 'profileLink')).toHaveLength(0);
expect(findTestSubject(wrapper, 'userMenuLink__link1')).toHaveLength(0);
expect(findTestSubject(wrapper, 'userMenuLink__link2')).toHaveLength(0);
expect(findTestSubject(wrapper, 'userMenuLink__link3')).toHaveLength(0);
expect(findTestSubject(wrapper, 'logoutLink')).toHaveLength(0);
wrapper.find(EuiHeaderSectionItemButton).simulate('click');
expect(findTestSubject(wrapper, 'userMenu')).toHaveLength(1);
expect(findTestSubject(wrapper, 'profileLink')).toHaveLength(1);
expect(findTestSubject(wrapper, 'userMenuLink__link1')).toHaveLength(1);
expect(findTestSubject(wrapper, 'userMenuLink__link2')).toHaveLength(1);
expect(findTestSubject(wrapper, 'userMenuLink__link3')).toHaveLength(1);
expect(findTestSubject(wrapper, 'logoutLink')).toHaveLength(1);
});
it('properly renders a popover for anonymous user.', async () => {
const props = {
user: Promise.resolve(
mockAuthenticatedUser({
authentication_provider: { type: 'anonymous', name: 'does no matter' },
})
),
editProfileUrl: '',
logoutUrl: '',
userMenuLinks$: new BehaviorSubject([
{ label: 'link1', href: 'path-to-link-1', iconType: 'empty', order: 1 },
{ label: 'link2', href: 'path-to-link-2', iconType: 'empty', order: 2 },
{ label: 'link3', href: 'path-to-link-3', iconType: 'empty', order: 3 },
]),
};
const wrapper = mountWithIntl(<SecurityNavControl {...props} />);
await nextTick();
wrapper.update();
expect(findTestSubject(wrapper, 'userMenu')).toHaveLength(0);
expect(findTestSubject(wrapper, 'profileLink')).toHaveLength(0);
expect(findTestSubject(wrapper, 'logoutLink')).toHaveLength(0);
wrapper.find(EuiHeaderSectionItemButton).simulate('click');
expect(findTestSubject(wrapper, 'userMenu')).toHaveLength(1);
expect(findTestSubject(wrapper, 'profileLink')).toHaveLength(0);
expect(findTestSubject(wrapper, 'logoutLink')).toHaveLength(1);
expect(findTestSubject(wrapper, 'logoutLink').text()).toBe('Log in');
});
});