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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
import { GlBadge, GlButton, GlAvatar } from '@gitlab/ui';
import { RouterLinkStub } from '@vue/test-utils';
import { nextTick } from 'vue';
import { mountExtended, extendedWrapper } from 'helpers/vue_test_utils_helper';
import NavItem from '~/super_sidebar/components/nav_item.vue';
import NavItemRouterLink from '~/super_sidebar/components/nav_item_router_link.vue';
import NavItemLink from '~/super_sidebar/components/nav_item_link.vue';
import {
CLICK_MENU_ITEM_ACTION,
TRACKING_UNKNOWN_ID,
TRACKING_UNKNOWN_PANEL,
} from '~/super_sidebar/constants';
import eventHub from '~/super_sidebar/event_hub';
describe('NavItem component', () => {
let wrapper;
const findAvatar = () => wrapper.findComponent(GlAvatar);
const findLink = () => wrapper.findByTestId('nav-item-link');
const findPill = () => wrapper.findComponent(GlBadge);
const findPinButton = () => wrapper.findComponent(GlButton);
const findNavItemRouterLink = () => extendedWrapper(wrapper.findComponent(NavItemRouterLink));
const findNavItemLink = () => extendedWrapper(wrapper.findComponent(NavItemLink));
const createWrapper = ({ item, props = {}, provide = {}, routerLinkSlotProps = {} }) => {
wrapper = mountExtended(NavItem, {
propsData: {
item,
...props,
},
provide,
stubs: {
RouterLink: {
...RouterLinkStub,
render() {
const children = this.$scopedSlots.default({
href: '/foo',
isActive: false,
navigate: jest.fn(),
...routerLinkSlotProps,
});
return children;
},
},
},
});
};
describe('pills', () => {
it.each([0, 5, 3.4, 'foo', '10%'])('item with pill_data `%p` renders a pill', (pillCount) => {
createWrapper({ item: { title: 'Foo', pill_count: pillCount } });
expect(findPill().text()).toBe(pillCount.toString());
});
it.each([null, undefined, false, true, '', NaN, Number.POSITIVE_INFINITY])(
'item with pill_data `%p` renders no pill',
(pillCount) => {
createWrapper({ item: { title: 'Foo', pill_count: pillCount } });
expect(findPill().exists()).toBe(false);
},
);
describe('updating pill value', () => {
const initialPillValue = '20%';
const updatedPillValue = '50%';
const itemIdForUpdate = '_some_item_id_';
const triggerPillValueUpdate = async ({
value = updatedPillValue,
itemId = itemIdForUpdate,
} = {}) => {
eventHub.$emit('updatePillValue', { value, itemId });
await nextTick();
};
it('updates the pill count', async () => {
createWrapper({ item: { id: itemIdForUpdate, pill_count: initialPillValue } });
await triggerPillValueUpdate();
expect(findPill().text()).toBe(updatedPillValue);
});
it('does not update the pill count for non matching item id', async () => {
createWrapper({ item: { id: '_non_matching_id_', pill_count: initialPillValue } });
await triggerPillValueUpdate();
expect(findPill().text()).toBe(initialPillValue);
});
});
describe('async updating pill prop', () => {
it('re-renders item with when prop pill_count changes', async () => {
createWrapper({ item: { title: 'Foo', pill_count: 0 } });
expect(findPill().text()).toBe('0');
// https://gitlab.com/gitlab-org/gitlab/-/issues/428246
// This is testing specific async behaviour that was before missed
await wrapper.setProps({ item: { title: 'Foo', pill_count: 10 } });
expect(findPill().text()).toBe('10');
});
});
});
describe('destroyed', () => {
it('should unbind event listeners on eventHub', async () => {
jest.spyOn(eventHub, '$off');
createWrapper({ item: {} });
await wrapper.destroy();
expect(eventHub.$off).toHaveBeenCalledWith('updatePillValue', expect.any(Function));
});
});
describe('pins', () => {
describe('when pins are not supported', () => {
it('does not render pin button', () => {
createWrapper({
item: { title: 'Foo' },
provide: {
panelSupportsPins: false,
},
});
expect(findPinButton().exists()).toBe(false);
});
});
describe('when pins are supported', () => {
beforeEach(() => {
createWrapper({
item: { title: 'Foo' },
provide: {
panelSupportsPins: true,
},
});
});
it('renders pin button', () => {
expect(findPinButton().exists()).toBe(true);
});
it('contains an aria-label', () => {
expect(findPinButton().attributes('aria-label')).toBe('Pin Foo');
});
it('toggles pointer events on after CSS fade-in', async () => {
const pinButton = findPinButton();
expect(pinButton.classes()).toContain('gl-pointer-events-none');
wrapper.trigger('mouseenter');
pinButton.vm.$emit('transitionend');
await nextTick();
expect(pinButton.classes()).not.toContain('gl-pointer-events-none');
});
it('does not toggle pointer events if mouse leaves before CSS fade-in ends', async () => {
const pinButton = findPinButton();
expect(pinButton.classes()).toContain('gl-pointer-events-none');
wrapper.trigger('mouseenter');
wrapper.trigger('mousemove');
wrapper.trigger('mouseleave');
pinButton.vm.$emit('transitionend');
await nextTick();
expect(pinButton.classes()).toContain('gl-pointer-events-none');
});
});
});
it('applies custom link classes', () => {
const customClass = 'customClass';
createWrapper({
item: { title: 'Foo' },
props: {
linkClasses: {
[customClass]: true,
},
},
});
expect(findLink().attributes('class')).toContain(customClass);
});
it('applies custom classes set in the backend', () => {
const customClass = 'customBackendClass';
createWrapper({ item: { title: 'Foo', link_classes: customClass } });
expect(findLink().attributes('class')).toContain(customClass);
});
it('applies data-method specified in the backend', () => {
const method = 'post';
createWrapper({ item: { title: 'Foo', data_method: method } });
expect(findLink().attributes('data-method')).toContain(method);
});
describe('Data Tracking Attributes', () => {
it.each`
id | panelType | eventLabel | eventProperty | eventExtra
${'abc'} | ${'xyz'} | ${'abc'} | ${'nav_panel_xyz'} | ${undefined}
${undefined} | ${'xyz'} | ${TRACKING_UNKNOWN_ID} | ${'nav_panel_xyz'} | ${'{"title":"Foo"}'}
${'abc'} | ${undefined} | ${'abc'} | ${TRACKING_UNKNOWN_PANEL} | ${'{"title":"Foo"}'}
${undefined} | ${undefined} | ${TRACKING_UNKNOWN_ID} | ${TRACKING_UNKNOWN_PANEL} | ${'{"title":"Foo"}'}
`(
'adds appropriate data tracking labels for id=$id and panelType=$panelType',
({ id, eventLabel, panelType, eventProperty, eventExtra }) => {
createWrapper({ item: { title: 'Foo', id }, props: {}, provide: { panelType } });
expect(findLink().attributes('data-track-action')).toBe(CLICK_MENU_ITEM_ACTION);
expect(findLink().attributes('data-track-label')).toBe(eventLabel);
expect(findLink().attributes('data-track-property')).toBe(eventProperty);
expect(findLink().attributes('data-track-extra')).toBe(eventExtra);
},
);
});
describe('when `item` prop has `to` attribute', () => {
describe('when `RouterLink` is not active', () => {
it('renders `NavItemRouterLink` with active indicator hidden', () => {
createWrapper({ item: { title: 'Foo', to: { name: 'foo' } } });
expect(findNavItemRouterLink().findByTestId('active-indicator').classes()).toContain(
'gl-opacity-0',
);
});
});
describe('when `RouterLink` is active', () => {
it('renders `NavItemRouterLink` with active indicator shown', () => {
createWrapper({
item: { title: 'Foo', to: { name: 'foo' } },
routerLinkSlotProps: { isActive: true },
});
expect(findNavItemRouterLink().findByTestId('active-indicator').classes()).toContain(
'gl-opacity-10',
);
});
});
});
describe('when `item` prop has `link` attribute', () => {
describe('when `item` has `is_active` set to `false`', () => {
it('renders `NavItemLink` with active indicator hidden', () => {
createWrapper({ item: { title: 'Foo', link: '/foo', is_active: false } });
expect(findNavItemLink().findByTestId('active-indicator').classes()).toContain(
'gl-opacity-0',
);
});
});
describe('when `item` has `is_active` set to `true`', () => {
it('renders `NavItemLink` with active indicator shown', () => {
createWrapper({ item: { title: 'Foo', link: '/foo', is_active: true } });
expect(findNavItemLink().findByTestId('active-indicator').classes()).toContain(
'gl-opacity-10',
);
});
});
});
describe('when `item` prop has `entity_id` attribute', () => {
it('renders an avatar', () => {
createWrapper({
item: { title: 'Foo', entity_id: 123, avatar: '/avatar.png', avatar_shape: 'circle' },
});
expect(findAvatar().props()).toMatchObject({
entityId: 123,
shape: 'circle',
src: '/avatar.png',
});
});
});
describe('when `item.is_active` is true', () => {
it('scrolls into view', () => {
createWrapper({
item: { is_active: true },
});
expect(wrapper.element.scrollIntoView).toHaveBeenNthCalledWith(1, false);
});
});
describe('when `item.is_active` is false', () => {
it('scrolls not into view', () => {
createWrapper({
item: { is_active: false },
});
expect(wrapper.element.scrollIntoView).not.toHaveBeenCalled();
});
});
});
|