test: renaming properties
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Improved tests for renaming properties.
authored by aleclarson · committed by aleclarson · parent 00c622f · Browse files · 2 files·+75 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+75 added−34 removed
- tests:+75 added−34 removed
Provenance
- Commit
d83f4b5- Tree
8df9329- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
00c622f
2 files changed+75 added−34 removed
modified:__tests__/base.js+17 added-12 removed
323 unmodified lines324 expect(nextState.anArray).not.toBe(baseState.anArray)325 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])326 })327 328- it("reusing object should work", () => {328+ it("can rename nested objects (no changes)", () => {329 const nextState = produce(330 baseState,331 s => {332 const obj = s.anObject333 delete s.anObject334- s.messy = obj334+ s.renamed = obj335 },336 listener337 )338 expect(nextState).not.toBe(baseState)339 expect(nextState.anArray).toBe(baseState.anArray)340+ expect(nextState.renamed.nested).toBe(baseState.anObject.nested)341 expect(enumerableOnly(nextState)).toEqual({342 anArray: [3, 2, {c: 3}, 1],343 aProp: "hi",343- messy: {344+ renamed: {345 nested: {346 yummie: true347 },348 coffee: false349 }350 })350- expect(nextState.messy.nested).toBe(baseState.anObject.nested)351 })352 353- it("refs should be transparent", () => {353+ // Very similar to the test before, but the reused object has one354+ // property changed, one added, and one removed.355+ it("can rename nested objects (with changes)", () => {356 const nextState = produce(357 baseState,358 s => {359 const obj = s.anObject358- s.aProp = "hello"360 delete s.anObject360- obj.coffee = true361- s.messy = obj361+362+ obj.coffee = true // change363+ obj.nested.yummy = true // add364+ delete obj.nested.yummie // delete365+366+ s.renamed = obj367 },368 listener369 )370 expect(nextState).not.toBe(baseState)371 expect(nextState.anArray).toBe(baseState.anArray)372+ expect(nextState.renamed.nested).not.toBe(baseState.anObject.nested)373 expect(enumerableOnly(nextState)).toEqual({374 anArray: [3, 2, {c: 3}, 1],369- aProp: "hello",370- messy: {375+ aProp: "hi",376+ renamed: {377 nested: {372- yummie: true378+ yummy: true379 },380 coffee: true381 }382 })377- expect(nextState.messy.nested).toBe(baseState.anObject.nested)383 })384 385 it("should allow setting to undefined a defined draft property", () => {386 const nextState = produce(1102 unmodified linesmodified:__tests__/patch.js+58 added-22 removed
20 unmodified lines21 test("patches are replayable", () => {22 expect(applyPatches(base, recordedPatches)).toEqual(res)23 })24 25- test("patches can be revered", () => {25+ test("patches can be reversed", () => {26 expect(applyPatches(res, recordedInversePatches)).toEqual(base)27 })28 }29 42 unmodified lines72 }).toThrowError(/^Remove can only remove the last key of an array/)73 })74 })75 76-describe("simple assignment", () => {76+describe("simple assignment - 1", () => {77 runPatchTest(78 {x: 3},79 d => {80 d.x++11 unmodified lines92 [{op: "replace", path: ["x", "y"], value: 5}]93 )94 })95 96-describe("simple assignment - 2", () => {97- runPatchTest(98- {x: {y: 4}},99- d => {100- d.x.y++101- },102- [{op: "replace", path: ["x", "y"], value: 5}]103- )104-})105-96 describe("delete 1", () => {97 runPatchTest(98 {x: {y: 4}},99 d => {2 unmodified lines102 [{op: "remove", path: ["x"]}]103 )104 })105 116-describe("moving stuff", () => {117- runPatchTest(118- {x: {y: 4}},119- d => {120- d.z = d.x121- d.z.y++122- delete d.x123- },124- [{op: "add", path: ["z"], value: {y: 5}}, {op: "remove", path: ["x"]}]125- )106+describe("renaming properties", () => {107+ describe("nested object (no changes)", () => {108+ runPatchTest(109+ {a: {b: 1}},110+ d => {111+ d.x = d.a112+ delete d.a113+ },114+ [115+ {op: "add", path: ["x"], value: {b: 1}},116+ {op: "remove", path: ["a"]}117+ ]118+ )119+ })120+121+ describe("nested object (with changes)", () => {122+ runPatchTest(123+ {a: {b: 1, c: 1}},124+ d => {125+ let a = d.a126+ a.b = 2 // change127+ delete a.c // delete128+ a.y = 2 // add129+130+ // rename131+ d.x = a132+ delete d.a133+ },134+ [135+ {op: "add", path: ["x"], value: {b: 2, y: 2}},136+ {op: "remove", path: ["a"]}137+ ]138+ )139+ })140+141+ describe("deeply nested object (with changes)", () => {142+ runPatchTest(143+ {a: {b: {c: 1, d: 1}}},144+ d => {145+ debugger146+ let b = d.a.b147+ b.c = 2 // change148+ delete b.d // delete149+ b.y = 2 // add150+151+ // rename152+ d.a.x = b153+ delete d.a.b154+ },155+ [156+ {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},157+ {op: "remove", path: ["a", "b"]}158+ ]159+ )160+ })161 })162 163 describe("minimum amount of changes", () => {164 runPatchTest(153 unmodified lines318 // immer does not detect this is not an actual change319 [{op: "replace", path: ["x"], value: 3}]320 )321 })322+323 describe("simple delete", () => {324 runPatchTest(325 {x: 2},326 d => {10 unmodified lines