Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 441638d

Browse files
committed
Add test for custom delimiter logic
1 parent 9fe0a4d commit 441638d

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

test/customConfig.test.ts

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { By, until } from 'selenium-webdriver';
2+
import { expect } from 'chai';
3+
import path from 'path';
4+
5+
import { runTestServer } from './testServer';
6+
import { runDriver } from './webdriver';
7+
import { runUI } from './uiSetup';
8+
9+
// extra timeout allowance on CI
10+
const testTimeoutMs = process.env.CI ? 20000 : 10000;
11+
12+
describe('importer with custom Papa Parse config', () => {
13+
const appUrl = runTestServer();
14+
const getDriver = runDriver();
15+
const initUI = runUI(getDriver);
16+
17+
beforeEach(async () => {
18+
await getDriver().get(appUrl);
19+
20+
await initUI((React, ReactDOM, ReactCSVImporter, ReactCSVImporterField) => {
21+
ReactDOM.render(
22+
React.createElement(
23+
ReactCSVImporter,
24+
{
25+
delimiter: '!', // not a normal guessable delimiter for Papa Parse
26+
processChunk: (rows, info) => {
27+
((window as unknown) as Record<
28+
string,
29+
unknown
30+
>).TEST_PROCESS_CHUNK_ROWS = rows;
31+
((window as unknown) as Record<
32+
string,
33+
unknown
34+
>).TEST_PROCESS_CHUNK_INFO = info;
35+
}
36+
},
37+
[
38+
React.createElement(ReactCSVImporterField, {
39+
name: 'fieldA',
40+
label: 'Field A'
41+
}),
42+
React.createElement(ReactCSVImporterField, {
43+
name: 'fieldB',
44+
label: 'Field B',
45+
optional: true
46+
})
47+
]
48+
),
49+
document.getElementById('root')
50+
);
51+
});
52+
});
53+
54+
describe('at preview stage', () => {
55+
beforeEach(async () => {
56+
const filePath = path.resolve(
57+
__dirname,
58+
'./fixtures/customDelimited.txt'
59+
);
60+
61+
const fileInput = await getDriver().findElement(By.xpath('//input'));
62+
await fileInput.sendKeys(filePath);
63+
64+
await getDriver().wait(
65+
until.elementLocated(By.xpath('//*[contains(., "Raw File Contents")]')),
66+
300 // extra time
67+
);
68+
});
69+
70+
it('shows correctly parsed preview table', async () => {
71+
const tablePreview = await getDriver().findElement(By.xpath('//table'));
72+
73+
// header row
74+
const tableCols = await tablePreview.findElements(
75+
By.xpath('thead/tr/th')
76+
);
77+
const tableColStrings = await tableCols.reduce(
78+
async (acc, col) => [...(await acc), await col.getText()],
79+
Promise.resolve([] as string[])
80+
);
81+
expect(tableColStrings).to.deep.equal(['val1', 'val2']);
82+
83+
// first data row
84+
const firstDataCells = await tablePreview.findElements(
85+
By.xpath('tbody/tr[1]/td')
86+
);
87+
const firstDataCellStrings = await firstDataCells.reduce(
88+
async (acc, col) => [...(await acc), await col.getText()],
89+
Promise.resolve([] as string[])
90+
);
91+
expect(firstDataCellStrings).to.deep.equal(['val3', 'val4']);
92+
});
93+
94+
describe('after accepting and assigning fields', () => {
95+
beforeEach(async () => {
96+
const previewNextButton = await getDriver().findElement(
97+
By.xpath('//button[text() = "Next"]')
98+
);
99+
100+
await previewNextButton.click();
101+
102+
await getDriver().wait(
103+
until.elementLocated(By.xpath('//*[contains(., "Select Columns")]')),
104+
300 // extra time
105+
);
106+
107+
// start the keyboard-based selection mode
108+
const focusedHeading = await getDriver().switchTo().activeElement();
109+
await focusedHeading.sendKeys('\t'); // tab to next element
110+
111+
const selectButton = await getDriver().findElement(
112+
By.xpath('//button[@aria-label = "Select column for assignment"][1]')
113+
);
114+
await selectButton.sendKeys('\n'); // cannot use click
115+
116+
await getDriver().wait(
117+
until.elementLocated(
118+
By.xpath('//*[contains(., "Assigning column A")]')
119+
),
120+
200
121+
);
122+
123+
const assignButton = await getDriver().findElement(
124+
By.xpath('//button[@aria-label = "Assign column A"]')
125+
);
126+
await assignButton.click();
127+
128+
const fieldsNextButton = await getDriver().findElement(
129+
By.xpath('//button[text() = "Next"]')
130+
);
131+
132+
await fieldsNextButton.click();
133+
134+
await getDriver().wait(
135+
until.elementLocated(
136+
By.xpath(
137+
'//button[@aria-label = "Go to previous step"]/../*[contains(., "Import")]'
138+
)
139+
),
140+
200
141+
);
142+
143+
await getDriver().wait(
144+
until.elementLocated(By.xpath('//*[contains(., "Complete")]')),
145+
200
146+
);
147+
});
148+
149+
it('produces parsed data with correct fields', async () => {
150+
const parsedData = await getDriver().executeScript(
151+
'return window.TEST_PROCESS_CHUNK_ROWS'
152+
);
153+
const chunkInfo = await getDriver().executeScript(
154+
'return window.TEST_PROCESS_CHUNK_INFO'
155+
);
156+
157+
expect(parsedData).to.deep.equal([{ fieldA: 'val3' }]);
158+
expect(chunkInfo).to.deep.equal({ startIndex: 0 });
159+
});
160+
});
161+
});
162+
}).timeout(testTimeoutMs);

test/fixtures/customDelimited.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val1!val2
2+
val3!val4

0 commit comments

Comments
 (0)