Skip to content

Commit ab151ec

Browse files
author
Harry Whorlow
committed
chore: test tweaks
1 parent 59bc26f commit ab151ec

File tree

1 file changed

+16
-51
lines changed

1 file changed

+16
-51
lines changed

packages/form-core/tests/FieldApi.spec.ts

+16-51
Original file line numberDiff line numberDiff line change
@@ -1156,81 +1156,46 @@ describe('field api', () => {
11561156
expect(form.getFieldValue('greet')).toStrictEqual('hello baz')
11571157
})
11581158

1159-
it('should listen to array field changes', () => {
1159+
it('should run the onChange listener when the field array is changed', () => {
11601160
const form = new FormApi({
11611161
defaultValues: {
1162-
items: ['first item', 'second item'],
1162+
items: ['one', 'two'],
11631163
itemsCount: 2,
11641164
itemsModified: false,
11651165
},
11661166
})
1167-
11681167
form.mount()
11691168

1169+
let arr!: string[]
1170+
11701171
const field = new FieldApi({
11711172
form,
11721173
name: 'items',
11731174
listeners: {
11741175
onChange: ({ value }) => {
1175-
form.setFieldValue('itemsCount', value.length)
1176-
form.setFieldValue('itemsModified', true)
1176+
arr = value
11771177
},
11781178
},
11791179
})
1180-
11811180
field.mount()
11821181

1183-
type CheckFieldValuesArgs = {
1184-
itemsCount: number
1185-
items: string[]
1186-
}
1187-
1188-
const checkFieldValuesAndResetForNextTest = ({
1189-
itemsCount,
1190-
items,
1191-
}: CheckFieldValuesArgs) => {
1192-
expect(form.getFieldValue('itemsCount')).toStrictEqual(itemsCount)
1193-
expect(form.getFieldValue('items')).toStrictEqual(items)
1194-
expect(form.getFieldValue('itemsModified')).toStrictEqual(true)
1195-
1196-
form.setFieldValue('itemsModified', false)
1197-
}
1198-
1199-
field.pushValue('third item')
1200-
checkFieldValuesAndResetForNextTest({
1201-
itemsCount: 3,
1202-
items: ['first item', 'second item', 'third item'],
1203-
})
1182+
field.removeValue(1)
1183+
expect(arr).toStrictEqual(['one'])
12041184

1205-
field.insertValue(1, 'middle item')
1206-
checkFieldValuesAndResetForNextTest({
1207-
itemsCount: 4,
1208-
items: ['first item', 'middle item', 'second item', 'third item'],
1209-
})
1185+
field.replaceValue(0, 'start')
1186+
expect(arr).toStrictEqual(['start'])
12101187

1211-
field.replaceValue(0, 'replaced item')
1212-
checkFieldValuesAndResetForNextTest({
1213-
itemsCount: 4,
1214-
items: ['replaced item', 'middle item', 'second item', 'third item'],
1215-
})
1188+
field.pushValue('end')
1189+
expect(arr).toStrictEqual(['start', 'end'])
12161190

1217-
field.removeValue(1)
1218-
checkFieldValuesAndResetForNextTest({
1219-
itemsCount: 3,
1220-
items: ['replaced item', 'second item', 'third item'],
1221-
})
1191+
field.insertValue(1, 'middle')
1192+
expect(arr).toStrictEqual(['start', 'middle', 'end'])
12221193

12231194
field.swapValues(0, 2)
1224-
checkFieldValuesAndResetForNextTest({
1225-
itemsCount: 3,
1226-
items: ['third item', 'second item', 'replaced item'],
1227-
})
1195+
expect(arr).toStrictEqual(['end', 'middle', 'start'])
12281196

1229-
field.moveValue(0, 2)
1230-
checkFieldValuesAndResetForNextTest({
1231-
itemsCount: 3,
1232-
items: ['second item', 'replaced item', 'third item'],
1233-
})
1197+
field.moveValue(0, 1)
1198+
expect(arr).toStrictEqual(['middle', 'end', 'start'])
12341199
})
12351200

12361201
it('should reset the form on a listener', () => {

0 commit comments

Comments
 (0)