Skip to content

Commit 4c64bb3

Browse files
test(utilities): refactor forEach to it.each and add more cases
1 parent 6bf3526 commit 4c64bb3

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

test/utilities.test.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,47 @@ const {
88

99
describe('utilities', () => {
1010
describe('camelCase', () => {
11-
[undefined, null, 1337, {}, []].forEach(value => {
12-
it(`throws an error if first argument is ${value}`, () => {
11+
it.each([undefined, null, {}, [], 0, 1, () => {}, new Date()])(
12+
'throws an error if first argument is %p',
13+
input => {
1314
expect(() => {
14-
camelCase(value);
15+
camelCase(input);
1516
}).toThrow(TypeError);
16-
});
17-
});
17+
}
18+
);
1819

19-
it('does not modify string if it does not need to be camelCased', () => {
20-
[
21-
['', ''],
22-
['foo', 'foo'],
23-
['fooBar', 'fooBar'],
24-
['--fooBar', '--fooBar'],
25-
['--foo-bar', '--foo-bar'],
26-
['--foo-100', '--foo-100']
27-
].forEach(testCase => {
28-
expect(camelCase(testCase[0])).toBe(testCase[1]);
29-
});
30-
});
20+
it.each([
21+
['', ''],
22+
['foo', 'foo'],
23+
['fooBar', 'fooBar'],
24+
['--fooBar', '--fooBar'],
25+
['--foo-bar', '--foo-bar'],
26+
['--foo-100', '--foo-100']
27+
])(
28+
'does not modify string if it does not need to be camelCased',
29+
(input, expected) => {
30+
expect(camelCase(input)).toBe(expected);
31+
}
32+
);
3133

32-
it('camelCases a string', () => {
33-
[
34-
['foo-bar', 'fooBar'],
35-
['foo-bar-baz', 'fooBarBaz'],
36-
['CAMEL-CASE', 'camelCase']
37-
].forEach(testCase => {
38-
expect(camelCase(testCase[0])).toBe(testCase[1]);
39-
});
34+
it.each([
35+
['foo-bar', 'fooBar'],
36+
['foo-bar-baz', 'fooBarBaz'],
37+
['CAMEL-CASE', 'camelCase']
38+
])('camelCases a string', (input, expected) => {
39+
expect(camelCase(input)).toBe(expected);
4040
});
4141
});
4242

4343
describe('invertObject', () => {
44-
[undefined, null, 'foo', 1337].forEach(value => {
45-
it(`throws an error if the first argument is ${value}`, () => {
44+
it.each([undefined, null, 'string', 0, 1, () => {}])(
45+
`throws an error if the first argument is %p`,
46+
input => {
4647
expect(() => {
47-
invertObject(value);
48+
invertObject(input);
4849
}).toThrow(TypeError);
49-
});
50-
});
50+
}
51+
);
5152

5253
it('swaps key with value', () => {
5354
expect(

0 commit comments

Comments
 (0)