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
|
import { GlCollapsibleListbox, GlListboxItem } from '@gitlab/ui';
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown/timezone_dropdown.vue';
import { formatTimezone } from '~/lib/utils/datetime_utility';
import { findTzByName, timezoneDataFixture } from './helpers';
describe('Deploy freeze timezone dropdown', () => {
let wrapper;
let store;
const findDropdown = () => wrapper.findComponent(GlCollapsibleListbox);
const findSearchBox = () => wrapper.findByTestId('listbox-search-input');
const createComponent = async (searchTerm, selectedTimezone) => {
wrapper = shallowMountExtended(TimezoneDropdown, {
store,
propsData: {
value: selectedTimezone,
timezoneData: timezoneDataFixture,
name: 'user[timezone]',
},
stubs: {
GlCollapsibleListbox,
},
});
findSearchBox().vm.$emit('input', searchTerm);
await nextTick();
};
const findAllDropdownItems = () => wrapper.findAllComponents(GlListboxItem);
const findDropdownItemByIndex = (index) => findAllDropdownItems().at(index);
const findEmptyResultsItem = () => wrapper.findByTestId('listbox-no-results-text');
const findHiddenInput = () => wrapper.find('input');
describe('No time zones found', () => {
beforeEach(async () => {
await createComponent('UTC timezone');
});
it('renders empty results message', () => {
expect(findEmptyResultsItem().exists()).toBe(true);
expect(findEmptyResultsItem().text()).toBe('No matching results');
});
});
describe('Search term is empty', () => {
beforeEach(async () => {
await createComponent('');
});
it('renders all timezones when search term is empty', () => {
expect(findAllDropdownItems()).toHaveLength(timezoneDataFixture.length);
});
});
describe('Time zones found', () => {
beforeEach(async () => {
await createComponent('Alaska');
});
it('renders only the time zone searched for', () => {
const selectedTz = findTzByName('Alaska');
expect(findAllDropdownItems()).toHaveLength(1);
expect(findDropdownItemByIndex(0).text()).toBe(formatTimezone(selectedTz));
});
it('should not display empty results message', () => {
expect(findEmptyResultsItem().exists()).toBe(false);
});
describe('Custom events', () => {
const selectedTz = findTzByName('Alaska');
it('should emit input if a time zone is clicked', () => {
const payload = formatTimezone(selectedTz);
findDropdown().vm.$emit('select', payload);
expect(wrapper.emitted('input')).toEqual([
[
{
formattedTimezone: payload,
identifier: selectedTz.identifier,
},
],
]);
});
});
});
describe('Selected time zone not found', () => {
beforeEach(async () => {
await createComponent('', 'Berlin');
});
it('renders empty selections', () => {
expect(findDropdown().props('toggleText')).toBe('Select timezone');
});
it('preserves initial value in the associated input', () => {
expect(findHiddenInput().attributes('value')).toBe('Berlin');
});
});
describe('Selected time zone found', () => {
beforeEach(async () => {
await createComponent('', 'Europe/Berlin');
});
it('renders selected time zone as dropdown label', () => {
expect(findDropdown().props('toggleText')).toBe('[UTC+2] Berlin');
});
it('adds a checkmark to the selected option', async () => {
findDropdown().vm.$emit('select', formatTimezone(findTzByName('Abu Dhabi')));
await nextTick();
expect(findDropdownItemByIndex(0).props('isSelected')).toBe(true);
});
});
});
|