Skip to content
Myr

test: hooks (onAssign, onDelete, onCopy)

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

authored by aleclarson · committed by aleclarson · parent 7f50364 · Browse files · 2 files+371 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

2 files+371 added−0 removed

  • tests:+371 added−0 removed

2 files+371 added−0 removed

added:__tests__/__snapshots__/hooks.js.snap+146 added
1// Jest Snapshot v1, https://goo.gl/fbAQLP23exports[`hooks - onAssign() when draft is an array assign 1`] = `4Array [5 Array [6 0,7 0,8 ],9]10`;1112exports[`hooks - onAssign() when draft is an array push 1`] = `13Array [14 Array [15 0,16 4,17 ],18]19`;2021exports[`hooks - onAssign() when draft is an array splice (length += 0) 1`] = `22Array [23 Array [24 1,25 0,26 ],27]28`;2930exports[`hooks - onAssign() when draft is an array splice (length += 1) 1`] = `31Array [32 Array [33 1,34 0,35 ],36 Array [37 2,38 0,39 ],40 Array [41 3,42 3,43 ],44]45`;4647exports[`hooks - onAssign() when draft is an array splice (length -= 1) 1`] = `48Array [49 Array [50 0,51 6,52 ],53 Array [54 1,55 3,56 ],57]58`;5960exports[`hooks - onAssign() when draft is an array unshift 1`] = `61Array [62 Array [63 0,64 0,65 ],66 Array [67 1,68 1,69 ],70]71`;7273exports[`hooks - onAssign() when draft is an object assign 1`] = `74Array [75 Array [76 "a",77 1,78 ],79 Array [80 "c",81 1,82 ],83]84`;8586exports[`hooks - onAssign() when draft is an object nested assignments 1`] = `87Array [88 Array [89 "c",90 2,91 ],92 Array [93 "b",94 Object {95 "c": 2,96 "e": 1,97 },98 ],99 Array [100 "a",101 Object {102 "b": Object {103 "c": 2,104 "e": 1,105 },106 },107 ],108]109`;110111exports[`hooks - onDelete() when draft is an array - length = 0 1`] = `Array []`;112113exports[`hooks - onDelete() when draft is an array - pop 1`] = `114Array [115 Array [116 "0",117 ],118]119`;120121exports[`hooks - onDelete() when draft is an array - splice (length -= 1) 1`] = `122Array [123 Array [124 "2",125 ],126]127`;128129exports[`hooks - onDelete() when draft is an object - delete 1`] = `130Array [131 Array [132 "a",133 ],134 Array [135 "c",136 ],137]138`;139140exports[`hooks - onDelete() when draft is an object - nested deletions 1`] = `141Array [142 Array [143 "c",144 ],145]146`;
added:__tests__/hooks.js+225 added
1"use strict"2import {Immer} from "../src/index"3import matchers from "expect/build/matchers"45describe("hooks -", () => {6 let produce, onAssign, onDelete, onCopy78 const reset = () =>9 ({produce, onAssign, onDelete, onCopy} = new Immer({10 autoFreeze: true,11 onAssign: defuseProxies(jest.fn().mockName("onAssign")),12 onDelete: defuseProxies(jest.fn().mockName("onDelete")),13 onCopy: defuseProxies(jest.fn().mockName("onCopy"))14 }))1516 describe("onAssign()", () => {17 beforeEach(reset)18 useSharedTests(() => onAssign)19 describe("when draft is an object", () => {20 test("assign", () => {21 produce({a: 0, b: 0, c: 0}, s => {22 s.a++23 s.c++24 })25 expectCalls(onAssign)26 })27 test("assign (no change)", () => {28 produce({a: 0}, s => {29 s.a = 030 })31 expect(onAssign).not.toBeCalled()32 })33 test("delete", () => {34 produce({a: 1}, s => {35 delete s.a36 })37 expect(onAssign).not.toBeCalled()38 })39 test("nested assignments", () => {40 produce({a: {b: {c: 1, d: 1, e: 1}}}, s => {41 const {b} = s.a42 b.c = 243 delete b.d44 b.e = 1 // no-op45 })46 expectCalls(onAssign)47 })48 })49 describe("when draft is an array", () => {50 test("assign", () => {51 produce([1], s => {52 s[0] = 053 })54 expectCalls(onAssign)55 })56 test("push", () => {57 produce([], s => {58 s.push(4)59 })60 expectCalls(onAssign)61 })62 test("pop", () => {63 produce([1], s => {64 s.pop()65 })66 expect(onAssign).not.toBeCalled()67 })68 test("unshift", () => {69 produce([1], s => {70 s.unshift(0)71 })72 expectCalls(onAssign)73 })74 test("length = 0", () => {75 produce([1], s => {76 s.length = 077 })78 expect(onAssign).not.toBeCalled()79 })80 test("splice (length += 1)", () => {81 produce([1, 2, 3], s => {82 s.splice(1, 1, 0, 0)83 })84 expectCalls(onAssign)85 })86 test("splice (length += 0)", () => {87 produce([1, 2, 3], s => {88 s.splice(1, 1, 0)89 })90 expectCalls(onAssign)91 })92 test("splice (length -= 1)", () => {93 produce([1, 2, 3], s => {94 s.splice(0, 2, 6)95 })96 expectCalls(onAssign)97 })98 })99 describe("when a draft is moved into a new object", () => {100 it("is called in the right order", () => {101 const calls = []102 onAssign.mockImplementation((_, prop) => {103 calls.push(prop)104 })105 produce({a: {b: 1, c: {}}}, s => {106 s.a.b = 0107 s.a.c.d = 1108 s.x = {y: {z: s.a}}109 delete s.a110 })111 // Sibling properties use enumeration order, which means new112 // properties come last among their siblings. The deepest113 // properties always come first in their ancestor chain.114 expect(calls).toEqual(["b", "d", "c", "x"])115 })116 })117 })118119 describe("onDelete()", () => {120 beforeEach(reset)121 useSharedTests(() => onDelete)122 describe("when draft is an object -", () => {123 test("delete", () => {124 produce({a: 1, b: 1, c: 1}, s => {125 delete s.a126 delete s.c127 })128 expectCalls(onDelete)129 })130 test("delete (no change)", () => {131 produce({}, s => {132 delete s.a133 })134 expect(onDelete).not.toBeCalled()135 })136 test("nested deletions", () => {137 produce({a: {b: {c: 1}}}, s => {138 delete s.a.b.c139 })140 expectCalls(onDelete)141 })142 })143 describe("when draft is an array -", () => {144 test("pop", () => {145 produce([1], s => {146 s.pop()147 })148 expectCalls(onDelete)149 })150 test("length = 0", () => {151 produce([1], s => {152 s.length = 0153 })154 expectCalls(onDelete)155 })156 test("splice (length -= 1)", () => {157 produce([1, 2, 3], s => {158 s.splice(0, 2, 6)159 })160 expectCalls(onDelete)161 })162 })163 })164165 describe("onCopy()", () => {166 beforeEach(reset)167 useSharedTests(() => onCopy)168 it("is called in the right order", () => {169 const calls = []170 onCopy.mockImplementation(s => {171 calls.push(s.base)172 })173 const base = {a: {b: {c: 1}}}174 produce(base, s => {175 delete s.a.b.c176 })177 expect(calls).toShallowEqual([base.a.b, base.a, base])178 })179 })180181 function useSharedTests(getHook) {182 it("is called before the parent is frozen", () => {183 const hook = getHook()184 hook.mockImplementation(s => {185 // Parent object must not be frozen.186 expect(Object.isFrozen(s.base)).toBeFalsy()187 })188 produce({a: {b: {c: 0}}}, s => {189 if (hook == onDelete) delete s.a.b.c190 else s.a.b.c = 1191 })192 expect(hook).toHaveBeenCalledTimes(hook == onDelete ? 1 : 3)193 })194 }195})196197// Produce a snapshot of the hook arguments (minus any draft state).198function expectCalls(hook) {199 expect(200 hook.mock.calls.map(call => {201 return call.slice(1)202 })203 ).toMatchSnapshot()204}205206// For defusing draft proxies.207function defuseProxies(fn) {208 return Object.assign((...args) => {209 expect(args[0].finalized).toBeTruthy()210 args[0].draft = args[0].drafts = null211 fn(...args)212 }, fn)213}214215expect.extend({216 toShallowEqual(received, expected) {217 const match = matchers.toBe(received, expected)218 return match.pass || !received || typeof received !== "object"219 ? match220 : !Array.isArray(expected) ||221 (Array.isArray(received) && received.length === expected.length)222 ? matchers.toEqual(received, expected)223 : match224 }225})