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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228"use strict"import {Immer, setUseProxies} from "../src/index"import matchers from "expect/build/matchers"
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("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("onCopy()", () => { useSharedTests(() => onCopy) it("is called in the right order", () => { const calls = [] onCopy.mockImplementation(s => { calls.push(s.base) }) const base = {a: {b: {c: 1}}} produce(base, s => { delete s.a.b.c }) expect(calls).toShallowEqual([base.a.b, base.a, 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 }})