hooks.js
This file
Path
__tests__/hooks.js
387 lines · 8.5 KiB · JavaScript
Last changed by
chore: verify build during testMichel Weststratebefore adoptionef8990b
History of this fileTake it
The walk stopped after 200 commits: lines older than e8fe4ba point at it.
fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 1why, one step back1"use strict"test: hooks in es5aleclarsonbefore adoption271cd1b
line 2why, one step back2import {Immer, setUseProxies} from "../src/index"fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 3why, one step back3import matchers from "expect/build/matchers"feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 4why, one step back4import {isSet} from "../src/common"fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 5why, one step back5test: hooks in es5aleclarsonbefore adoption271cd1b
lines 6–9why, one step back6describe("hooks (proxy) -", () => createHookTests(true))7describe("hooks (es5) -", () => createHookTests(false))89function createHookTests(useProxies) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 10why, one step back10 let produce, onAssign, onDelete, onCopyfix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 11why, one step back11style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 12–20why, one step back12 beforeEach(() => {13 ;({produce, onAssign, onDelete, onCopy} = new Immer({14 autoFreeze: true,15 useProxies,16 onAssign: defuseProxies(jest.fn().mockName("onAssign")),17 onDelete: defuseProxies(jest.fn().mockName("onDelete")),18 onCopy: defuseProxies(jest.fn().mockName("onCopy"))19 }))20 })fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 21why, one step back21style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 22–121why, one step back22 describe("onAssign()", () => {23 useSharedTests(() => onAssign)24 describe("when draft is an object", () => {25 test("assign", () => {26 produce({a: 0, b: 0, c: 0}, s => {27 s.a++28 s.c++29 })30 expectCalls(onAssign)31 })32 test("assign (no change)", () => {33 produce({a: 0}, s => {34 s.a = 035 })36 expect(onAssign).not.toBeCalled()37 })38 test("delete", () => {39 produce({a: 1}, s => {40 delete s.a41 })42 expect(onAssign).not.toBeCalled()43 })44 test("nested assignments", () => {45 produce({a: {b: {c: 1, d: 1, e: 1}}}, s => {46 const {b} = s.a47 b.c = 248 delete b.d49 b.e = 1 // no-op50 })51 expectCalls(onAssign)52 })53 })54 describe("when draft is an array", () => {55 test("assign", () => {56 produce([1], s => {57 s[0] = 058 })59 expectCalls(onAssign)60 })61 test("push", () => {62 produce([], s => {63 s.push(4)64 })65 expectCalls(onAssign)66 })67 test("pop", () => {68 produce([1], s => {69 s.pop()70 })71 expect(onAssign).not.toBeCalled()72 })73 test("unshift", () => {74 produce([1], s => {75 s.unshift(0)76 })77 expectCalls(onAssign)78 })79 test("length = 0", () => {80 produce([1], s => {81 s.length = 082 })83 expect(onAssign).not.toBeCalled()84 })85 test("splice (length += 1)", () => {86 produce([1, 2, 3], s => {87 s.splice(1, 1, 0, 0)88 })89 expectCalls(onAssign)90 })91 test("splice (length += 0)", () => {92 produce([1, 2, 3], s => {93 s.splice(1, 1, 0)94 })95 expectCalls(onAssign)96 })97 test("splice (length -= 1)", () => {98 produce([1, 2, 3], s => {99 s.splice(0, 2, 6)100 })101 expectCalls(onAssign)102 })103 })104 describe("when a draft is moved into a new object", () => {105 it("is called in the right order", () => {106 const calls = []107 onAssign.mockImplementation((_, prop) => {108 calls.push(prop)109 })110 produce({a: {b: 1, c: {}}}, s => {111 s.a.b = 0112 s.a.c.d = 1113 s.x = {y: {z: s.a}}114 delete s.a115 })116 // Sibling properties use enumeration order, which means new117 // properties come last among their siblings. The deepest118 // properties always come first in their ancestor chain.119 expect(calls).toEqual(["b", "d", "c", "x"])120 })121 })feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 122–126why, one step back122123 describe("when draft is a Map", () => {124 test("assign", () => {125 const key1 = {prop: "val1"}126 const key2 = {prop: "val2"}chore: verify build during testMichel Weststratebefore adoptionef8990b
lines 127–137why, one step back127 produce(128 new Map([129 ["a", 0],130 [key1, 1],131 [key2, 2]132 ]),133 s => {134 s.set("a", 10)135 s.set(key1, 11)136 }137 )feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 138–155why, one step back138 expectCalls(onAssign)139 })140 test("assign (no change)", () => {141 produce(new Map([["a", 0]]), s => {142 s.set("a", 0)143 })144 expect(onAssign).not.toBeCalled()145 })146 test("delete", () => {147 produce(new Map([["a", 0]]), s => {148 s.delete("a")149 })150 expect(onAssign).not.toBeCalled()151 })152 test("nested assignments", () => {153 const key1 = {prop: "val1"}154 produce(155 new Map([chore: verify build during testMichel Weststratebefore adoptionef8990b
lines 156–168why, one step back156 [157 "a",158 new Map([159 [160 key1,161 new Map([162 ["b", 1],163 ["c", 1],164 ["d", 1]165 ])166 ]167 ])168 ]feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 169–205why, one step back169 ]),170 s => {171 const nested = s.get("a").get(key1)172 nested.set("b", 2)173 nested.delete("c")174 nested.set("d", 1) // no-op175 }176 )177 expectCalls(onAssign)178 })179 })180181 describe("when draft is a Set", () => {182 test("assign", () => {183 produce({a: new Set([1, 2, 3])}, s => {184 s.a.add(4)185 })186 expectCalls(onAssign)187 })188 test("assign (no change)", () => {189 produce({a: new Set([1, 2, 3])}, s => {190 s.a.add(3)191 })192 expect(onAssign).not.toBeCalled()193 })194 // Any mutation of a Set results in a new assignment. Including deletes.195 test("delete", () => {196 produce({a: new Set([1, 2, 3])}, s => {197 s.a.delete(1)198 })199 expectCalls(onAssign)200 })201 test("nested assignments", () => {202 const val1 = {prop: "val1"}203 produce({a: new Set(["a", new Set([val1, 1])])}, s => {204 let nested205 s.a.forEach(value => {chore: verify build during testMichel Weststratebefore adoptionef8990b
line 206why, one step back206 if (value instanceof Set) {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 207–216why, one step back207 nested = value208 }209 })210 nested.add(2)211 nested.delete(val1)212 nested.add(1) // no-op213 })214 expectCalls(onAssign)215 })216 })style: use tabsuse-tabsbefore adoptiona9c0fd0
line 217why, one step back217 })fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 218why, one step back218style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 219–261why, one step back219 describe("onDelete()", () => {220 useSharedTests(() => onDelete)221 describe("when draft is an object -", () => {222 test("delete", () => {223 produce({a: 1, b: 1, c: 1}, s => {224 delete s.a225 delete s.c226 })227 expectCalls(onDelete)228 })229 test("delete (no change)", () => {230 produce({}, s => {231 delete s.a232 })233 expect(onDelete).not.toBeCalled()234 })235 test("nested deletions", () => {236 produce({a: {b: {c: 1}}}, s => {237 delete s.a.b.c238 })239 expectCalls(onDelete)240 })241 })242 describe("when draft is an array -", () => {243 test("pop", () => {244 produce([1], s => {245 s.pop()246 })247 expectCalls(onDelete)248 })249 test("length = 0", () => {250 produce([1], s => {251 s.length = 0252 })253 expectCalls(onDelete)254 })255 test("splice (length -= 1)", () => {256 produce([1, 2, 3], s => {257 s.splice(0, 2, 6)258 })259 expectCalls(onDelete)260 })261 })feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 262–266why, one step back262263 describe("when draft is a Map -", () => {264 test("delete", () => {265 const key1 = {prop: "val1"}266 const key2 = {prop: "val2"}chore: verify build during testMichel Weststratebefore adoptionef8990b
lines 267–277why, one step back267 produce(268 new Map([269 ["a", 0],270 [key1, 1],271 [key2, 2]272 ]),273 s => {274 s.delete("a")275 s.delete(key1)276 }277 )feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 278–304why, one step back278 expectCalls(onDelete)279 })280 test("delete (no change)", () => {281 produce(new Map(), s => {282 s.delete("a")283 })284 expect(onDelete).not.toBeCalled()285 })286 test("nested deletions", () => {287 const key1 = {prop: "val1"}288 produce(new Map([["a", new Map([[key1, new Map([["b", 1]])]])]]), s => {289 s.get("a")290 .get(key1)291 .delete("b")292 })293 expectCalls(onDelete)294 })295 })296297 describe("when draft is a Set -", () => {298 test("delete", () => {299 produce({a: new Set([1])}, s => {300 s.a.delete(1)301 })302 expect(onDelete).not.toBeCalled()303 })304 })style: use tabsuse-tabsbefore adoptiona9c0fd0
line 305why, one step back305 })fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 306why, one step back306style: use tabsuse-tabsbefore adoptiona9c0fd0
line 307why, one step back307 describe("onCopy()", () => {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 308–310why, one step back308 let calls309 beforeEach(() => {310 calls = []style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 311–313why, one step back311 onCopy.mockImplementation(s => {312 calls.push(s.base)313 })feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 314–317why, one step back314 })315316 useSharedTests(() => onCopy)317 it("is called in the right order for objects", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 318–322why, one step back318 const base = {a: {b: {c: 1}}}319 produce(base, s => {320 delete s.a.b.c321 })322 expect(calls).toShallowEqual([base.a.b, base.a, base])feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 323–339why, one step back323 })324325 it("is called in the right order for Maps", () => {326 const base = new Map([["a", new Map([["b", 0]])]])327 produce(base, s => {328 s.get("a").delete("b")329 })330 expect(calls).toShallowEqual([base.get("a"), base])331 })332333 it("is called in the right order for Sets", () => {334 const item1 = {a: 0}335 const base = new Set([item1])336 produce(base, s => {337 s.forEach(item => item.a++)338 })339 expect(calls).toShallowEqual([item1, base])style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 340–341why, one step back340 })341 })fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 342why, one step back342style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 343–356why, one step back343 function useSharedTests(getHook) {344 it("is called before the parent is frozen", () => {345 const hook = getHook()346 hook.mockImplementation(s => {347 // Parent object must not be frozen.348 expect(Object.isFrozen(s.base)).toBeFalsy()349 })350 produce({a: {b: {c: 0}}}, s => {351 if (hook == onDelete) delete s.a.b.c352 else s.a.b.c = 1353 })354 expect(hook).toHaveBeenCalledTimes(hook == onDelete ? 1 : 3)355 })356 }test: hooks in es5aleclarsonbefore adoption271cd1b
line 357why, one step back357}fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
lines 358–360why, one step back358359// Produce a snapshot of the hook arguments (minus any draft state).360function expectCalls(hook) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 361–365why, one step back361 expect(362 hook.mock.calls.map(call => {363 return call.slice(1)364 })365 ).toMatchSnapshot()fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
lines 366–369why, one step back366}367368// For defusing draft proxies.369function defuseProxies(fn) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 370–374why, one step back370 return Object.assign((...args) => {371 expect(args[0].finalized).toBeTruthy()372 args[0].draft = args[0].drafts = null373 fn(...args)374 }, fn)fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
lines 375–377why, one step back375}376377expect.extend({style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 378–386why, one step back378 toShallowEqual(received, expected) {379 const match = matchers.toBe(received, expected)380 return match.pass || !received || typeof received !== "object"381 ? match382 : !Array.isArray(expected) ||383 (Array.isArray(received) && received.length === expected.length)384 ? matchers.toEqual(received, expected)385 : match386 }fix(test): faulty .flowconfigaleclarsonbefore adoptione8fe4ba
line 387why, one step back387})