Skip to content

Commit 67cc218

Browse files
Run test cases with it.each()
1 parent 1ea2640 commit 67cc218

File tree

1 file changed

+28
-49
lines changed

1 file changed

+28
-49
lines changed

demo/src/utils/wordList.spec.ts

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,48 @@
11
import { parseString, WhitespaceHandling } from './wordList';
22

3-
interface TestCase {
4-
delimiter: string;
5-
inputString: string;
6-
}
7-
83
const { Preserve, TrimLeadingAndTrailing } = WhitespaceHandling;
94

105
describe('parseString', () => {
11-
it('returns an empty array when input string is empty', () => {
12-
function testInput(input: string): void {
13-
const wordList = parseString(input, ',', Preserve);
6+
it.each(['', null, undefined])(
7+
'returns an empty array when input string is %p',
8+
input => {
9+
const wordList = parseString(input as string, ',', Preserve);
1410
expect(wordList).toEqual([]);
1511
}
12+
);
1613

17-
['', null, undefined].forEach(val => testInput(val as string));
18-
});
19-
20-
it('returns entire input string in array when delimiter is empty', () => {
21-
function testDelimiter(delimiter: string): void {
14+
it.each(['', null, undefined])(
15+
'returns entire input string in array when delimiter is %p',
16+
testDelimiter => {
17+
const delimiter = testDelimiter as string;
2218
const wordList = parseString(' some input string ', delimiter, Preserve);
2319
expect(wordList).toEqual([' some input string ']);
2420
}
21+
);
2522

26-
['', null, undefined].forEach(val => testDelimiter(val as string));
27-
});
28-
29-
it('returns entire input string in array when delimiter is not present in input string', () => {
30-
function testDelimiter(delimiter: string): void {
23+
it.each([',', '\n', '\t'])(
24+
'returns entire input string in array when delimiter %p is not present in input string',
25+
delimiter => {
3126
const wordList = parseString(' some input string ', delimiter, Preserve);
3227
expect(wordList).toEqual([' some input string ']);
3328
}
34-
35-
[',', '\n', '\t'].forEach(testDelimiter);
36-
});
37-
38-
it('splits input string by delimiter', () => {
39-
const expectedResult = ['foo', 'Bar', ' b@z '];
40-
const testCases: TestCase[] = [
41-
{ delimiter: ',', inputString: 'foo,Bar, b@z ' },
42-
{ delimiter: '\t', inputString: 'foo Bar b@z ' },
43-
{ delimiter: 'aaa', inputString: 'fooaaaBaraaa b@z ' },
44-
];
45-
46-
function testSplit({ delimiter, inputString }: TestCase): void {
47-
const wordList = parseString(inputString, delimiter, Preserve);
48-
expect(wordList).toEqual(expectedResult);
49-
}
50-
51-
testCases.forEach(testSplit);
29+
);
30+
31+
it.each([
32+
['foo,Bar, b@z ', ','],
33+
['foo Bar b@z ', '\t'],
34+
['fooaaaBaraaa b@z ', 'aaa'],
35+
])('splits input string %p by delimiter %p', (inputString, delimiter) => {
36+
const wordList = parseString(inputString, delimiter, Preserve);
37+
expect(wordList).toEqual(['foo', 'Bar', ' b@z ']);
5238
});
5339

54-
it('splits input string by whitespace delimiter', () => {
55-
const expectedResult = ['foo', 'Bar', 'b@z', ''];
56-
const testCases: TestCase[] = [
57-
{ delimiter: ' ', inputString: 'foo Bar b@z ' },
58-
{ delimiter: ' ', inputString: 'foo Bar b@z ' },
59-
];
60-
61-
function testSplit({ delimiter, inputString }: TestCase): void {
62-
const wordList = parseString(inputString, delimiter, Preserve);
63-
expect(wordList).toEqual(expectedResult);
64-
}
65-
66-
testCases.forEach(testSplit);
40+
it.each([
41+
['foo Bar b@z ', ' '],
42+
['foo Bar b@z ', ' '],
43+
])('splits input string %p by delimiter %p', (inputString, delimiter) => {
44+
const wordList = parseString(inputString, delimiter, Preserve);
45+
expect(wordList).toEqual(['foo', 'Bar', 'b@z', '']);
6746
});
6847

6948
it('splits multiline input string by newline delimiter', () => {

0 commit comments

Comments
 (0)