-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathminDigitChars.test.ts
More file actions
98 lines (89 loc) 路 2.64 KB
/
Copy pathminDigitChars.test.ts
File metadata and controls
98 lines (89 loc) 路 2.64 KB
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
import { describe, expect, test } from 'vitest';
import type { StringIssue } from '../../schemas/index.ts';
import { _getDigitCount } from '../../utils/index.ts';
import { expectActionIssue, expectNoActionIssue } from '../../vitest/index.ts';
import {
minDigitChars,
type MinDigitCharsAction,
type MinDigitCharsIssue,
} from './minDigitChars.ts';
describe('minDigitChars', () => {
describe('should return action object', () => {
const baseAction: Omit<MinDigitCharsAction<string, 5, never>, 'message'> = {
kind: 'validation',
type: 'min_digits',
reference: minDigitChars,
expects: '>=5',
requirement: 5,
async: false,
'~run': expect.any(Function),
};
test('with undefined message', () => {
const action: MinDigitCharsAction<string, 5, undefined> = {
...baseAction,
message: undefined,
};
expect(minDigitChars(5)).toStrictEqual(action);
expect(minDigitChars(5, undefined)).toStrictEqual(action);
});
test('with string message', () => {
expect(minDigitChars(5, 'message')).toStrictEqual({
...baseAction,
message: 'message',
} satisfies MinDigitCharsAction<string, 5, string>);
});
test('with function message', () => {
const message = () => 'message';
expect(minDigitChars(5, message)).toStrictEqual({
...baseAction,
message,
} satisfies MinDigitCharsAction<string, 5, typeof message>);
});
});
describe('should return dataset without issues', () => {
const action = minDigitChars(5);
test('for untyped inputs', () => {
const issues: [StringIssue] = [
{
kind: 'schema',
type: 'string',
input: null,
expected: 'string',
received: 'null',
message: 'message',
},
];
expect(
action['~run']({ typed: false, value: null, issues }, {})
).toStrictEqual({
typed: false,
value: null,
issues,
});
});
test('for valid strings', () => {
expectNoActionIssue(action, ['12345', '123456', '45foobarbaz123']);
});
});
describe('should return dataset with issues', () => {
const action = minDigitChars(5, 'message');
const baseIssue: Omit<
MinDigitCharsIssue<string, 5>,
'input' | 'received'
> = {
kind: 'validation',
type: 'min_digits',
expected: '>=5',
message: 'message',
requirement: 5,
};
test('for invalid strings', () => {
expectActionIssue(
action,
baseIssue,
['', 'foo', 'abc1234'],
(value) => `${_getDigitCount(value)}`
);
});
});
});