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
|
import { GlDatepicker } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { useFakeDate } from 'helpers/fake_date';
import waitForPromises from 'helpers/wait_for_promises';
import ExpirationDatepicker from '~/members/components/table/expiration_datepicker.vue';
import { MEMBER_TYPES } from '~/members/constants';
import { member } from '../../mock_data';
Vue.use(Vuex);
describe('ExpirationDatepicker', () => {
// March 15th, 2020 3:00
useFakeDate(2020, 2, 15, 3);
let wrapper;
let actions;
let resolveUpdateMemberExpiration;
const $toast = {
show: jest.fn(),
};
const createStore = () => {
actions = {
updateMemberExpiration: jest.fn(
() =>
new Promise((resolve) => {
resolveUpdateMemberExpiration = resolve;
}),
),
};
return new Vuex.Store({
modules: {
[MEMBER_TYPES.user]: { namespaced: true, actions },
},
});
};
const createComponent = (propsData = {}) => {
wrapper = mount(ExpirationDatepicker, {
propsData: {
member,
permissions: { canUpdate: true },
...propsData,
},
provide: {
namespace: MEMBER_TYPES.user,
},
store: createStore(),
mocks: {
$toast,
},
});
};
const findInput = () => wrapper.find('input');
const findDatepicker = () => wrapper.findComponent(GlDatepicker);
describe('datepicker input', () => {
it('sets `member.expiresAt` as initial date', async () => {
createComponent({ member: { ...member, expiresAt: '2020-03-17T00:00:00Z' } });
await nextTick();
expect(findInput().element.value).toBe('2020-03-17');
});
});
describe('props', () => {
beforeEach(() => {
createComponent();
});
it('sets `minDate` prop as tomorrow', () => {
expect(findDatepicker().props('minDate').toISOString()).toBe(
new Date('2020-3-16').toISOString(),
);
});
it('sets `target` prop as `null` so datepicker opens on focus', () => {
expect(findDatepicker().props('target')).toBe(null);
});
it("sets `container` prop as `null` so table styles don't affect the datepicker styles", () => {
expect(findDatepicker().props('container')).toBe(null);
});
it('shows clear button', () => {
expect(findDatepicker().props('showClearButton')).toBe(true);
});
});
describe('when datepicker is changed', () => {
beforeEach(() => {
createComponent();
findDatepicker().vm.$emit('input', new Date('2020-03-17'));
});
it('calls `updateMemberExpiration` Vuex action', () => {
expect(actions.updateMemberExpiration).toHaveBeenCalledWith(expect.any(Object), {
memberId: member.id,
expiresAt: new Date('2020-03-17'),
});
});
it('displays toast when successful', async () => {
resolveUpdateMemberExpiration();
await waitForPromises();
expect($toast.show).toHaveBeenCalledWith('Expiration date updated successfully.');
});
it('disables dropdown while waiting for `updateMemberExpiration` to resolve', async () => {
expect(findDatepicker().props('disabled')).toBe(true);
resolveUpdateMemberExpiration();
await waitForPromises();
expect(findDatepicker().props('disabled')).toBe(false);
});
});
describe('when datepicker is cleared', () => {
beforeEach(async () => {
createComponent();
findInput().setValue('2020-03-17');
await nextTick();
wrapper.find('[data-testid="clear-button"]').trigger('click');
});
it('calls `updateMemberExpiration` Vuex action', () => {
expect(actions.updateMemberExpiration).toHaveBeenCalledWith(expect.any(Object), {
memberId: member.id,
expiresAt: null,
});
});
it('displays toast when successful', async () => {
resolveUpdateMemberExpiration();
await waitForPromises();
expect($toast.show).toHaveBeenCalledWith('Expiration date removed successfully.');
});
it('disables datepicker while waiting for `updateMemberExpiration` to resolve', async () => {
expect(findDatepicker().props('disabled')).toBe(true);
resolveUpdateMemberExpiration();
await waitForPromises();
expect(findDatepicker().props('disabled')).toBe(false);
});
});
describe('when user does not have `canUpdate` permissions', () => {
it('disables datepicker', () => {
createComponent({ permissions: { canUpdate: false } });
expect(findDatepicker().props('disabled')).toBe(true);
});
});
});
|