Skip to content
Myr

Added tests reproducing #472 and #466

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent c1432fa · Browse files · 1 file+176 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+176 added−0 removed

  • tests:+176 added−0 removed

1 file+176 added−0 removed

added:__tests__/map-set.js+176 added
1"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)8runBaseTest("proxy (autofreeze)", true, true)9runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)10runBaseTest("es5 (no freeze)", false, false)11runBaseTest("es5 (autofreeze)", false, true)12runBaseTest("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 = 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 {162 op: "remove",163 path: ["map", "b", "a"]164 }165 ],166 [167 {168 op: "add",169 path: ["map", "b", "a"],170 value: true171 }172 ]173 ])174 })175 })176}