/
VestTest.test.js
128 lines (109 loc) · 3.34 KB
/
VestTest.test.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import runCreateRef from '../../../../../testUtils/runCreateRef';
import VestTest from 'VestTest';
import addTestToState from 'addTestToState';
import context from 'ctx';
import { setPending } from 'pending';
import usePending from 'usePending';
import useTestObjects from 'useTestObjects';
const fieldName = 'unicycle';
const statement = 'I am Root.';
let stateRef;
it.ctx = (str, cb) => it(str, () => context.run({ stateRef }, cb));
describe('VestTest', () => {
let testObject;
beforeEach(() => {
testObject = new VestTest({
fieldName,
statement,
testFn: jest.fn(),
});
});
test('TestObject constructor', () => {
expect(testObject).toMatchSnapshot();
});
it('Should have a unique id', () => {
Array.from(
{ length: 100 },
() => new VestTest({ fieldName, statement, testFn: jest.fn() })
).reduce((existing, { id }) => {
expect(existing[id]).toBeUndefined();
existing[id] = true;
return existing;
}, {});
});
describe('testObject.warn', () => {
it('Should set `.isWarning` to true', () => {
expect(testObject.isWarning).toBe(false);
testObject.warn();
expect(testObject.isWarning).toBe(true);
expect(testObject).toMatchSnapshot();
});
});
describe('testObject.fail', () => {
beforeEach(() => {
jest.resetModules();
const VestTest = require('VestTest');
testObject = new VestTest({
fieldName,
statement,
testFn: jest.fn(),
});
});
afterEach(() => {
jest.resetAllMocks();
});
it('Should set this.failed to true', () => {
expect(testObject.failed).toBe(false);
testObject.fail();
expect(testObject.failed).toBe(true);
});
});
describe('testobject.valueOf', () => {
test('When `failed` is false', () => {
expect(testObject.failed).toBe(false);
expect(testObject.valueOf()).toBe(true);
});
test('When `failed` is true', () => {
testObject.failed = true;
expect(testObject.valueOf()).toBe(false);
});
});
describe('testObject.cancel', () => {
beforeEach(() => {
stateRef = runCreateRef();
context.run({ stateRef }, () => {
addTestToState(testObject);
});
});
it.ctx('Should remove a testObject from the state', () => {
const [testObjects] = useTestObjects();
expect(testObjects).toEqual(expect.arrayContaining([testObject]));
testObject.cancel();
expect(testObjects).toEqual(expect.not.arrayContaining([testObject]));
});
it.ctx('Should remove a testObject from the pending state', () => {
setPending(testObject);
{
const [{ pending }] = usePending();
expect(pending).toEqual(expect.arrayContaining([testObject]));
}
testObject.cancel();
{
const [{ pending }] = usePending();
expect(pending).toEqual(expect.not.arrayContaining([testObject]));
}
});
it.ctx('Should remove a testObject from the lagging state', () => {
usePending(state => ({ ...state, lagging: [testObject] }));
{
const [{ lagging }] = usePending();
expect(lagging).toEqual(expect.arrayContaining([testObject]));
}
testObject.cancel();
{
const [{ lagging }] = usePending();
expect(lagging).toEqual(expect.not.arrayContaining([testObject]));
}
});
});
});