Skip to content
Myr

map-set.js

This file

Path

__tests__/map-set.js

299 lines · 6.1 KiB · JavaScript

Last changed by

fix: make plugin loading idempotent, fixes #692Michel Weststratebefore adoption754331b

History of this file

Take it

The walk stopped after 200 commits: lines older than fbae585 point at it.

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 2–7why, one step back
2import {
3 Immer,4 nothing,5 original,6 isDraft,7 immerable,

fix: make plugin loading idempotent, fixes #692Michel Weststratebefore adoption754331b

lines 8–9why, one step back
8 enableAllPlugins,
9 enableMapSet

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 10why, one step back
10} from "../src/immer"

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 11–12why, one step back
11import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/common"
12

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 15–87why, one step back
15jest.setTimeout(1000)
1617runBaseTest("proxy (no freeze)", true, false)18// runBaseTest("proxy (autofreeze)", true, true)19// runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)20// runBaseTest("es5 (no freeze)", false, false)21// runBaseTest("es5 (autofreeze)", false, true)22// runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)2324function runBaseTest(name, useProxies, autoFreeze, useListener) {25 const listener = useListener ? function() {} : undefined26 const {produce, produceWithPatches} = createPatchedImmer({27 useProxies,28 autoFreeze29 })3031 // When `useListener` is true, append a function to the arguments of every32 // uncurried `produce` call in every test. This makes tests easier to read.33 function createPatchedImmer(options) {34 const immer = new Immer(options)3536 const {produce} = immer37 immer.produce = function(...args) {38 return typeof args[1] === "function" && args.length < 339 ? produce(...args, listener)40 : produce(...args)41 }4243 return immer44 }4546 describe("map issues " + name, () => {47 test("#472 ", () => {48 const project = produce(new Map(), draft => {49 draft.set("bar1", {blocked: false})50 draft.set("bar2", {blocked: false})51 })5253 // Read before write -- no error54 produce(project, draft => {55 const bar1 = draft.get("bar1")56 const bar2 = draft.get("bar2")57 bar1.blocked = true58 bar2.blocked = true59 })6061 // Read/write interleaved -- error62 produce(project, draft => {63 const bar1 = draft.get("bar1")64 bar1.blocked = true65 const bar2 = draft.get("bar2")66 bar2.blocked = true // TypeError: "blocked" is read-only67 })68 })6970 test("#466 - setNoPatches", () => {71 const obj = {72 set: new Set()73 }7475 const result = produceWithPatches(obj, draft => {76 draft.set.add("abc")77 })78 expect(result).toEqual([79 {set: new Set(["abc"])},80 [{op: "add", path: ["set", 0], value: "abc"}],81 [{op: "remove", path: ["set", 0], value: "abc"}]82 ])83 })8485 test("#466 - mapChangeBug ", () => {86 const obj = {87 map: new Map([

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 88–95why, one step back
88 [
89 "a",90 new Map([91 ["b", true],92 ["c", true],93 ["d", true]94 ])95 ],

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 96–110why, one step back
96 ["b", new Map([["a", true]])],
97 ["c", new Map([["a", true]])],98 ["d", new Map([["a", true]])]99 ])100 }101 const result = produceWithPatches(obj, draft => {102 const aMap = draft.map.get("a")103 aMap.forEach((_, other) => {104 const otherMap = draft.map.get(other)105 otherMap.delete("a")106 })107 })108 expect(result).toEqual([109 {110 map: new Map([

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 111–118why, one step back
111 [
112 "a",113 new Map([114 ["b", true],115 ["c", true],116 ["d", true]117 ])118 ],

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 119–160why, one step back
119 ["b", new Map()],
120 ["c", new Map()],121 ["d", new Map()]122 ])123 },124 [125 {126 op: "remove",127 path: ["map", "b", "a"]128 },129 {130 op: "remove",131 path: ["map", "c", "a"]132 },133 {134 op: "remove",135 path: ["map", "d", "a"]136 }137 ],138 [139 {140 op: "add",141 path: ["map", "b", "a"],142 value: true143 },144 {145 op: "add",146 path: ["map", "c", "a"],147 value: true148 },149 {150 op: "add",151 path: ["map", "d", "a"],152 value: true153 }154 ]155 ])156 })157158 test("#466 - mapChangeBug2 ", () => {159 const obj = {160 map: new Map([

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 161–168why, one step back
161 [
162 "a",163 new Map([164 ["b", true],165 ["c", true],166 ["d", true]167 ])168 ],

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 169–181why, one step back
169 ["b", new Map([["a", true]])],
170 ["c", new Map([["a", true]])],171 ["d", new Map([["a", true]])]172 ])173 }174 const obj1 = produce(obj, draft => {})175 const [result, p, ip] = produceWithPatches(obj1, draft => {176 const aMap = draft.map.get("a")177 aMap.forEach((_, other) => {178 const otherMap = draft.map.get(other)179 otherMap.delete("a")180 })181 })

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 182–197why, one step back
182 expect(result).toEqual({
183 map: new Map([184 [185 "a",186 new Map([187 ["b", true],188 ["c", true],189 ["d", true]190 ])191 ],192 ["b", new Map([])],193 ["c", new Map([])],194 ["d", new Map([])]195 ])196 })197 expect(p).toEqual([

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

line 198why, one step back
198 {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 199–208why, one step back
199 op: "remove",
200 path: ["map", "b", "a"]201 },202 {203 op: "remove",204 path: ["map", "c", "a"]205 },206 {207 op: "remove",208 path: ["map", "d", "a"]

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

line 209why, one step back
209 }

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 210–227why, one step back
210 ])
211 expect(ip).toEqual([212 {213 op: "add",214 path: ["map", "b", "a"],215 value: true216 },217 {218 op: "add",219 path: ["map", "c", "a"],220 value: true221 },222 {223 op: "add",224 path: ["map", "d", "a"],225 value: true226 }227 ])

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

line 228why, one step back
228 })

