Skip to content

Commit 59bc26f

Browse files
committed
chore: group field array listener tests
1 parent e8f1c32 commit 59bc26f

File tree

1 file changed

+36
-149
lines changed

1 file changed

+36
-149
lines changed

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

+36-149
Original file line numberDiff line numberDiff line change
@@ -1156,11 +1156,12 @@ describe('field api', () => {
11561156
expect(form.getFieldValue('greet')).toStrictEqual('hello baz')
11571157
})
11581158

1159-
it('should change the form state when running listener onChange with pushValue', () => {
1159+
it('should listen to array field changes', () => {
11601160
const form = new FormApi({
11611161
defaultValues: {
11621162
items: ['first item', 'second item'],
11631163
itemsCount: 2,
1164+
itemsModified: false,
11641165
},
11651166
})
11661167

@@ -1172,178 +1173,64 @@ describe('field api', () => {
11721173
listeners: {
11731174
onChange: ({ value }) => {
11741175
form.setFieldValue('itemsCount', value.length)
1176+
form.setFieldValue('itemsModified', true)
11751177
},
11761178
},
11771179
})
11781180

11791181
field.mount()
11801182

1181-
field.pushValue('third item')
1183+
type CheckFieldValuesArgs = {
1184+
itemsCount: number
1185+
items: string[]
1186+
}
11821187

1183-
expect(form.getFieldValue('itemsCount')).toStrictEqual(3)
1184-
expect(form.getFieldValue('items')).toStrictEqual([
1185-
'first item',
1186-
'second item',
1187-
'third item',
1188-
])
1189-
})
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)
11901195

1191-
it('should change the form state when running listener onChange with insertValue', () => {
1192-
const form = new FormApi({
1193-
defaultValues: {
1194-
items: ['first item', 'second item'],
1195-
itemsCount: 2,
1196-
},
1197-
})
1198-
1199-
form.mount()
1196+
form.setFieldValue('itemsModified', false)
1197+
}
12001198

1201-
const field = new FieldApi({
1202-
form,
1203-
name: 'items',
1204-
listeners: {
1205-
onChange: ({ value }) => {
1206-
form.setFieldValue('itemsCount', value.length)
1207-
},
1208-
},
1199+
field.pushValue('third item')
1200+
checkFieldValuesAndResetForNextTest({
1201+
itemsCount: 3,
1202+
items: ['first item', 'second item', 'third item'],
12091203
})
12101204

1211-
field.mount()
1212-
12131205
field.insertValue(1, 'middle item')
1214-
1215-
expect(form.getFieldValue('itemsCount')).toStrictEqual(3)
1216-
expect(form.getFieldValue('items')).toStrictEqual([
1217-
'first item',
1218-
'middle item',
1219-
'second item',
1220-
])
1221-
})
1222-
1223-
it('should change the form state when running listener onChange with replaceValue', () => {
1224-
const form = new FormApi({
1225-
defaultValues: {
1226-
items: ['first item', 'second item'],
1227-
itemsModified: false,
1228-
},
1206+
checkFieldValuesAndResetForNextTest({
1207+
itemsCount: 4,
1208+
items: ['first item', 'middle item', 'second item', 'third item'],
12291209
})
12301210

1231-
form.mount()
1232-
1233-
const field = new FieldApi({
1234-
form,
1235-
name: 'items',
1236-
listeners: {
1237-
onChange: () => {
1238-
form.setFieldValue('itemsModified', true)
1239-
},
1240-
},
1241-
})
1242-
1243-
field.mount()
1244-
12451211
field.replaceValue(0, 'replaced item')
1246-
1247-
expect(form.getFieldValue('itemsModified')).toStrictEqual(true)
1248-
expect(form.getFieldValue('items')).toStrictEqual([
1249-
'replaced item',
1250-
'second item',
1251-
])
1252-
})
1253-
1254-
it('should change the form state when running listener onChange with removeValue', () => {
1255-
const form = new FormApi({
1256-
defaultValues: {
1257-
items: ['first item', 'second item', 'third item'],
1258-
itemsCount: 3,
1259-
},
1212+
checkFieldValuesAndResetForNextTest({
1213+
itemsCount: 4,
1214+
items: ['replaced item', 'middle item', 'second item', 'third item'],
12601215
})
12611216

1262-
form.mount()
1263-
1264-
const field = new FieldApi({
1265-
form,
1266-
name: 'items',
1267-
listeners: {
1268-
onChange: ({ value }) => {
1269-
form.setFieldValue('itemsCount', value.length)
1270-
},
1271-
},
1272-
})
1273-
1274-
field.mount()
1275-
12761217
field.removeValue(1)
1277-
1278-
expect(form.getFieldValue('itemsCount')).toStrictEqual(2)
1279-
expect(form.getFieldValue('items')).toStrictEqual([
1280-
'first item',
1281-
'third item',
1282-
])
1283-
})
1284-
1285-
it('should change the form state when running listener onChange with swapValues', () => {
1286-
const form = new FormApi({
1287-
defaultValues: {
1288-
items: ['first item', 'second item', 'third item'],
1289-
itemsModified: false,
1290-
},
1291-
})
1292-
1293-
form.mount()
1294-
1295-
const field = new FieldApi({
1296-
form,
1297-
name: 'items',
1298-
listeners: {
1299-
onChange: () => {
1300-
form.setFieldValue('itemsModified', true)
1301-
},
1302-
},
1218+
checkFieldValuesAndResetForNextTest({
1219+
itemsCount: 3,
1220+
items: ['replaced item', 'second item', 'third item'],
13031221
})
13041222

1305-
field.mount()
1306-
13071223
field.swapValues(0, 2)
1308-
1309-
expect(form.getFieldValue('itemsModified')).toStrictEqual(true)
1310-
expect(form.getFieldValue('items')).toStrictEqual([
1311-
'third item',
1312-
'second item',
1313-
'first item',
1314-
])
1315-
})
1316-
1317-
it('should change the form state when running listener onChange with moveValue', () => {
1318-
const form = new FormApi({
1319-
defaultValues: {
1320-
items: ['first item', 'second item', 'third item'],
1321-
itemsModified: false,
1322-
},
1224+
checkFieldValuesAndResetForNextTest({
1225+
itemsCount: 3,
1226+
items: ['third item', 'second item', 'replaced item'],
13231227
})
13241228

1325-
form.mount()
1326-
1327-
const field = new FieldApi({
1328-
form,
1329-
name: 'items',
1330-
listeners: {
1331-
onChange: () => {
1332-
form.setFieldValue('itemsModified', true)
1333-
},
1334-
},
1335-
})
1336-
1337-
field.mount()
1338-
13391229
field.moveValue(0, 2)
1340-
1341-
expect(form.getFieldValue('itemsModified')).toStrictEqual(true)
1342-
expect(form.getFieldValue('items')).toStrictEqual([
1343-
'second item',
1344-
'third item',
1345-
'first item',
1346-
])
1230+
checkFieldValuesAndResetForNextTest({
1231+
itemsCount: 3,
1232+
items: ['second item', 'replaced item', 'third item'],
1233+
})
13471234
})
13481235

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

0 commit comments

Comments
 (0)