Skip to content

Commit

Permalink
fix(core): fix patchFieldStates update problem (#3763)
Browse files Browse the repository at this point in the history
* fix(core): fix patchFieldStates update problem

* test(core): adding missing tests

* build(deps): restore yarn.lock
  • Loading branch information
ZirkleTsing committed Mar 20, 2023
1 parent 1450f60 commit eca5a7e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
58 changes: 58 additions & 0 deletions packages/core/src/__tests__/internals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
deserialize,
isHTMLInputEvent,
} from '../shared/internals'
import { createForm } from '../'
import { attach } from './shared'

test('getValuesFromEvent', () => {
expect(getValuesFromEvent([{ target: { value: 123 } }])).toEqual([123])
Expand All @@ -28,6 +30,62 @@ test('patchFieldStates', () => {
expect(fields).toEqual({})
})

test('patchFieldStates should be sequence', () => {
const form = attach(createForm())
attach(
form.createArrayField({
name: 'array',
})
)
attach(
form.createField({
name: 'input',
basePath: 'array.0',
})
)
attach(
form.createField({
name: 'input',
basePath: 'array.1',
})
)
const before = Object.keys(form.fields)
form.fields['array'].move(1, 0)
const after = Object.keys(form.fields)
expect(after).toEqual(before)

const form2 = attach(createForm())
attach(
form2.createField({
name: 'field1',
title: 'Field 1',
})
)
attach(
form2.createField({
name: 'field2',
title: 'Field 1',
})
)

patchFieldStates(form2.fields, [
{
type: 'update',
address: 'field2',
oldAddress: 'field1',
payload: form2.field1,
},
{
type: 'update',
address: 'field1',
oldAddress: 'field2',
payload: form2.field2,
},
])

expect(Object.keys(form2.fields)).toEqual(['field1', 'field2'])
})

test('deserialize', () => {
expect(deserialize(null, null)).toBeUndefined()
expect(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export const patchFieldStates = (
if (payload) {
target[address] = payload
if (target[oldAddress] === payload) {
delete target[oldAddress]
target[oldAddress] = undefined
}
}
if (address && payload) {
Expand Down

0 comments on commit eca5a7e

Please sign in to comment.