Skip to content
Myr

hooks.js

This file

Path

__tests__/hooks.js

361 lines · 8.3 KiB · JavaScript

Last changed by

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

History of this file

Take it

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

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

test: hooks in es5aleclarsonbefore adoption271cd1b

line 2why, one step back
2import {Immer, setUseProxies} from "../src/index"

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

line 3why, one step back
3import matchers from "expect/build/matchers"

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 4why, one step back
4import {isSet} from "../src/common"

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

line 5why, one step back
5

test: hooks in es5aleclarsonbefore adoption271cd1b

lines 6–9why, one step back
6describe("hooks (proxy) -", () => createHookTests(true))
7describe("hooks (es5) -", () => createHookTests(false))89function createHookTests(useProxies) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 10why, one step back
10 let produce, onAssign, onDelete, onCopy

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

line 11why, one step back
11

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 12–20why, one step back
12 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 })

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

line 21why, one step back
21

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 22–121why, one step back
22 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–197why, one step back
122
123 describe("when draft is a Map", () => {124 test("assign", () => {125 const key1 = {prop: "val1"}126 const key2 = {prop: "val2"}127 produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {128 s.set("a", 10)129 s.set(key1, 11)130 })131 expectCalls(onAssign)132 })133 test("assign (no change)", () => {134 produce(new Map([["a", 0]]), s => {135 s.set("a", 0)136 })137 expect(onAssign).not.toBeCalled()138 })139 test("delete", () => {140 produce(new Map([["a", 0]]), s => {141 s.delete("a")142 })143 expect(onAssign).not.toBeCalled()144 })145 test("nested assignments", () => {146 const key1 = {prop: "val1"}147 produce(148 new Map([149 ["a", new Map([[key1, new Map([["b", 1], ["c", 1], ["d", 1]])]])]150 ]),151 s => {152 const nested = s.get("a").get(key1)153 nested.set("b", 2)154 nested.delete("c")155 nested.set("d", 1) // no-op156 }157 )158 expectCalls(onAssign)159 })160 })161162 describe("when draft is a Set", () => {163 test("assign", () => {164 produce({a: new Set([1, 2, 3])}, s => {165 s.a.add(4)166 })167 expectCalls(onAssign)168 })169 test("assign (no change)", () => {170 produce({a: new Set([1, 2, 3])}, s => {171 s.a.add(3)172 })173 expect(onAssign).not.toBeCalled()174 })175 // Any mutation of a Set results in a new assignment. Including deletes.176 test("delete", () => {177 produce({a: new Set([1, 2, 3])}, s => {178 s.a.delete(1)179 })180 expectCalls(onAssign)181 })182 test("nested assignments", () => {183 const val1 = {prop: "val1"}184 produce({a: new Set(["a", new Set([val1, 1])])}, s => {185 let nested186 s.a.forEach(value => {187 if (isSet(value)) {188 nested = value189 }190 })191 nested.add(2)192 nested.delete(val1)193 nested.add(1) // no-op194 })195 expectCalls(onAssign)196 })197 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 198why, one step back
198 })

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

line 199why, one step back
199

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 200–242why, one step back
200 describe("onDelete()", () => {
201 useSharedTests(() => onDelete)202 describe("when draft is an object -", () => {203 test("delete", () => {204 produce({a: 1, b: 1, c: 1}, s => {205 delete s.a206 delete s.c207 })208 expectCalls(onDelete)209 })210 test("delete (no change)", () => {211 produce({}, s => {212 delete s.a213 })214 expect(onDelete).not.toBeCalled()215 })216 test("nested deletions", () => {217 produce({a: {b: {c: 1}}}, s => {218 delete s.a.b.c219 })220 expectCalls(onDelete)221 })222 })223 describe("when draft is an array -", () => {224 test("pop", () => {225 produce([1], s => {226 s.pop()227 })228 expectCalls(onDelete)229 })230 test("length = 0", () => {231 produce([1], s => {232 s.length = 0233 })234 expectCalls(onDelete)235 })236 test("splice (length -= 1)", () => {237 produce([1, 2, 3], s => {238 s.splice(0, 2, 6)239 })240 expectCalls(onDelete)241 })242 })

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 243–278why, one step back
243
244 describe("when draft is a Map -", () => {245 test("delete", () => {246 const key1 = {prop: "val1"}247 const key2 = {prop: "val2"}248 produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {249 s.delete("a")250 s.delete(key1)251 })252 expectCalls(onDelete)253 })254 test("delete (no change)", () => {255 produce(new Map(), s => {256 s.delete("a")257 })258 expect(onDelete).not.toBeCalled()259 })260 test("nested deletions", () => {261 const key1 = {prop: "val1"}262 produce(new Map([["a", new Map([[key1, new Map([["b", 1]])]])]]), s => {263 s.get("a")264 .get(key1)265 .delete("b")266 })267 expectCalls(onDelete)268 })269 })270271 describe("when draft is a Set -", () => {272 test("delete", () => {273 produce({a: new Set([1])}, s => {274 s.a.delete(1)275 })276 expect(onDelete).not.toBeCalled()277 })278 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 279why, one step back
279 })

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

line 280why, one step back
280

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 281why, one step back
281 describe("onCopy()", () => {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 282–284why, one step back
282 let calls
283 beforeEach(() => {284 calls = []

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 285–287why, one step back
285 onCopy.mockImplementation(s => {
286 calls.push(s.base)287 })

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 288–291why, one step back
288 })
289290 useSharedTests(() => onCopy)291 it("is called in the right order for objects", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 292–296why, one step back
292 const base = {a: {b: {c: 1}}}
293 produce(base, s => {294 delete s.a.b.c295 })296 expect(calls).toShallowEqual([base.a.b, base.a, base])

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 297–313why, one step back
297 })
298299 it("is called in the right order for Maps", () => {300 const base = new Map([["a", new Map([["b", 0]])]])301 produce(base, s => {302 s.get("a").delete("b")303 })304 expect(calls).toShallowEqual([base.get("a"), base])305 })306307 it("is called in the right order for Sets", () => {308 const item1 = {a: 0}309 const base = new Set([item1])310 produce(base, s => {311 s.forEach(item => item.a++)312 })313 expect(calls).toShallowEqual([item1, base])

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 314–315why, one step back
314 })
315 })

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

line 316why, one step back
316

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 317–330why, one step back
317 function useSharedTests(getHook) {
318 it("is called before the parent is frozen", () => {319 const hook = getHook()320 hook.mockImplementation(s => {321 // Parent object must not be frozen.322 expect(Object.isFrozen(s.base)).toBeFalsy()323 })324 produce({a: {b: {c: 0}}}, s => {325 if (hook == onDelete) delete s.a.b.c326 else s.a.b.c = 1327 })328 expect(hook).toHaveBeenCalledTimes(hook == onDelete ? 1 : 3)329 })330 }

test: hooks in es5aleclarsonbefore adoption271cd1b

line 331why, one step back
331}

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

lines 332–334why, one step back
332
333// Produce a snapshot of the hook arguments (minus any draft state).334function expectCalls(hook) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 335–339why, one step back
335 expect(
336 hook.mock.calls.map(call => {337 return call.slice(1)338 })339 ).toMatchSnapshot()

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

lines 340–343why, one step back
340}
341342// For defusing draft proxies.343function defuseProxies(fn) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 344–348why, one step back
344 return Object.assign((...args) => {
345 expect(args[0].finalized).toBeTruthy()346 args[0].draft = args[0].drafts = null347 fn(...args)348 }, fn)

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

lines 349–351why, one step back
349}
350351expect.extend({

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 352–360why, one step back
352 toShallowEqual(received, expected) {
353 const match = matchers.toBe(received, expected)354 return match.pass || !received || typeof received !== "object"355 ? match356 : !Array.isArray(expected) ||357 (Array.isArray(received) && received.length === expected.length)358 ? matchers.toEqual(received, expected)359 : match360 }

test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744

line 361why, one step back
361})