Skip to content

Commit

Permalink
* fix: form state now correctly updates members when hidden state cha…
Browse files Browse the repository at this point in the history
…nges (#3719)

Underlying issue: immutableReconcile did not account for changes in number of keys in objects,
which resulted in it considering next object unchanged when a key had been removed.
This made the code retain the previous value incorrectly,

This had the effect of associating wrong schemaTypes with member fields, when hidden function
changed state (as one observed effect).
  • Loading branch information
snorrees committed Oct 6, 2022
1 parent 2450e56 commit ea48fa3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,27 @@ test('does not mutate any of its input', () => {

expect(() => immutableReconcile(prev, next)).not.toThrow()
})

test('returns new object when previous and next has different number of keys', () => {
const moreKeys = {
key1: 'value1',
key2: 'value2',
key3: 'value3',
}
const lessKeys = {
key1: 'value1',
key2: 'value2',
}

expect(immutableReconcile(moreKeys, lessKeys)).not.toBe(moreKeys)
expect(immutableReconcile(lessKeys, moreKeys)).not.toBe(lessKeys)
})

test('returns new array when previous and next has different length', () => {
const moreItems = ['a', 'b']
const lessItems = ['a']

expect(immutableReconcile(moreItems, lessItems)).not.toBe(moreItems)

expect(immutableReconcile(lessItems, moreItems)).not.toBe(lessItems)
})
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ function _immutableReconcile<T>(
assertType<Record<string, unknown>>(previous)
assertType<Record<string, unknown>>(next)

let allEqual = true
const nextKeys = Object.keys(next)
let allEqual = Object.keys(previous).length === nextKeys.length
const result: Record<string, unknown> = {}
for (const key of Object.keys(next)) {
for (const key of nextKeys) {
if (parents.has(next[key])) {
return next
}
Expand Down

0 comments on commit ea48fa3

Please sign in to comment.