Skip to content

Commit 5b3b1f6

Browse files
test: update tests for utils
1 parent a3b7f21 commit 5b3b1f6

File tree

2 files changed

+66
-64
lines changed

2 files changed

+66
-64
lines changed

src/utils.test.ts

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,89 @@ import { Case, Level } from '@commitlint/load';
22
import { green, red } from 'chalk';
33
import { getLongest, pipeWith, valueFromRule, wordCase, maxLengthTransformerFactory } from './utils';
44

5-
describe('getLongest', () => {
6-
test('return longest string length in array', () => {
7-
const result = getLongest(['1', '333', '22']);
5+
describe('utils', () => {
6+
describe('getLongest', () => {
7+
test('return longest string length in array', () => {
8+
const result = getLongest(['1', '333', '22']);
89

9-
expect(result).toBe(3);
10+
expect(result).toBe(3);
11+
});
1012
});
11-
});
1213

13-
describe('pipeWith', () => {
14-
test('pipes value to function', () => {
15-
const result = pipeWith('hello ', x => (x += 'world'));
16-
17-
expect(result).toBe('hello world');
18-
});
14+
describe('pipeWith', () => {
15+
test('pipes value to function', () => {
16+
const result = pipeWith('hello ', x => (x += 'world'));
1917

20-
test('pipes to many functions', () => {
21-
const increment = (x: number) => ++x;
18+
expect(result).toBe('hello world');
19+
});
2220

23-
const result = pipeWith(5, increment, increment, increment);
21+
test('pipes to many functions', () => {
22+
const increment = (x: number) => ++x;
2423

25-
expect(result).toBe(8);
26-
});
27-
});
24+
const result = pipeWith(5, increment, increment, increment);
2825

29-
describe('wordCase', () => {
30-
test.each<[string, Case, string]>([
31-
['FOO_BAR', 'lower-case', 'foo_bar'],
32-
['FOO_BAR', 'lowercase', 'foo_bar'],
33-
['FOO_BAR', 'lowerCase', 'foo_bar'],
34-
['fooBar', 'snake-case', 'foo_bar'],
35-
['fooBar', 'pascal-case', 'FooBar'],
36-
['fooBar', 'sentence-case', 'Foo bar'],
37-
['fooBar', 'sentencecase', 'Foo bar'],
38-
['fooBar', 'start-case', 'Foo Bar'],
39-
['fooBar', 'kebab-case', 'foo-bar'],
40-
['fooBar', 'upper-case', 'FOOBAR'],
41-
['fooBar', 'uppercase', 'FOOBAR'],
42-
['foo_Bar', 'camel-case', 'fooBar']
43-
])('wordCase(%s, %s): %s', (value, rule, expected) => {
44-
const result = wordCase(value, rule);
45-
46-
expect(result).toBe(expected);
26+
expect(result).toBe(8);
27+
});
4728
});
4829

49-
describe('valueFromRule', () => {
50-
test('should return false if rule is undefined', () => {
51-
const result = valueFromRule(undefined);
30+
describe('wordCase', () => {
31+
test.each<[string, Case, string]>([
32+
['FOO_BAR', 'lower-case', 'foo_bar'],
33+
['FOO_BAR', 'lowercase', 'foo_bar'],
34+
['FOO_BAR', 'lowerCase', 'foo_bar'],
35+
['fooBar', 'snake-case', 'foo_bar'],
36+
['fooBar', 'pascal-case', 'FooBar'],
37+
['fooBar', 'sentence-case', 'Foo bar'],
38+
['fooBar', 'sentencecase', 'Foo bar'],
39+
['fooBar', 'start-case', 'Foo Bar'],
40+
['fooBar', 'kebab-case', 'foo-bar'],
41+
['fooBar', 'upper-case', 'FOOBAR'],
42+
['fooBar', 'uppercase', 'FOOBAR'],
43+
['foo_Bar', 'camel-case', 'fooBar']
44+
])('wordCase(%s, %s): %s', (value, rule, expected) => {
45+
const result = wordCase(value, rule);
5246

53-
expect(result).toBe(false);
47+
expect(result).toBe(expected);
5448
});
5549

56-
test('should return false if disabled', () => {
57-
const result = valueFromRule([Level.Disable, 'always', 72]);
50+
describe('valueFromRule', () => {
51+
test('should return false if rule is undefined', () => {
52+
const result = valueFromRule(undefined);
5853

59-
expect(result).toBe(false);
60-
});
54+
expect(result).toBeFalsy();
55+
});
6156

62-
test('should return false if applicable never', () => {
63-
const result = valueFromRule([Level.Error, 'never', 72]);
57+
test('should return false if disabled', () => {
58+
const result = valueFromRule([Level.Disable, 'always', 72]);
6459

65-
expect(result).toBe(false);
66-
});
60+
expect(result).toBeFalsy();
61+
});
62+
63+
test('should return false if applicable never', () => {
64+
const result = valueFromRule([Level.Error, 'never', 72]);
6765

68-
test('should return value of rule', () => {
69-
const result = valueFromRule([Level.Error, 'always', 72]);
66+
expect(result).toBeFalsy();
67+
});
7068

71-
expect(result).toBe(72);
69+
test('should return value of rule', () => {
70+
const result = valueFromRule([Level.Error, 'always', 72]);
71+
72+
expect(result).toBe(72);
73+
});
7274
});
73-
});
7475

75-
describe('maxLengthTransformerFactory', () => {
76-
test.each<[number | undefined, string, string]>([
77-
[3, 'foo', green('(3) foo')],
78-
[3, 'foo bar', red('(7) foo bar')],
79-
[undefined, 'foo', 'foo']
80-
])('should transform when length: %n to expected: %s', (length, value, expected) => {
81-
const factory = maxLengthTransformerFactory(length);
76+
describe('maxLengthTransformerFactory', () => {
77+
test.each<[number | undefined, string, string]>([
78+
[3, 'foo', green('(3) foo')],
79+
[3, 'foo bar', red('(7) foo bar')],
80+
[undefined, 'foo', 'foo']
81+
])('should transform when length: %n to expected: %s', (length, value, expected) => {
82+
const factory = maxLengthTransformerFactory(length);
8283

83-
const result = factory(value);
84+
const result = factory(value);
8485

85-
expect(result).toBe(expected);
86+
expect(result).toBe(expected);
87+
});
8688
});
8789
});
8890
});

src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ export function wordCase(value: string, rule: Case): string {
4141
}
4242
}
4343

44-
export function valueFromRule<T>(rule: Rule<T> | undefined): false | T {
44+
export function valueFromRule<T>(rule: Rule<T> | undefined): undefined | T {
4545
if (rule == null) {
46-
return false;
46+
return undefined;
4747
}
4848

4949
const [level, applicable, value] = rule;
5050

5151
if (level == Level.Disable) {
52-
return false;
52+
return undefined;
5353
}
5454

5555
if (applicable == 'never') {
56-
return false;
56+
return undefined;
5757
}
5858

5959
return value;

0 commit comments

Comments
 (0)