This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathbasics.test.ts
312 lines (266 loc) · 10 KB
/
basics.test.ts
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
306
307
308
309
310
311
312
import { By, until } from 'selenium-webdriver';
import { expect } from 'chai';
import path from 'path';
import { runTestServer } from './testServer';
import { runDriver } from './webdriver';
import { runUI } from './uiSetup';
// extra timeout allowance on CI
const testTimeoutMs = process.env.CI ? 20000 : 10000;
describe('importer basics', () => {
const appUrl = runTestServer();
const getDriver = runDriver();
const initUI = runUI(getDriver);
beforeEach(async () => {
await getDriver().get(appUrl);
await initUI((React, ReactDOM, ReactCSVImporter, ReactCSVImporterField) => {
ReactDOM.render(
React.createElement(
ReactCSVImporter,
{
dataHandler: (rows, info) => {
((window as unknown) as Record<
string,
unknown
>).TEST_DATA_HANDLER_ROWS = rows;
((window as unknown) as Record<
string,
unknown
>).TEST_DATA_HANDLER_INFO = info;
return new Promise((resolve) => {
((window as unknown) as Record<
string,
unknown
>).TEST_DATA_HANDLER_RESOLVE = resolve;
});
}
},
[
React.createElement(ReactCSVImporterField, {
name: 'fieldA',
label: 'Field A'
}),
React.createElement(ReactCSVImporterField, {
name: 'fieldB',
label: 'Field B',
optional: true
})
]
),
document.getElementById('root')
);
});
});
it('shows file selector', async () => {
const fileInput = await getDriver().findElement(By.xpath('//input'));
expect(await fileInput.getAttribute('type')).to.equal('file');
});
describe('with file selected', () => {
beforeEach(async () => {
const filePath = path.resolve(__dirname, './fixtures/simple.csv');
const fileInput = await getDriver().findElement(By.xpath('//input'));
await fileInput.sendKeys(filePath);
await getDriver().wait(
until.elementLocated(By.xpath('//*[contains(., "Raw File Contents")]')),
300 // extra time
);
});
it('shows file name under active focus for screen reader', async () => {
const focusedHeading = await getDriver().switchTo().activeElement();
expect(await focusedHeading.getText()).to.equal('simple.csv');
});
it('shows raw file contents', async () => {
const rawPreview = await getDriver().findElement(By.xpath('//pre'));
expect(await rawPreview.getText()).to.have.string('AAAA,BBBB,CCCC,DDDD');
});
it('shows a preview table', async () => {
const tablePreview = await getDriver().findElement(By.xpath('//table'));
// header row
const tableCols = await tablePreview.findElements(
By.xpath('thead/tr/th')
);
const tableColStrings = await tableCols.reduce(
async (acc, col) => [...(await acc), await col.getText()],
Promise.resolve([] as string[])
);
expect(tableColStrings).to.deep.equal(['ColA', 'ColB', 'ColC', 'ColD']);
// first data row
const firstDataCells = await tablePreview.findElements(
By.xpath('tbody/tr[1]/td')
);
const firstDataCellStrings = await firstDataCells.reduce(
async (acc, col) => [...(await acc), await col.getText()],
Promise.resolve([] as string[])
);
expect(firstDataCellStrings).to.deep.equal([
'AAAA',
'BBBB',
'CCCC',
'DDDD'
]);
});
it('allows toggling header row', async () => {
const headersCheckbox = await getDriver().findElement(
By.xpath(
'//label[contains(., "Data has headers")]/input[@type="checkbox"]'
)
);
await headersCheckbox.click();
// ensure there are no headers now
const tablePreview = await getDriver().findElement(By.xpath('//table'));
const tableCols = await tablePreview.findElements(
By.xpath('thead/tr/th')
);
expect(tableCols.length).to.equal(0);
// first data row should now show the header strings
const firstDataCells = await tablePreview.findElements(
By.xpath('tbody/tr[1]/td')
);
const firstDataCellStrings = await firstDataCells.reduce(
async (acc, col) => [...(await acc), await col.getText()],
Promise.resolve([] as string[])
);
expect(firstDataCellStrings).to.deep.equal([
'ColA',
'ColB',
'ColC',
'ColD'
]);
});
describe('with preview accepted', () => {
beforeEach(async () => {
const nextButton = await getDriver().findElement(
By.xpath('//button[text() = "Choose columns"]')
);
await nextButton.click();
await getDriver().wait(
until.elementLocated(By.xpath('//*[contains(., "Select Columns")]')),
300 // extra time
);
});
it('shows selection prompt under active focus for screen reader', async () => {
const focusedHeading = await getDriver().switchTo().activeElement();
expect(await focusedHeading.getText()).to.equal('Select Columns');
});
it('shows target fields', async () => {
const targetFields = await getDriver().findElements(
By.xpath('//section[@aria-label = "Target fields"]/section')
);
expect(targetFields.length).to.equal(2);
expect(await targetFields[0].getAttribute('aria-label')).to.equal(
'Field A (required)'
);
expect(await targetFields[1].getAttribute('aria-label')).to.equal(
'Field B (optional)'
);
});
it('does not allow to proceed without assignment', async () => {
const nextButton = await getDriver().findElement(
By.xpath('//button[text() = "Import"]')
);
await nextButton.click();
await getDriver().wait(
until.elementLocated(
By.xpath('//*[contains(., "Please assign all required fields")]')
),
300 // extra time
);
});
it('offers keyboard-only select start buttons', async () => {
const selectButtons = await getDriver().findElements(
By.xpath('//button[@aria-label = "Select column for assignment"]')
);
expect(selectButtons.length).to.equal(4);
});
describe('with assigned field', () => {
beforeEach(async () => {
// start the keyboard-based selection mode
const focusedHeading = await getDriver().switchTo().activeElement();
await focusedHeading.sendKeys('\t'); // tab to next element
const selectButton = await getDriver().findElement(
By.xpath(
'//button[@aria-label = "Select column for assignment"][1]'
)
);
await selectButton.sendKeys('\n'); // cannot use click
await getDriver().wait(
until.elementLocated(
By.xpath('//*[contains(., "Assigning column A")]')
),
200
);
const assignButton = await getDriver().findElement(
By.xpath('//button[@aria-label = "Assign column A"]')
);
await assignButton.click();
});
describe('with confirmation to start processing', () => {
beforeEach(async () => {
const nextButton = await getDriver().findElement(
By.xpath('//button[text() = "Import"]')
);
await nextButton.click();
await getDriver().wait(
until.elementLocated(
By.xpath(
'//button[@aria-label = "Go to previous step"]/../*[contains(., "Import")]'
)
),
200
);
});
it('sets focus on next heading', async () => {
const focusedHeading = await getDriver().switchTo().activeElement();
expect(await focusedHeading.getText()).to.equal('Import');
});
it('does not finish until dataHandler returns', async () => {
await getDriver().sleep(300);
const focusedHeading = await getDriver().switchTo().activeElement();
expect(await focusedHeading.getText()).to.equal('Import');
});
describe('after dataHandler is complete', () => {
beforeEach(async () => {
await getDriver().executeScript(
'window.TEST_DATA_HANDLER_RESOLVE()'
);
await getDriver().wait(
until.elementLocated(By.xpath('//*[contains(., "Complete")]')),
200
);
});
it('has active focus on completion message', async () => {
const focusedHeading = await getDriver()
.switchTo()
.activeElement();
expect(await focusedHeading.getText()).to.equal('Complete');
});
it('produces parsed data with correct fields', async () => {
const parsedData = await getDriver().executeScript(
'return window.TEST_DATA_HANDLER_ROWS'
);
const chunkInfo = await getDriver().executeScript(
'return window.TEST_DATA_HANDLER_INFO'
);
expect(parsedData).to.deep.equal([
{ fieldA: 'AAAA' },
{ fieldA: 'EEEE' }
]);
expect(chunkInfo).to.deep.equal({ startIndex: 0 });
});
it('does not show any interactable buttons', async () => {
const anyButtons = await getDriver().findElements(
By.xpath('//button')
);
expect(anyButtons.length).to.equal(1);
expect(await anyButtons[0].getAttribute('aria-label')).to.equal(
'Go to previous step'
);
expect(await anyButtons[0].getAttribute('disabled')).to.equal(
'true'
);
});
});
});
});
});
});
}).timeout(testTimeoutMs);