fix: fixed issue where new items in Set where not recorded after iterating. Fixes #586Michel Weststratebefore adoption169db77

lines 229–238why, one step back
229
230 test("#586", () => {231 const base = new Set([1, 2])232 const set = produce(base, draftSet => {233 debugger234 expect(Array.from(draftSet)).toEqual([1, 2])235 draftSet.add(3)236 })237 expect(Array.from(set).sort()).toEqual([1, 2, 3])238 })

fix: new map keys were not added if value is undefinedMichel Weststratebefore adoption4a1bd65

lines 239–247why, one step back
239
240 test("#627 - new map key with value=undefined", () => {241 const map = new Map()242 const map1 = produce(map, draft => {243 draft.set("key", undefined)244 })245 expect(map1.has("key")).toBe(true)246 expect(map1.get("key")).toBe(undefined)247 })

fix: clear map creates invalid patches, fixes #663Michel Weststratebefore adoptionbacc1e0

lines 248–269why, one step back
248
249 test("#663 - clear map", () => {250 const map = new Map([251 ["a", "b"],252 ["b", "c"]253 ])254 const result = produceWithPatches(map, draft => {255 draft.clear()256 })257258 expect(result).toEqual([259 new Map(),260 [261 {op: "remove", path: ["a"]},262 {op: "remove", path: ["b"]}263 ],264 [265 {op: "add", path: ["a"], value: "b"},266 {op: "add", path: ["b"], value: "c"}267 ]268 ])269 })

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 270–282why, one step back
270
271 test("#680 - Clearing empty Set&Map should be noop", () => {272 const map = new Map()273 let result = produce(map, draft => {274 draft.clear()275 })276 expect(result).toBe(map)277278 const set = new Set()279 result = produce(set, draft => {280 draft.clear()281 })282 expect(result).toBe(set)

fix: make plugin loading idempotent, fixes #692Michel Weststratebefore adoption754331b

lines 283–296why, one step back
283 })
284285 test("#692 - idempotent plugin loading", () => {286 let mapType1287 produce(new Map(), draft => {288 mapType1 = draft.constructor289 })290291 enableMapSet()292 let mapType2293 produce(new Map(), draft => {294 mapType2 = draft.constructor295 })296 expect(mapType1).toBe(mapType2)

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

line 297why, one step back
297 })

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 298–299why, one step back
298 })
299}