hooks.js
This file
Path
__tests__/hooks.js
227 lines · 7.1 KiB · JavaScript
Last changed by
test: hooks in es5aleclarsonbefore adoption271cd1b
History of this fileTake it
test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744
line 1why, one step back1"use strict"test: hooks in es5aleclarsonbefore adoption271cd1b
line 2why, one step back2import {Immer, setUseProxies} from "../src/index"test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744
lines 3–4why, one step back3import matchers from "expect/build/matchers"4test: hooks in es5aleclarsonbefore adoption271cd1b
lines 5–8why, one step back5describe("hooks (proxy) -", () => createHookTests(true))6describe("hooks (es5) -", () => createHookTests(false))78function createHookTests(useProxies) {test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744
lines 9–10why, one step back9 let produce, onAssign, onDelete, onCopy10test: hooks in es5aleclarsonbefore adoption271cd1b
lines 11–12why, one step back11 beforeEach(() => {12 ;({produce, onAssign, onDelete, onCopy} = new Immer({test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744
line 13why, one step back13 autoFreeze: true,test: hooks in es5aleclarsonbefore adoption271cd1b
line 14why, one step back14 useProxies,test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744
lines 15–18why, one step back15 onAssign: defuseProxies(jest.fn().mockName("onAssign")),16 onDelete: defuseProxies(jest.fn().mockName("onDelete")),17 onCopy: defuseProxies(jest.fn().mockName("onCopy"))18 }))test: hooks in es5aleclarsonbefore adoption271cd1b
line 19why, one step back19 })test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744
lines 20–196why, one step back2021 describe("onAssign()", () => {22 useSharedTests(() => onAssign)23 describe("when draft is an object", () => {24 test("assign", () => {25 produce({a: 0, b: 0, c: 0}, s => {26 s.a++27 s.c++28 })29 expectCalls(onAssign)30 })31 test("assign (no change)", () => {32 produce({a: 0}, s => {33 s.a = 034 })35 expect(onAssign).not.toBeCalled()36 })37 test("delete", () => {38 produce({a: 1}, s => {39 delete s.a40 })41 expect(onAssign).not.toBeCalled()42 })43 test("nested assignments", () => {44 produce({a: {b: {c: 1, d: 1, e: 1}}}, s => {45 const {b} = s.a46 b.c = 247 delete b.d48 b.e = 1 // no-op49 })50 expectCalls(onAssign)51 })52 })53 describe("when draft is an array", () => {54 test("assign", () => {55 produce([1], s => {56 s[0] = 057 })58 expectCalls(onAssign)59 })60 test("push", () => {61 produce([], s => {62 s.push(4)63 })64 expectCalls(onAssign)65 })66 test("pop", () => {67 produce([1], s => {68 s.pop()69 })70 expect(onAssign).not.toBeCalled()71 })72 test("unshift", () => {73 produce([1], s => {74 s.unshift(0)75 })76 expectCalls(onAssign)77 })78 test("length = 0", () => {79 produce([1], s => {80 s.length = 081 })82 expect(onAssign).not.toBeCalled()83 })84 test("splice (length += 1)", () => {85 produce([1, 2, 3], s => {86 s.splice(1, 1, 0, 0)87 })88 expectCalls(onAssign)89 })90 test("splice (length += 0)", () => {91 produce([1, 2, 3], s => {92 s.splice(1, 1, 0)93 })94 expectCalls(onAssign)95 })96 test("splice (length -= 1)", () => {97 produce([1, 2, 3], s => {98 s.splice(0, 2, 6)99 })100 expectCalls(onAssign)101 })102 })103 describe("when a draft is moved into a new object", () => {104 it("is called in the right order", () => {105 const calls = []106 onAssign.mockImplementation((_, prop) => {107 calls.push(prop)108 })109 produce({a: {b: 1, c: {}}}, s => {110 s.a.b = 0111 s.a.c.d = 1112 s.x = {y: {z: s.a}}113 delete s.a114 })115 // Sibling properties use enumeration order, which means new116 // properties come last among their siblings. The deepest117 // properties always come first in their ancestor chain.118 expect(calls).toEqual(["b", "d", "c", "x"])119 })120 })121 })122123 describe("onDelete()", () => {124 useSharedTests(() => onDelete)125 describe("when draft is an object -", () => {126 test("delete", () => {127 produce({a: 1, b: 1, c: 1}, s => {128 delete s.a129 delete s.c130 })131 expectCalls(onDelete)132 })133 test("delete (no change)", () => {134 produce({}, s => {135 delete s.a136 })137 expect(onDelete).not.toBeCalled()138 })139 test("nested deletions", () => {140 produce({a: {b: {c: 1}}}, s => {141 delete s.a.b.c142 })143 expectCalls(onDelete)144 })145 })146 describe("when draft is an array -", () => {147 test("pop", () => {148 produce([1], s => {149 s.pop()150 })151 expectCalls(onDelete)152 })153 test("length = 0", () => {154 produce([1], s => {155 s.length = 0156 })157 expectCalls(onDelete)158 })159 test("splice (length -= 1)", () => {160 produce([1, 2, 3], s => {161 s.splice(0, 2, 6)162 })163 expectCalls(onDelete)164 })165 })166 })167168 describe("onCopy()", () => {169 useSharedTests(() => onCopy)170 it("is called in the right order", () => {171 const calls = []172 onCopy.mockImplementation(s => {173 calls.push(s.base)174 })175 const base = {a: {b: {c: 1}}}176 produce(base, s => {177 delete s.a.b.c178 })179 expect(calls).toShallowEqual([base.a.b, base.a, base])180 })181 })182183 function useSharedTests(getHook) {184 it("is called before the parent is frozen", () => {185 const hook = getHook()186 hook.mockImplementation(s => {187 // Parent object must not be frozen.188 expect(Object.isFrozen(s.base)).toBeFalsy()189 })190 produce({a: {b: {c: 0}}}, s => {191 if (hook == onDelete) delete s.a.b.c192 else s.a.b.c = 1193 })194 expect(hook).toHaveBeenCalledTimes(hook == onDelete ? 1 : 3)195 })196 }test: hooks in es5aleclarsonbefore adoption271cd1b
line 197why, one step back197}test: hooks (onAssign, onDelete, onCopy)aleclarsonbefore adoptionab05744
lines 198–227why, one step back198199// Produce a snapshot of the hook arguments (minus any draft state).200function expectCalls(hook) {201 expect(202 hook.mock.calls.map(call => {203 return call.slice(1)204 })205 ).toMatchSnapshot()206}207208// For defusing draft proxies.209function defuseProxies(fn) {210 return Object.assign((...args) => {211 expect(args[0].finalized).toBeTruthy()212 args[0].draft = args[0].drafts = null213 fn(...args)214 }, fn)215}216217expect.extend({218 toShallowEqual(received, expected) {219 const match = matchers.toBe(received, expected)220 return match.pass || !received || typeof received !== "object"221 ? match222 : !Array.isArray(expected) ||223 (Array.isArray(received) && received.length === expected.length)224 ? matchers.toEqual(received, expected)225 : match226 }227})