Skip to content
Myr

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

2 files+75 added−34 removed

modified:__tests__/base.js+17 added-12 removed
323 unmodified lines
324 expect(nextState.anArray).not.toBe(baseState.anArray)325 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])326 })327328 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 })352353 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 = obj361362 obj.coffee = true // change363 obj.nested.yummy = true // add364 delete obj.nested.yummie // delete365366 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 })384385 it("should allow setting to undefined a defined draft property", () => {386 const nextState = produce(
1102 unmodified lines
modified:__tests__/patch.js+58 added-22 removed
20 unmodified lines
21 test("patches are replayable", () => {22 expect(applyPatches(base, recordedPatches)).toEqual(res)23 })2425 test("patches can be revered", () => {25 test("patches can be reversed", () => {26 expect(applyPatches(res, recordedInversePatches)).toEqual(base)27 })28 }29
42 unmodified lines
72 }).toThrowError(/^Remove can only remove the last key of an array/)73 })74})7576describe("simple assignment", () => {76describe("simple assignment - 1", () => {77 runPatchTest(78 {x: 3},79 d => {80 d.x++
11 unmodified lines
92 [{op: "replace", path: ["x", "y"], value: 5}]93 )94})9596describe("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})10596describe("delete 1", () => {97 runPatchTest(98 {x: {y: 4}},99 d => {
2 unmodified lines
102 [{op: "remove", path: ["x"]}]103 )104})105116describe("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 )106describe("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 })120121 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 // add129130 // 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 })140141 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 // add150151 // 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})162163describe("minimum amount of changes", () => {164 runPatchTest(
153 unmodified lines
318 // immer does not detect this is not an actual change319 [{op: "replace", path: ["x"], value: 3}]320 )321})322323describe("simple delete", () => {324 runPatchTest(325 {x: 2},326 d => {
10 unmodified lines