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
|
import MockAdapter from 'axios-mock-adapter';
import { memoize, cloneDeep } from 'lodash';
import usersFixture from 'test_fixtures/autocomplete/users.json';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
import UsersSelect from '~/users_select';
// fixtures -------------------------------------------------------------------
const getUserSearchHTML = memoize((fixture) => {
const parser = new DOMParser();
const el = parser.parseFromString(fixture, 'text/html').querySelector('.assignee');
return el.outerHTML;
});
const getUsersFixture = () => usersFixture;
export const getUsersFixtureAt = (idx) => getUsersFixture()[idx];
// test context ---------------------------------------------------------------
export const createTestContext = ({ fixture }) => {
let mock = null;
let subject = null;
const setup = () => {
const rootEl = document.createElement('div');
rootEl.innerHTML = getUserSearchHTML(fixture);
document.body.appendChild(rootEl);
mock = new MockAdapter(axios);
mock.onGet('/-/autocomplete/users.json').reply(HTTP_STATUS_OK, cloneDeep(getUsersFixture()));
};
const teardown = () => {
mock.restore();
document.body.innerHTML = '';
subject = null;
};
const createSubject = () => {
if (subject) {
throw new Error('test subject is already created');
}
subject = new UsersSelect(null);
};
return {
setup,
teardown,
createSubject,
};
};
// finders -------------------------------------------------------------------
export const findAssigneesInputs = () =>
document.querySelectorAll('input[name="merge_request[assignee_ids][]');
export const findAssigneesInputsModel = () =>
Array.from(findAssigneesInputs()).map((input) => ({
value: input.value,
dataset: { ...input.dataset },
}));
export const findUserSearchButton = () => document.querySelector('.js-user-search');
export const findDropdownItem = ({ id }) => document.querySelector(`li[data-user-id="${id}"] a`);
export const findDropdownItemsModel = () =>
Array.from(document.querySelectorAll('.dropdown-content li')).map((el) => {
if (el.classList.contains('divider')) {
return {
type: 'divider',
};
} else if (el.classList.contains('dropdown-header')) {
return {
type: 'dropdown-header',
text: el.textContent,
};
}
return {
type: 'user',
userId: el.dataset.userId,
};
});
// arrange/act helpers -------------------------------------------------------
export const setAssignees = (...users) => {
findAssigneesInputs().forEach((x) => x.remove());
const container = document.querySelector('.js-sidebar-assignee-data');
container.prepend(
...users.map((user) => {
const input = document.createElement('input');
input.name = 'merge_request[assignee_ids][]';
input.value = user.id.toString();
input.dataset.avatarUrl = user.avatar_url;
input.dataset.name = user.name;
input.dataset.username = user.username;
input.dataset.canMerge = user.can_merge;
return input;
}),
);
};
export const toggleDropdown = () => findUserSearchButton().click();
export const waitForDropdownItems = async () => {
await axios.waitForAll();
await waitForPromises();
};
// assertion helpers ---------------------------------------------------------
export const createUnassignedExpectation = () => {
return [
{ type: 'user', userId: '0' },
{ type: 'divider' },
...getUsersFixture().map((x) => ({
type: 'user',
userId: x.id.toString(),
})),
];
};
export const createAssignedExpectation = ({ header, assigned }) => {
const assignedIds = new Set(assigned.map((x) => x.id));
const unassignedIds = getUsersFixture().filter((x) => !assignedIds.has(x.id));
return [
{ type: 'user', userId: '0' },
{ type: 'divider' },
{ type: 'dropdown-header', text: header },
...assigned.map((x) => ({ type: 'user', userId: x.id.toString() })),
{ type: 'divider' },
...unassignedIds.map((x) => ({ type: 'user', userId: x.id.toString() })),
];
};
export const createInputsModelExpectation = (users) =>
users.map((user) => ({
value: user.id.toString(),
dataset: {
approved: user.approved.toString(),
avatar_url: user.avatar_url,
can_merge: user.can_merge.toString(),
can_update_merge_request: user.can_update_merge_request.toString(),
id: user.id.toString(),
name: user.name,
show_status: user.show_status.toString(),
state: user.state,
username: user.username,
web_url: user.web_url,
},
}));
|