Skip to content
Myr

map-set.js

This file

Path

__tests__/map-set.js

229 lines · 4.6 KiB · JavaScript

Last changed by

New build set. Current statsMichel Weststratebefore adoption9aec0fe

History of this file

Take it

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 1why, one step back
1"use strict"

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 2–9why, one step back
2import {
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 back
10import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/common"
11

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 12–13why, one step back
12enableAllPlugins()
13

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 14–86why, one step back
14jest.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([

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 87–94why, one step back
87 [
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 back
95 ["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([

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 110–117why, one step back
110 [
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 back
118 ["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([

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 160–167why, one step back
160 [
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 back
168 ["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 })

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 181–196why, one step back
181 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 back
197 {

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 198–207why, one step back
198 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 back
208 }

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 209–226why, one step back
209 ])
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

lines 227–229why, one step back
227 })
228 })229}