patch.js
This file
Path
__tests__/patch.js
265 lines · 5.6 KiB · JavaScript
Last changed by
Fixed & added testsMichel Weststratebefore adoptionaf95dc8
History of this fileTake it
Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 1why, one step back1"use strict"Implemented inverse patchesMichel Weststratebefore adoptiond203f56
line 2why, one step back2import produce, {setUseProxies, applyPatches} from "../src/immer"Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 3–5why, one step back34jest.setTimeout(1000)5Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
line 6why, one step back6function runPatchTest(base, producer, patches, inversePathes) {Implemented inverse patchesMichel Weststratebefore adoptiond203f56
line 7why, one step back7 function runPatchTestHelper() {Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 8why, one step back8 let recordedPatchesPatches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
line 9why, one step back9 let recordedInversePatchesInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 10–11why, one step back10 const res = produce(base, producer, (p, i) => {11 recordedPatches = pPatches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
line 12why, one step back12 recordedInversePatches = iInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 13–16why, one step back13 })1415 test("produces the correct patches", () => {16 expect(recordedPatches).toEqual(patches)Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 17–18why, one step back17 if (inversePathes)18 expect(recordedInversePatches).toEqual(inversePathes)Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 19–20why, one step back19 })20Implemented inverse patchesMichel Weststratebefore adoptiond203f56
lines 21–22why, one step back21 test("patches are replayable", () => {22 expect(applyPatches(base, recordedPatches)).toEqual(res)Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 23–24why, one step back23 })24WIP on ES5 implMichel Weststratebefore adoption11a2d24
line 25why, one step back25 test("patches can be revered", () => {Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
line 26why, one step back26 expect(applyPatches(res, recordedInversePatches)).toEqual(base)Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 27–29why, one step back27 })28 }29Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
line 30why, one step back30 describe(`proxy`, () => {Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 31why, one step back31 setUseProxies(true)Implemented inverse patchesMichel Weststratebefore adoptiond203f56
line 32why, one step back32 runPatchTestHelper()Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 33–34why, one step back33 })34Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
line 35why, one step back35 describe(`es5`, () => {Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 36why, one step back36 setUseProxies(false)Implemented inverse patchesMichel Weststratebefore adoptiond203f56
line 37why, one step back37 runPatchTestHelper()Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 38–40why, one step back38 })39}40Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 41–49why, one step back41describe("simple assignment", () => {42 runPatchTest(43 {x: 3},44 d => {45 d.x++46 },47 [{op: "replace", path: ["x"], value: 4}]48 )49})Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 50why, one step back50Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 51–59why, one step back51describe("simple assignment - 2", () => {52 runPatchTest(53 {x: {y: 4}},54 d => {55 d.x.y++56 },57 [{op: "replace", path: ["x", "y"], value: 5}]58 )59})Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 60why, one step back60Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 61–69why, one step back61describe("simple assignment - 2", () => {62 runPatchTest(63 {x: {y: 4}},64 d => {65 d.x.y++66 },67 [{op: "replace", path: ["x", "y"], value: 5}]68 )69})Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 70why, one step back70Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 71–79why, one step back71describe("delete 1", () => {72 runPatchTest(73 {x: {y: 4}},74 d => {75 delete d.x76 },77 [{op: "remove", path: ["x"]}]78 )79})Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 80why, one step back80Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 81–91why, one step back81describe("moving stuff", () => {82 runPatchTest(83 {x: {y: 4}},84 d => {85 d.z = d.x86 d.z.y++87 delete d.x88 },89 [{op: "add", path: ["z"], value: {y: 5}}, {op: "remove", path: ["x"]}]90 )91})Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 92why, one step back92Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 93–106why, one step back93describe("minimum amount of changes", () => {94 runPatchTest(95 {x: 3, y: {a: 4}, z: 3},96 d => {97 d.y.a = 498 d.y.b = 599 Object.assign(d, {x: 4, y: {a: 2}})100 },101 [102 {op: "replace", path: ["x"], value: 4},103 {op: "replace", path: ["y"], value: {a: 2}}104 ]105 )106})More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
line 107why, one step back107Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 108–121why, one step back108describe("arrays - 1", () => {109 runPatchTest(110 {x: [1, 2, 3]},111 d => {112 d.x.unshift(4)113 },114 [115 {op: "replace", path: ["x", 0], value: 4},116 {op: "replace", path: ["x", 1], value: 1},117 {op: "replace", path: ["x", 2], value: 2},118 {op: "add", path: ["x", 3], value: 3}119 ]120 )121})More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
line 122why, one step back122Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 123–230why, one step back123describe("arrays - 2", () => {124 runPatchTest(125 {x: [1, 2, 3]},126 d => {127 d.x.splice(1, 1)128 },129 [130 {op: "replace", path: ["x", 1], value: 3},131 {op: "replace", path: ["x", "length"], value: 2}132 ]133 )134})135136describe("arrays - 3", () => {137 runPatchTest(138 {x: [1, 2, 3]},139 d => {140 d.x.unshift(4)141 d.x.splice(2, 1)142 },143 [144 {op: "replace", path: ["x", 0], value: 4},145 {op: "replace", path: ["x", 1], value: 1}146 ]147 )148})149150describe("arrays - 4", () => {151 runPatchTest(152 {x: [1, 2, 3]},153 d => {154 d.x.splice(1, 1)155 d.x.unshift(4)156 },157 [158 {op: "replace", path: ["x", 0], value: 4},159 {op: "replace", path: ["x", 1], value: 1}160 ]161 )162})163164describe("arrays - 5a", () => {165 runPatchTest(166 {x: [1, 2, 3]},167 d => {168 d.x.length -= 2169 },170 [{op: "replace", path: ["x", "length"], value: 1}],171 [172 {op: "add", path: ["x", 1], value: 2},173 {op: "add", path: ["x", 2], value: 3}174 ]175 )176})177178describe("arrays - 5b", () => {179 runPatchTest(180 {x: [1, 2, 3]},181 d => {182 d.x.pop()183 d.x.pop()184 },185 [{op: "replace", path: ["x", "length"], value: 1}]186 )187})188189describe("arrays - 6", () => {190 runPatchTest(191 {x: [1, 2, 3]},192 d => {193 d.x.push(4, 5)194 },195 [196 {op: "add", path: ["x", 3], value: 4},197 {op: "add", path: ["x", 4], value: 5}198 ],199 [{op: "replace", path: ["x", "length"], value: 3}]200 )201})202203describe("simple replacement", () => {204 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])205})206207describe("same value replacement - 1", () => {208 runPatchTest(209 {x: {y: 3}},210 d => {211 const a = d.x212 d.x = a213 },214 []215 )216})217218describe("same value replacement - 2", () => {219 runPatchTest(220 {x: {y: 3}},221 d => {222 const a = d.x223 d.x = 4224 d.x = a225 },226 // immer does not detect this is not an actual change227 [{op: "replace", path: ["x"], value: {y: 3}}]228 )229})230Fixed & added testsMichel Weststratebefore adoptionaf95dc8
lines 231–251why, one step back231describe("same value replacement - 3", () => {232 runPatchTest(233 {x: 3},234 d => {235 d.x = 3236 },237 []238 )239})240241describe("same value replacement - 4", () => {242 runPatchTest(243 {x: 3},244 d => {245 d.x = 4246 d.x = 3247 },248 // immer does not detect this is not an actual change249 [{op: "replace", path: ["x"], value: 3}]250 )251})Patches now also succeed for ES5. Patches generated are more optimalMichel Weststratebefore adoptionfd9ed2d
lines 252–265why, one step back252describe("simple delete", () => {253 runPatchTest(254 {x: 2},255 d => {256 delete d.x257 },258 [259 {260 op: "remove",261 path: ["x"]262 }263 ]264 )265})