map-set.js
This file
Path
__tests__/map-set.js
197 lines · 4.4 KiB · JavaScript
Last changed by
fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
History of this fileTake it
Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 1–7why, 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)fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 8–12why, one step back8// 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)Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 13–59why, one step back1314function 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 })59fixed most testsMichel Weststratebefore adoption385c0a4
line 60why, one step back60 test("#466 - setNoPatches", () => {Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 61–74why, one step back61 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 })74fixed most testsMichel Weststratebefore adoption385c0a4
line 75why, one step back75 test("#466 - mapChangeBug ", () => {Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 76–133why, one step back76 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 })133fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 134why, one step back134 test("#466 - mapChangeBug2 ", () => {Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 135–143why, one step back135 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 => {})fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 144why, one step back144 const [result, p, ip] = produceWithPatches(obj1, draft => {Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 145–150why, one step back145 const aMap = draft.map.get("a")146 aMap.forEach((_, other) => {147 const otherMap = draft.map.get(other)148 otherMap.delete("a")149 })150 })fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 151why, one step back151 expect(result).toEqual(Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 152–158why, one step back152 {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 ])fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 159–161why, one step back159 }160 )161 expect(p).toEqual(Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 162–165why, one step back162 [163 {164 op: "remove",165 path: ["map", "b", "a"]fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 166–173why, one step back166 },167 {168 op: "remove",169 path: ["map", "c", "a"]170 },171 {172 op: "remove",173 path: ["map", "d", "a"]Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
line 174why, one step back174 }fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 175–176why, one step back175 ])176 expect(ip).toEqual(Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 177–181why, one step back177 [178 {179 op: "add",180 path: ["map", "b", "a"],181 value: truefixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
lines 182–191why, one step back182 },183 {184 op: "add",185 path: ["map", "c", "a"],186 value: true187 },188 {189 op: "add",190 path: ["map", "d", "a"],191 value: trueAdded tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 192–193why, one step back192 }193 ]fixed remaining tests, fixes #466Michel Weststratebefore adoption7f5e301
line 194why, one step back194 )Added tests reproducing #472 and #466Michel Weststratebefore adoptiondb9b689
lines 195–197why, one step back195 })196 })197}