Skip to content

Commit

Permalink
fix: maintain the enforceString status after replacing a value
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed May 23, 2022
1 parent ef09810 commit 4d1e9e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib/logic/documentState.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ export function documentStatePatch(json, state, operations) {
}
}

function after(state, operation) {
function after(state, operation, previousState) {
const { op, path } = operation

let updatedState = state
Expand All @@ -518,6 +518,14 @@ export function documentStatePatch(json, state, operations) {
}
}

if (op === 'replace') {
// copy the old enforceString state after replacing a value
const enforceString = getIn(previousState, path.concat([STATE_ENFORCE_STRING]))
if (typeof enforceString === 'boolean') {
updatedState = setIn(updatedState, path.concat([STATE_ENFORCE_STRING]), enforceString)
}
}

return updatedState
}

Expand Down
10 changes: 10 additions & 0 deletions src/lib/logic/documentState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,16 @@ describe('documentState', () => {
assert.strictEqual(typeof updatedState.c[STATE_ID], 'string')
})

it('replace: should keep enforceString state', () => {
const json = '42'
const state = syncState(json, undefined, [], () => false)
assert.strictEqual(state[STATE_ENFORCE_STRING], true)

const operations = [{ op: 'replace', path: '', value: 'forty two' }]
const updatedState = documentStatePatch(json, state, operations).state
assert.deepStrictEqual(updatedState[STATE_ENFORCE_STRING], true)
})

it('add: should override a value in an object', () => {
// TODO
})
Expand Down

0 comments on commit 4d1e9e3

Please sign in to comment.