Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

265 lines · 5.6 KiB · JavaScript

Last changed by

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

History of this file

Take it

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1–265why, one step back
1"use strict"
2import produce, {setUseProxies, applyPatches} from "../src/immer"34jest.setTimeout(1000)56function runPatchTest(base, producer, patches, inversePathes) {7 function runPatchTestHelper() {8 let recordedPatches9 let recordedInversePatches10 const res = produce(base, producer, (p, i) => {11 recordedPatches = p12 recordedInversePatches = i13 })1415 test("produces the correct patches", () => {16 expect(recordedPatches).toEqual(patches)17 if (inversePathes)18 expect(recordedInversePatches).toEqual(inversePathes)19 })2021 test("patches are replayable", () => {22 expect(applyPatches(base, recordedPatches)).toEqual(res)23 })2425 test("patches can be revered", () => {26 expect(applyPatches(res, recordedInversePatches)).toEqual(base)27 })28 }2930 describe(`proxy`, () => {31 setUseProxies(true)32 runPatchTestHelper()33 })3435 describe(`es5`, () => {36 setUseProxies(false)37 runPatchTestHelper()38 })39}4041describe("simple assignment", () => {42 runPatchTest(43 {x: 3},44 d => {45 d.x++46 },47 [{op: "replace", path: ["x"], value: 4}]48 )49})5051describe("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})6061describe("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})7071describe("delete 1", () => {72 runPatchTest(73 {x: {y: 4}},74 d => {75 delete d.x76 },77 [{op: "remove", path: ["x"]}]78 )79})8081describe("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})9293describe("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})107108describe("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})122123describe("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})230231describe("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})252describe("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})