map-set.js
This file
Path
__tests__/map-set.js
248 lines · 5.1 KiB · JavaScript
Last changed by
fix: new map keys were not added if value is undefinedMichel Weststratebefore adoption4a1bd65
History of this fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 1why, one step back1"use strict"Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 2–9why, one step back2import {3 Immer,4 nothing,5 original,6 isDraft,7 immerable,8 enableAllPlugins9} from "../src/immer"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 10–11why, one step back10import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/common"11Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 12–13why, one step back12enableAllPlugins()13feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 14–86why, one step back14jest.setTimeout(1000)1516runBaseTest("proxy (no freeze)", true, false)17// runBaseTest("proxy (autofreeze)", true, true)18// runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)19// runBaseTest("es5 (no freeze)", false, false)20// runBaseTest("es5 (autofreeze)", false, true)21// runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)2223function runBaseTest(name, useProxies, autoFreeze, useListener) {24 const listener = useListener ? function() {} : undefined25 const {produce, produceWithPatches} = createPatchedImmer({26 useProxies,27 autoFreeze28 })2930 // When `useListener` is true, append a function to the arguments of every31 // uncurried `produce` call in every test. This makes tests easier to read.32 function createPatchedImmer(options) {33 const immer = new Immer(options)3435 const {produce} = immer36 immer.produce = function(...args) {37 return typeof args[1] === "function" && args.length < 338 ? produce(...args, listener)39 : produce(...args)40 }4142 return immer43 }4445 describe("map issues " + name, () => {46 test("#472 ", () => {47 const project = produce(new Map(), draft => {48 draft.set("bar1", {blocked: false})49 draft.set("bar2", {blocked: false})50 })5152 // Read before write -- no error53 produce(project, draft => {54 const bar1 = draft.get("bar1")55 const bar2 = draft.get("bar2")56 bar1.blocked = true57 bar2.blocked = true58 })5960 // Read/write interleaved -- error61 produce(project, draft => {62 const bar1 = draft.get("bar1")63 bar1.blocked = true64 const bar2 = draft.get("bar2")65 bar2.blocked = true // TypeError: "blocked" is read-only66 })67 })6869 test("#466 - setNoPatches", () => {70 const obj = {71 set: new Set()72 }7374 const result = produceWithPatches(obj, draft => {75 draft.set.add("abc")76 })77 expect(result).toEqual([78 {set: new Set(["abc"])},79 [{op: "add", path: ["set", 0], value: "abc"}],80 [{op: "remove", path: ["set", 0], value: "abc"}]81 ])82 })8384 test("#466 - mapChangeBug ", () => {85 const obj = {86 map: new Map([Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 87–94why, one step back87 [88 "a",89 new Map([90 ["b", true],91 ["c", true],92 ["d", true]93 ])94 ],feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 95–109why, one step back95 ["b", new Map([["a", true]])],96 ["c", new Map([["a", true]])],97 ["d", new Map([["a", true]])]98 ])99 }100 const result = produceWithPatches(obj, draft => {101 const aMap = draft.map.get("a")102 aMap.forEach((_, other) => {103 const otherMap = draft.map.get(other)104 otherMap.delete("a")105 })106 })107 expect(result).toEqual([108 {109 map: new Map([Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 110–117why, one step back110 [111 "a",112 new Map([113 ["b", true],114 ["c", true],115 ["d", true]116 ])117 ],feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 118–159why, one step back118 ["b", new Map()],119 ["c", new Map()],120 ["d", new Map()]121 ])122 },123 [124 {125 op: "remove",126 path: ["map", "b", "a"]127 },128 {129 op: "remove",130 path: ["map", "c", "a"]131 },132 {133 op: "remove",134 path: ["map", "d", "a"]135 }136 ],137 [138 {139 op: "add",140 path: ["map", "b", "a"],141 value: true142 },143 {144 op: "add",145 path: ["map", "c", "a"],146 value: true147 },148 {149 op: "add",150 path: ["map", "d", "a"],151 value: true152 }153 ]154 ])155 })156157 test("#466 - mapChangeBug2 ", () => {158 const obj = {159 map: new Map([Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 160–167why, one step back160 [161 "a",162 new Map([163 ["b", true],164 ["c", true],165 ["d", true]166 ])167 ],feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 168–180why, one step back168 ["b", new Map([["a", true]])],169 ["c", new Map([["a", true]])],170 ["d", new Map([["a", true]])]171 ])172 }173 const obj1 = produce(obj, draft => {})174 const [result, p, ip] = produceWithPatches(obj1, draft => {175 const aMap = draft.map.get("a")176 aMap.forEach((_, other) => {177 const otherMap = draft.map.get(other)178 otherMap.delete("a")179 })180 })Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 181–196why, one step back181 expect(result).toEqual({182 map: new Map([183 [184 "a",185 new Map([186 ["b", true],187 ["c", true],188 ["d", true]189 ])190 ],191 ["b", new Map([])],192 ["c", new Map([])],193 ["d", new Map([])]194 ])195 })196 expect(p).toEqual([feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 197why, one step back197 {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 198–207why, one step back198 op: "remove",199 path: ["map", "b", "a"]200 },201 {202 op: "remove",203 path: ["map", "c", "a"]204 },205 {206 op: "remove",207 path: ["map", "d", "a"]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 208why, one step back208 }Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 209–226why, one step back209 ])210 expect(ip).toEqual([211 {212 op: "add",213 path: ["map", "b", "a"],214 value: true215 },216 {217 op: "add",218 path: ["map", "c", "a"],219 value: true220 },221 {222 op: "add",223 path: ["map", "d", "a"],224 value: true225 }226 ])feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 227why, one step back227 })fix: fixed issue where new items in Set where not recorded after iterating. Fixes #586Michel Weststratebefore adoption169db77
lines 228–237why, one step back228229 test("#586", () => {230 const base = new Set([1, 2])231 const set = produce(base, draftSet => {232 debugger233 expect(Array.from(draftSet)).toEqual([1, 2])234 draftSet.add(3)235 })236 expect(Array.from(set).sort()).toEqual([1, 2, 3])237 })fix: new map keys were not added if value is undefinedMichel Weststratebefore adoption4a1bd65
lines 238–246why, one step back238239 test("#627 - new map key with value=undefined", () => {240 const map = new Map()241 const map1 = produce(map, draft => {242 draft.set("key", undefined)243 })244 expect(map1.has("key")).toBe(true)245 expect(map1.get("key")).toBe(undefined)246 })feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 247–248why, one step back247 })248}