Skip to content

Commit 2d27ddf

Browse files
committed
Ensure all it() calls are in describe() calls
1 parent 23b74ad commit 2d27ddf

File tree

3 files changed

+241
-202
lines changed

3 files changed

+241
-202
lines changed
Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
1-
import {expect, it} from 'vitest'
1+
import {describe, expect, it} from 'vitest'
22

33
import {insert, move, push, remove, swap} from '../src/array-utils.js'
44

55
const [A, B, C, D] = ['a', 'b', 'c', 'd']
66

7-
it('push', () => {
8-
expect(push([A, B, C], D)).toStrictEqual([A, B, C, D])
9-
})
7+
describe('array utils', () => {
8+
describe('push', () => {
9+
it('works', () => {
10+
expect(push([A, B, C], D)).toStrictEqual([A, B, C, D])
11+
})
12+
})
1013

11-
it('insert', () => {
12-
expect(insert([A, B, C], D, -1)).toStrictEqual([A, B, C])
13-
expect(insert([A, B, C], D, 0)).toStrictEqual([D, A, B, C])
14-
expect(insert([A, B, C], D, 1)).toStrictEqual([A, D, B, C])
15-
expect(insert([A, B, C], D, 2)).toStrictEqual([A, B, D, C])
16-
expect(insert([A, B, C], D, 3)).toStrictEqual([A, B, C, D])
17-
expect(insert([A, B, C], D, 4)).toStrictEqual([A, B, C])
18-
})
14+
describe('insert', () => {
15+
it('works', () => {
16+
expect(insert([A, B, C], D, -1)).toStrictEqual([A, B, C])
17+
expect(insert([A, B, C], D, 0)).toStrictEqual([D, A, B, C])
18+
expect(insert([A, B, C], D, 1)).toStrictEqual([A, D, B, C])
19+
expect(insert([A, B, C], D, 2)).toStrictEqual([A, B, D, C])
20+
expect(insert([A, B, C], D, 3)).toStrictEqual([A, B, C, D])
21+
expect(insert([A, B, C], D, 4)).toStrictEqual([A, B, C])
22+
})
23+
})
1924

20-
it('remove', () => {
21-
expect(remove([A, B, C], 0)).toStrictEqual([B, C])
22-
expect(remove([A, B, C], 1)).toStrictEqual([A, C])
23-
expect(remove([A, B, C], 2)).toStrictEqual([A, B])
24-
})
25+
describe('remove', () => {
26+
it('works', () => {
27+
expect(remove([A, B, C], 0)).toStrictEqual([B, C])
28+
expect(remove([A, B, C], 1)).toStrictEqual([A, C])
29+
expect(remove([A, B, C], 2)).toStrictEqual([A, B])
30+
})
31+
})
2532

26-
it('move', () => {
27-
expect(move([A, B, C], 0, 1)).toStrictEqual([B, A, C])
28-
expect(move([A, B, C], 1, 2)).toStrictEqual([A, C, B])
29-
expect(move([A, B, C], 0, 2)).toStrictEqual([B, C, A])
30-
expect(move([A, B, C], 2, 1)).toStrictEqual([A, C, B])
31-
expect(move([A, B, C], 2, 0)).toStrictEqual([C, A, B])
32-
})
33+
describe('move', () => {
34+
it('works', () => {
35+
expect(move([A, B, C], 0, 1)).toStrictEqual([B, A, C])
36+
expect(move([A, B, C], 1, 2)).toStrictEqual([A, C, B])
37+
expect(move([A, B, C], 0, 2)).toStrictEqual([B, C, A])
38+
expect(move([A, B, C], 2, 1)).toStrictEqual([A, C, B])
39+
expect(move([A, B, C], 2, 0)).toStrictEqual([C, A, B])
40+
})
41+
})
3342

34-
it('swap', () => {
35-
expect(swap([A, B, C], 0, 1)).toStrictEqual([B, A, C])
36-
expect(swap([A, B, C], 1, 2)).toStrictEqual([A, C, B])
37-
expect(swap([A, B, C], 0, 2)).toStrictEqual([C, B, A])
38-
expect(swap([A, B, C], 2, 1)).toStrictEqual([A, C, B])
39-
expect(swap([A, B, C], 2, 0)).toStrictEqual([C, B, A])
43+
describe('swap', () => {
44+
it('works', () => {
45+
expect(swap([A, B, C], 0, 1)).toStrictEqual([B, A, C])
46+
expect(swap([A, B, C], 1, 2)).toStrictEqual([A, C, B])
47+
expect(swap([A, B, C], 0, 2)).toStrictEqual([C, B, A])
48+
expect(swap([A, B, C], 2, 1)).toStrictEqual([A, C, B])
49+
expect(swap([A, B, C], 2, 0)).toStrictEqual([C, B, A])
50+
})
51+
})
4052
})

packages/core/tests/form.test.ts

Lines changed: 123 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,141 @@
1-
import {expect, it, vi} from 'vitest'
1+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
4+
import {describe, expect, it, vi} from 'vitest'
25

36
import {Formulier} from '../src/form.js'
47
import {getPath} from '../src/state-utils.js'
58

69
const INITIAL_VALUES = {a: {b: {c: 'c', d: 'd'}}}
710

8-
it('copies initial values', () => {
9-
const form = new Formulier({initialValues: INITIAL_VALUES})
10-
expect(form.store.getState().values).toBe(INITIAL_VALUES)
11-
})
11+
describe('form', () => {
12+
it('copies initial values', () => {
13+
const form = new Formulier({initialValues: INITIAL_VALUES})
14+
expect(form.store.getState().values).toBe(INITIAL_VALUES)
15+
})
1216

13-
it('calls listeners', () => {
14-
const form = new Formulier({initialValues: INITIAL_VALUES})
15-
const listener = vi.fn(() => undefined)
16-
form.store.subscribe(listener)
17-
form.setFieldValue('a.b.c', 'd')
18-
expect(listener).toHaveBeenCalledTimes(1)
19-
})
17+
it('calls listeners', () => {
18+
const form = new Formulier({initialValues: INITIAL_VALUES})
19+
const listener = vi.fn(() => undefined)
20+
form.store.subscribe(listener)
21+
form.setFieldValue('a.b.c', 'd')
22+
expect(listener).toHaveBeenCalledTimes(1)
23+
})
2024

21-
it('removes listeners', () => {
22-
const form = new Formulier({initialValues: INITIAL_VALUES})
23-
const listener = vi.fn(() => undefined)
24-
const cancel = form.store.subscribe(listener)
25-
cancel()
26-
form.setFieldValue('a.b.c', 'd')
27-
expect(listener).toHaveBeenCalledTimes(0)
28-
expect(form.store['listeners'].size).toBe(0)
29-
})
25+
it('removes listeners', () => {
26+
const form = new Formulier({initialValues: INITIAL_VALUES})
27+
const listener = vi.fn(() => undefined)
28+
const cancel = form.store.subscribe(listener)
29+
cancel()
30+
form.setFieldValue('a.b.c', 'd')
31+
expect(listener).toHaveBeenCalledTimes(0)
32+
expect(form.store['listeners'].size).toBe(0)
33+
})
3034

31-
it('touches fields', () => {
32-
const form = new Formulier({initialValues: INITIAL_VALUES})
33-
expect(form.store.getState().touched['a.b.c']).toBe(undefined)
34-
form.touchField('a.b.c')
35-
expect(form.store.getState().touched['a.b.c']).toBe(true)
36-
})
35+
it('touches fields', () => {
36+
const form = new Formulier({initialValues: INITIAL_VALUES})
37+
expect(form.store.getState().touched['a.b.c']).toBe(undefined)
38+
form.touchField('a.b.c')
39+
expect(form.store.getState().touched['a.b.c']).toBe(true)
40+
})
3741

38-
it('increments submit count', () => {
39-
const form = new Formulier({initialValues: INITIAL_VALUES})
40-
expect(form.store.getState().submitCount).toBe(0)
41-
form.incrementSubmitCount()
42-
form.incrementSubmitCount()
43-
expect(form.store.getState().submitCount).toBe(2)
44-
})
42+
it('increments submit count', () => {
43+
const form = new Formulier({initialValues: INITIAL_VALUES})
44+
expect(form.store.getState().submitCount).toBe(0)
45+
form.incrementSubmitCount()
46+
form.incrementSubmitCount()
47+
expect(form.store.getState().submitCount).toBe(2)
48+
})
4549

46-
it('should not call listeners if setFieldValue does nothing', () => {
47-
const form = new Formulier({initialValues: INITIAL_VALUES})
48-
const listener = vi.fn(() => undefined)
49-
form.store.subscribe(listener)
50-
expect(listener).toHaveBeenCalledTimes(0)
51-
form.setFieldValue('a.b.c', 'd')
52-
expect(listener).toHaveBeenCalledTimes(1)
53-
form.setFieldValue('a.b.c', 'd')
54-
expect(listener).toHaveBeenCalledTimes(1)
55-
})
50+
it('should not call listeners if setFieldValue does nothing', () => {
51+
const form = new Formulier({initialValues: INITIAL_VALUES})
52+
const listener = vi.fn(() => undefined)
53+
form.store.subscribe(listener)
54+
expect(listener).toHaveBeenCalledTimes(0)
55+
form.setFieldValue('a.b.c', 'd')
56+
expect(listener).toHaveBeenCalledTimes(1)
57+
form.setFieldValue('a.b.c', 'd')
58+
expect(listener).toHaveBeenCalledTimes(1)
59+
})
5660

57-
it('should not call listeners if touchField does nothing', () => {
58-
const form = new Formulier({initialValues: INITIAL_VALUES})
59-
const listener = vi.fn(() => undefined)
60-
form.store.subscribe(listener)
61-
expect(listener).toHaveBeenCalledTimes(0)
62-
form.touchField('a.b.c')
63-
expect(listener).toHaveBeenCalledTimes(1)
64-
form.touchField('a.b.c')
65-
expect(listener).toHaveBeenCalledTimes(1)
66-
})
61+
it('should not call listeners if touchField does nothing', () => {
62+
const form = new Formulier({initialValues: INITIAL_VALUES})
63+
const listener = vi.fn(() => undefined)
64+
form.store.subscribe(listener)
65+
expect(listener).toHaveBeenCalledTimes(0)
66+
form.touchField('a.b.c')
67+
expect(listener).toHaveBeenCalledTimes(1)
68+
form.touchField('a.b.c')
69+
expect(listener).toHaveBeenCalledTimes(1)
70+
})
6771

68-
it('registers and unregisters fields', () => {
69-
const form = new Formulier({initialValues: INITIAL_VALUES})
70-
const validator = () => null
71-
expect((form.store.getState().values as any).b).toBe(undefined)
72-
expect(form.store.getState().touched['b']).toBe(undefined)
73-
expect(form.store.getState().errors['b']).toBe(undefined)
74-
expect(form.store.getState().validators['b']).toBe(undefined)
75-
const unregisterB = form.registerField('b', validator)
76-
const unregisterC = form.registerField('c', undefined)
77-
expect((form.store.getState().values as any).b).toBe(null)
78-
expect(form.store.getState().touched['b']).toBe(false)
79-
expect(form.store.getState().errors['b']).toBe(null)
80-
expect(form.store.getState().validators['b']).toBe(validator)
81-
expect(form.store.getState().validators['c']).toBe(null)
82-
form.setFieldValue('b', 'value')
83-
form.setFieldValue('c', 'value')
84-
expect(getPath(form.store.getState().values, 'b')).toBe('value')
85-
form.registerField('b', validator)
86-
form.registerField('c', undefined)
87-
expect(getPath(form.store.getState().values, 'b')).toBe('value')
88-
unregisterB()
89-
unregisterC()
90-
expect((form.store.getState().values as any).b).toBe(undefined)
91-
expect(form.store.getState().touched['b']).toBe(undefined)
92-
expect(form.store.getState().errors['b']).toBe(undefined)
93-
expect(form.store.getState().validators['b']).toBe(undefined)
94-
expect(form.store.getState().validators['c']).toBe(undefined)
95-
})
72+
it('registers and unregisters fields', () => {
73+
const form = new Formulier({initialValues: INITIAL_VALUES})
74+
const validator = () => null
75+
expect((form.store.getState().values as any).b).toBe(undefined)
76+
expect(form.store.getState().touched['b']).toBe(undefined)
77+
expect(form.store.getState().errors['b']).toBe(undefined)
78+
expect(form.store.getState().validators['b']).toBe(undefined)
79+
const unregisterB = form.registerField('b', validator)
80+
const unregisterC = form.registerField('c', undefined)
81+
expect((form.store.getState().values as any).b).toBe(null)
82+
expect(form.store.getState().touched['b']).toBe(false)
83+
expect(form.store.getState().errors['b']).toBe(null)
84+
expect(form.store.getState().validators['b']).toBe(validator)
85+
expect(form.store.getState().validators['c']).toBe(null)
86+
form.setFieldValue('b', 'value')
87+
form.setFieldValue('c', 'value')
88+
expect(getPath(form.store.getState().values, 'b')).toBe('value')
89+
form.registerField('b', validator)
90+
form.registerField('c', undefined)
91+
expect(getPath(form.store.getState().values, 'b')).toBe('value')
92+
unregisterB()
93+
unregisterC()
94+
expect((form.store.getState().values as any).b).toBe(undefined)
95+
expect(form.store.getState().touched['b']).toBe(undefined)
96+
expect(form.store.getState().errors['b']).toBe(undefined)
97+
expect(form.store.getState().validators['b']).toBe(undefined)
98+
expect(form.store.getState().validators['c']).toBe(undefined)
99+
})
96100

97-
it('validates field', () => {
98-
const form = new Formulier({initialValues: INITIAL_VALUES})
99-
const validator = (value: unknown) => (value === 1 ? null : 'Error!')
100-
form.registerField('a.b.c', validator)
101-
expect(form.validateField('a.b.c')).toBe(false)
102-
expect(form.store.getState().errors['a.b.c']).toBe('Error!')
103-
form.setFieldValue('a.b.c', 1)
104-
expect(form.validateField('a.b.c')).toBe(true)
105-
expect(form.store.getState().errors['a.b.c']).toBe(null)
106-
})
101+
it('validates field', () => {
102+
const form = new Formulier({initialValues: INITIAL_VALUES})
103+
const validator = (value: unknown) => (value === 1 ? null : 'Error!')
104+
form.registerField('a.b.c', validator)
105+
expect(form.validateField('a.b.c')).toBe(false)
106+
expect(form.store.getState().errors['a.b.c']).toBe('Error!')
107+
form.setFieldValue('a.b.c', 1)
108+
expect(form.validateField('a.b.c')).toBe(true)
109+
expect(form.store.getState().errors['a.b.c']).toBe(null)
110+
})
107111

108-
it('validates fields', () => {
109-
const form = new Formulier({initialValues: INITIAL_VALUES})
110-
const validator = (value: unknown) => (value === 1 ? null : 'Error!')
111-
form.registerField('a.b.c', validator)
112-
form.registerField('a.b.d', validator)
113-
form.setFieldValue('a.b.c', 1)
114-
expect(form.validateFields()).toBe(false)
115-
expect(form.store.getState().errors['a.b.c']).toBe(null)
116-
expect(form.store.getState().errors['a.b.d']).toBe('Error!')
117-
form.setFieldValue('a.b.d', 1)
118-
expect(form.validateFields()).toBe(true)
119-
expect(form.store.getState().errors['a.b.c']).toBe(null)
120-
expect(form.store.getState().errors['a.b.d']).toBe(null)
121-
})
112+
it('validates fields', () => {
113+
const form = new Formulier({initialValues: INITIAL_VALUES})
114+
const validator = (value: unknown) => (value === 1 ? null : 'Error!')
115+
form.registerField('a.b.c', validator)
116+
form.registerField('a.b.d', validator)
117+
form.setFieldValue('a.b.c', 1)
118+
expect(form.validateFields()).toBe(false)
119+
expect(form.store.getState().errors['a.b.c']).toBe(null)
120+
expect(form.store.getState().errors['a.b.d']).toBe('Error!')
121+
form.setFieldValue('a.b.d', 1)
122+
expect(form.validateFields()).toBe(true)
123+
expect(form.store.getState().errors['a.b.c']).toBe(null)
124+
expect(form.store.getState().errors['a.b.d']).toBe(null)
125+
})
122126

123-
it('skips calling listeners when batching', () => {
124-
const form = new Formulier({initialValues: INITIAL_VALUES})
125-
const listener = vi.fn(() => undefined)
126-
form.store.subscribe(listener)
127-
expect(listener).toHaveBeenCalledTimes(0)
128-
form.store.setState(state => ({...state, errors: {'a.b.c': 'Error 1'}}))
129-
form.store.setState(state => ({...state, errors: {'a.b.c': 'Error 2'}}))
130-
expect(listener).toHaveBeenCalledTimes(2)
131-
form.store.batch(() => {
132-
form.store.setState(state => ({...state, errors: {'a.b.c': 'Error 3'}}))
133-
form.store.setState(state => ({...state, errors: {'a.b.c': 'Error 4'}}))
127+
it('skips calling listeners when batching', () => {
128+
const form = new Formulier({initialValues: INITIAL_VALUES})
129+
const listener = vi.fn(() => undefined)
130+
form.store.subscribe(listener)
131+
expect(listener).toHaveBeenCalledTimes(0)
132+
form.store.setState(state => ({...state, errors: {'a.b.c': 'Error 1'}}))
133+
form.store.setState(state => ({...state, errors: {'a.b.c': 'Error 2'}}))
134+
expect(listener).toHaveBeenCalledTimes(2)
135+
form.store.batch(() => {
136+
form.store.setState(state => ({...state, errors: {'a.b.c': 'Error 3'}}))
137+
form.store.setState(state => ({...state, errors: {'a.b.c': 'Error 4'}}))
138+
})
139+
expect(listener).toHaveBeenCalledTimes(3)
134140
})
135-
expect(listener).toHaveBeenCalledTimes(3)
136141
})

0 commit comments

Comments
 (0)