manual.js
This file
Path
__tests__/manual.js
136 lines · 3.8 KiB · JavaScript
Last changed by
Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
History of this fileTake it
Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 1–136why, one step back1"use strict"2import {3 setUseProxies,4 createDraft,5 finishDraft,6 produce,7 isDraft8} from "../src/index"910runTests("proxy", true)11runTests("es5", false)1213function runTests(name, useProxies) {14 describe("manual - " + name, () => {15 setUseProxies(useProxies)1617 it("should check arguments", () => {18 expect(() => createDraft(3)).toThrowErrorMatchingSnapshot()19 const buf = new Buffer([])20 expect(() => createDraft(buf)).toThrowErrorMatchingSnapshot()21 expect(() => finishDraft({})).toThrowErrorMatchingSnapshot()22 })2324 it("should support manual drafts", () => {25 const state = [{}, {}, {}]2627 const draft = createDraft(state)28 draft.forEach((item, index) => {29 item.index = index30 })3132 const result = finishDraft(draft)3334 expect(result).not.toBe(state)35 expect(result).toEqual([{index: 0}, {index: 1}, {index: 2}])36 expect(state).toEqual([{}, {}, {}])37 })3839 it("cannot modify after finish", () => {40 const state = {a: 1}4142 const draft = createDraft(state)43 draft.a = 244 expect(finishDraft(draft)).toEqual({a: 2})45 expect(() => {46 draft.a = 347 }).toThrowErrorMatchingSnapshot()48 })4950 it("should support patches drafts", () => {51 const state = {a: 1}5253 const draft = createDraft(state)54 draft.a = 255 draft.b = 35657 const listener = jest.fn()58 const result = finishDraft(draft, listener)5960 expect(result).not.toBe(state)61 expect(result).toEqual({a: 2, b: 3})62 expect(listener.mock.calls).toMatchSnapshot()63 })6465 it("should handle multiple create draft calls", () => {66 const state = {a: 1}6768 const draft = createDraft(state)69 draft.a = 27071 const draft2 = createDraft(state)72 draft2.b = 37374 const result = finishDraft(draft)7576 expect(result).not.toBe(state)77 expect(result).toEqual({a: 2})7879 draft2.a = 480 const result2 = finishDraft(draft2)81 expect(result2).not.toBe(result)82 expect(result2).toEqual({a: 4, b: 3})83 })8485 it("combines with produce - 1", () => {86 const state = {a: 1}8788 const draft = createDraft(state)89 draft.a = 290 const res1 = produce(draft, d => {91 d.b = 392 })93 draft.b = 494 const res2 = finishDraft(draft)95 expect(res1).toEqual({a: 2, b: 3})96 expect(res2).toEqual({a: 2, b: 4})97 })9899 it("combines with produce - 2", () => {100 const state = {a: 1}101102 const res1 = produce(state, draft => {103 draft.b = 3104 const draft2 = createDraft(draft)105 draft.c = 4106 draft2.d = 5107 const res2 = finishDraft(draft2)108 expect(res2).toEqual({109 a: 1,110 b: 3,111 d: 5112 })113 draft.d = 2114 })115 expect(res1).toEqual({116 a: 1,117 b: 3,118 c: 4,119 d: 2120 })121 })122123 it("should not finish drafts from produce", () => {124 produce({x: 1}, draft => {125 expect(() => finishDraft(draft)).toThrowErrorMatchingSnapshot()126 })127 })128129 it("should not finish twice", () => {130 const draft = createDraft({a: 1})131 draft.a++132 finishDraft(draft)133 expect(() => finishDraft(draft)).toThrowErrorMatchingSnapshot()134 })135 })136}