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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388"use strict"import {Immer, setUseProxies} from "../src/index"import matchers from "expect/build/matchers"import {isSet} from "../src/common"
describe("hooks (proxy) -", () => createHookTests(true))describe("hooks (es5) -", () => createHookTests(false))
function createHookTests(useProxies) { let produce, onAssign, onDelete, onCopy
beforeEach(() => { ;({produce, onAssign, onDelete, onCopy} = new Immer({ autoFreeze: true, useProxies, onAssign: defuseProxies(jest.fn().mockName("onAssign")), onDelete: defuseProxies(jest.fn().mockName("onDelete")), onCopy: defuseProxies(jest.fn().mockName("onCopy")) })) })
describe("onAssign()", () => { useSharedTests(() => onAssign) describe("when draft is an object", () => { test("assign", () => { produce({a: 0, b: 0, c: 0}, s => { s.a++ s.c++ }) expectCalls(onAssign) }) test("assign (no change)", () => { produce({a: 0}, s => { s.a = 0 }) expect(onAssign).not.toBeCalled() }) test("delete", () => { produce({a: 1}, s => { delete s.a }) expect(onAssign).not.toBeCalled() }) test("nested assignments", () => { produce({a: {b: {c: 1, d: 1, e: 1}}}, s => { const {b} = s.a b.c = 2 delete b.d b.e = 1 // no-op }) expectCalls(onAssign) }) }) describe("when draft is an array", () => { test("assign", () => { produce([1], s => { s[0] = 0 }) expectCalls(onAssign) }) test("push", () => { produce([], s => { s.push(4) }) expectCalls(onAssign) }) test("pop", () => { produce([1], s => { s.pop() }) expect(onAssign).not.toBeCalled() }) test("unshift", () => { produce([1], s => { s.unshift(0) }) expectCalls(onAssign) }) test("length = 0", () => { produce([1], s => { s.length = 0 }) expect(onAssign).not.toBeCalled() }) test("splice (length += 1)", () => { produce([1, 2, 3], s => { s.splice(1, 1, 0, 0) }) expectCalls(onAssign) }) test("splice (length += 0)", () => { produce([1, 2, 3], s => { s.splice(1, 1, 0) }) expectCalls(onAssign) }) test("splice (length -= 1)", () => { produce([1, 2, 3], s => { s.splice(0, 2, 6) }) expectCalls(onAssign) }) }) describe("when a draft is moved into a new object", () => { it("is called in the right order", () => { const calls = [] onAssign.mockImplementation((_, prop) => { calls.push(prop) }) produce({a: {b: 1, c: {}}}, s => { s.a.b = 0 s.a.c.d = 1 s.x = {y: {z: s.a}} delete s.a }) // Sibling properties use enumeration order, which means new // properties come last among their siblings. The deepest // properties always come first in their ancestor chain. expect(calls).toEqual(["b", "d", "c", "x"]) }) })
describe("when draft is a Map", () => { test("assign", () => { const key1 = {prop: "val1"} const key2 = {prop: "val2"} produce( new Map([ ["a", 0], [key1, 1], [key2, 2] ]), s => { s.set("a", 10) s.set(key1, 11) } ) expectCalls(onAssign) }) test("assign (no change)", () => { produce(new Map([["a", 0]]), s => { s.set("a", 0) }) expect(onAssign).not.toBeCalled() }) test("delete", () => { produce(new Map([["a", 0]]), s => { s.delete("a") }) expect(onAssign).not.toBeCalled() }) test("nested assignments", () => { const key1 = {prop: "val1"} produce( new Map([ [ "a", new Map([ [ key1, new Map([ ["b", 1], ["c", 1], ["d", 1] ]) ] ]) ] ]), s => { const nested = s.get("a").get(key1) nested.set("b", 2) nested.delete("c") nested.set("d", 1) // no-op } ) expectCalls(onAssign) }) })
describe("when draft is a Set", () => { test("assign", () => { produce({a: new Set([1, 2, 3])}, s => { s.a.add(4) }) expectCalls(onAssign) }) test("assign (no change)", () => { produce({a: new Set([1, 2, 3])}, s => { s.a.add(3) }) expect(onAssign).not.toBeCalled() }) // Any mutation of a Set results in a new assignment. Including deletes. test("delete", () => { produce({a: new Set([1, 2, 3])}, s => { s.a.delete(1) }) expectCalls(onAssign) }) test("nested assignments", () => { const val1 = {prop: "val1"} produce({a: new Set(["a", new Set([val1, 1])])}, s => { let nested s.a.forEach(value => { if (value instanceof Set) { nested = value } }) nested.add(2) nested.delete(val1) nested.add(1) // no-op }) expectCalls(onAssign) }) }) })
describe("onDelete()", () => { useSharedTests(() => onDelete) describe("when draft is an object -", () => { test("delete", () => { produce({a: 1, b: 1, c: 1}, s => { delete s.a delete s.c }) expectCalls(onDelete) }) test("delete (no change)", () => { produce({}, s => { delete s.a }) expect(onDelete).not.toBeCalled() }) test("nested deletions", () => { produce({a: {b: {c: 1}}}, s => { delete s.a.b.c }) expectCalls(onDelete) }) }) describe("when draft is an array -", () => { test("pop", () => { produce([1], s => { s.pop() }) expectCalls(onDelete) }) test("length = 0", () => { produce([1], s => { s.length = 0 }) expectCalls(onDelete) }) test("splice (length -= 1)", () => { produce([1, 2, 3], s => { s.splice(0, 2, 6) }) expectCalls(onDelete) }) })
describe("when draft is a Map -", () => { test("delete", () => { const key1 = {prop: "val1"} const key2 = {prop: "val2"} produce( new Map([ ["a", 0], [key1, 1], [key2, 2] ]), s => { s.delete("a") s.delete(key1) } ) expectCalls(onDelete) }) test("delete (no change)", () => { produce(new Map(), s => { s.delete("a") }) expect(onDelete).not.toBeCalled() }) test("nested deletions", () => { const key1 = {prop: "val1"} produce(new Map([["a", new Map([[key1, new Map([["b", 1]])]])]]), s => { s.get("a") .get(key1) .delete("b") }) expectCalls(onDelete) }) })
describe("when draft is a Set -", () => { test("delete", () => { produce({a: new Set([1])}, s => { s.a.delete(1) }) expect(onDelete).not.toBeCalled() }) }) })
describe("onCopy()", () => { let calls beforeEach(() => { calls = [] onCopy.mockImplementation(s => { calls.push(s.base) }) })
useSharedTests(() => onCopy) it("is called in the right order for objects", () => { const base = {a: {b: {c: 1}}} produce(base, s => { delete s.a.b.c }) expect(calls).toShallowEqual([base.a.b, base.a, base]) })
it("is called in the right order for Maps", () => { const base = new Map([["a", new Map([["b", 0]])]]) produce(base, s => { s.get("a").delete("b") }) expect(calls).toShallowEqual([base.get("a"), base]) })
it("is called in the right order for Sets", () => { const item1 = {a: 0} const base = new Set([item1]) produce(base, s => { s.forEach(item => item.a++) }) expect(calls).toShallowEqual([item1, base]) }) })
function useSharedTests(getHook) { it("is called before the parent is frozen", () => { const hook = getHook() hook.mockImplementation(s => { // Parent object must not be frozen. expect(Object.isFrozen(s.base)).toBeFalsy() }) produce({a: {b: {c: 0}}}, s => { if (hook == onDelete) delete s.a.b.c else s.a.b.c = 1 }) expect(hook).toHaveBeenCalledTimes(hook == onDelete ? 1 : 3) }) }}
// Produce a snapshot of the hook arguments (minus any draft state).function expectCalls(hook) { expect( hook.mock.calls.map(call => { return call.slice(1) }) ).toMatchSnapshot()}
// For defusing draft proxies.function defuseProxies(fn) { return Object.assign((...args) => { expect(args[0].finalized).toBeTruthy() args[0].draft = args[0].drafts = null fn(...args) }, fn)}
expect.extend({ toShallowEqual(received, expected) { const match = matchers.toBe(received, expected) return match.pass || !received || typeof received !== "object" ? match : !Array.isArray(expected) || (Array.isArray(received) && received.length === expected.length) ? matchers.toEqual(received, expected) : match }})