map-set.js
This file
Path
__tests__/map-set.js
197 lines · 4.4 KiB · JavaScript
Last changed by
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
History of this fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 1–197why, one step back1"use strict"2import {Immer, nothing, original, isDraft, immerable} from "../src/index"3import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/common"45jest.setTimeout(1000)67runBaseTest("proxy (no freeze)", true, false)8// runBaseTest("proxy (autofreeze)", true, true)9// runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)10// runBaseTest("es5 (no freeze)", false, false)11// runBaseTest("es5 (autofreeze)", false, true)12// runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)1314function runBaseTest(name, useProxies, autoFreeze, useListener) {15 const listener = useListener ? function() {} : undefined16 const {produce, produceWithPatches} = createPatchedImmer({17 useProxies,18 autoFreeze19 })2021 // When `useListener` is true, append a function to the arguments of every22 // uncurried `produce` call in every test. This makes tests easier to read.23 function createPatchedImmer(options) {24 const immer = new Immer(options)2526 const {produce} = immer27 immer.produce = function(...args) {28 return typeof args[1] === "function" && args.length < 329 ? produce(...args, listener)30 : produce(...args)31 }3233 return immer34 }3536 describe("map issues " + name, () => {37 test("#472 ", () => {38 const project = produce(new Map(), draft => {39 draft.set("bar1", {blocked: false})40 draft.set("bar2", {blocked: false})41 })4243 // Read before write -- no error44 produce(project, draft => {45 const bar1 = draft.get("bar1")46 const bar2 = draft.get("bar2")47 bar1.blocked = true48 bar2.blocked = true49 })5051 // Read/write interleaved -- error52 produce(project, draft => {53 const bar1 = draft.get("bar1")54 bar1.blocked = true55 const bar2 = draft.get("bar2")56 bar2.blocked = true // TypeError: "blocked" is read-only57 })58 })5960 test("#466 - setNoPatches", () => {61 const obj = {62 set: new Set()63 }6465 const result = produceWithPatches(obj, draft => {66 draft.set.add("abc")67 })68 expect(result).toEqual([69 {set: new Set(["abc"])},70 [{op: "add", path: ["set", 0], value: "abc"}],71 [{op: "remove", path: ["set", 0], value: "abc"}]72 ])73 })7475 test("#466 - mapChangeBug ", () => {76 const obj = {77 map: new Map([78 ["a", new Map([["b", true], ["c", true], ["d", true]])],79 ["b", new Map([["a", true]])],80 ["c", new Map([["a", true]])],81 ["d", new Map([["a", true]])]82 ])83 }84 const result = produceWithPatches(obj, draft => {85 const aMap = draft.map.get("a")86 aMap.forEach((_, other) => {87 const otherMap = draft.map.get(other)88 otherMap.delete("a")89 })90 })91 expect(result).toEqual([92 {93 map: new Map([94 ["a", new Map([["b", true], ["c", true], ["d", true]])],95 ["b", new Map()],96 ["c", new Map()],97 ["d", new Map()]98 ])99 },100 [101 {102 op: "remove",103 path: ["map", "b", "a"]104 },105 {106 op: "remove",107 path: ["map", "c", "a"]108 },109 {110 op: "remove",111 path: ["map", "d", "a"]112 }113 ],114 [115 {116 op: "add",117 path: ["map", "b", "a"],118 value: true119 },120 {121 op: "add",122 path: ["map", "c", "a"],123 value: true124 },125 {126 op: "add",127 path: ["map", "d", "a"],128 value: true129 }130 ]131 ])132 })133134 test("#466 - mapChangeBug2 ", () => {135 const obj = {136 map: new Map([137 ["a", new Map([["b", true], ["c", true], ["d", true]])],138 ["b", new Map([["a", true]])],139 ["c", new Map([["a", true]])],140 ["d", new Map([["a", true]])]141 ])142 }143 const obj1 = produce(obj, draft => {})144 const [result, p, ip] = produceWithPatches(obj1, draft => {145 const aMap = draft.map.get("a")146 aMap.forEach((_, other) => {147 const otherMap = draft.map.get(other)148 otherMap.delete("a")149 })150 })151 expect(result).toEqual(152 {153 map: new Map([154 ["a", new Map([["b", true], ["c", true], ["d", true]])],155 ["b", new Map([])],156 ["c", new Map([])],157 ["d", new Map([])]158 ])159 }160 )161 expect(p).toEqual(162 [163 {164 op: "remove",165 path: ["map", "b", "a"]166 },167 {168 op: "remove",169 path: ["map", "c", "a"]170 },171 {172 op: "remove",173 path: ["map", "d", "a"]174 }175 ])176 expect(ip).toEqual(177 [178 {179 op: "add",180 path: ["map", "b", "a"],181 value: true182 },183 {184 op: "add",185 path: ["map", "c", "a"],186 value: true187 },188 {189 op: "add",190 path: ["map", "d", "a"],191 value: true192 }193 ]194 )195 })196 })197}