Skip to content

Commit 5155a33

Browse files
authored
chore: regression tests for fixed issue #916 (#1215)
1 parent b67db83 commit 5155a33

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

__tests__/patch.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,69 @@ describe("applyPatches", () => {
198198
})
199199
})
200200

201+
describe("#916 nested patches with moved child draft", () => {
202+
const makeBaseState = () => ({
203+
id: 0,
204+
type: "root",
205+
children: [{id: 2, type: "node", value: "A"}]
206+
})
207+
208+
const makeRootWithGroup = groupName => ({
209+
id: 0,
210+
type: "root",
211+
children: [
212+
{
213+
id: 3,
214+
children: [{id: 2, type: "node", value: "A"}],
215+
groupName
216+
}
217+
]
218+
})
219+
220+
function runNestedPatchScenario() {
221+
let nestedPatches = []
222+
const [nextState] = produceWithPatches(makeBaseState(), draft => {
223+
const node = draft.children[0]
224+
draft.children.splice(0, 1) // remove node from root
225+
226+
const group = {id: 3, children: [node], groupName: ""} // new group
227+
draft.children.splice(0, 0, group) // add group to root
228+
229+
const [, patches] = produceWithPatches(draft, inner => {
230+
const value = inner.children[0].children[0].value
231+
inner.children[0].groupName = value + " Group"
232+
})
233+
234+
nestedPatches = patches
235+
applyPatches(draft, patches)
236+
})
237+
238+
return {nextState, patches: nestedPatches}
239+
}
240+
241+
it("#916 nested patches with moved child draft", () => {
242+
const {nextState, patches} = runNestedPatchScenario()
243+
244+
expect(patches).toEqual(
245+
expect.arrayContaining([
246+
{
247+
op: "replace",
248+
path: ["children", 0, "groupName"],
249+
value: "A Group"
250+
}
251+
])
252+
)
253+
expect(nextState).toEqual(makeRootWithGroup("A Group"))
254+
})
255+
256+
it("#916 patches apply on non-draft base", () => {
257+
const {patches} = runNestedPatchScenario()
258+
expect(applyPatches(makeRootWithGroup(""), patches)).toEqual(
259+
makeRootWithGroup("A Group")
260+
)
261+
})
262+
})
263+
201264
// New macro-style test
202265
runPatchTests(
203266
"simple assignment - 1",

0 commit comments

Comments
 (0)