manual.js
This file
Path
__tests__/manual.js
152 lines · 3.2 KiB · JavaScript
Last changed by
chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5
History of this fileTake it
The walk stopped after 200 commits: lines older than 07f5cc9 point at it.
chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 1why, one step back1"use strict"chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5
line 2why, one step back2import {vi} from "vitest"chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 3–8why, one step back3import {4 setUseProxies,5 createDraft,6 finishDraft,7 produce,8 isDraft,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 9why, one step back9 enablePatcheschore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 10–11why, one step back10} from "../src/immer"11Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 12why, one step back12enablePatches()chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 13–17why, one step back1314const isProd = process.env.NODE_ENV === "production"1516runTests("proxy", true)17Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 18why, one step back18function runTests(name) {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 19–52why, one step back19 describe("manual - " + name, () => {20 it("should check arguments", () => {21 expect(() => createDraft(3)).toThrowErrorMatchingSnapshot()22 const buf = Buffer.from([])23 expect(() => createDraft(buf)).toThrowErrorMatchingSnapshot()24 expect(() => finishDraft({})).toThrowErrorMatchingSnapshot()25 })2627 it("should support manual drafts", () => {28 const state = [{}, {}, {}]2930 const draft = createDraft(state)31 draft.forEach((item, index) => {32 item.index = index33 })3435 const result = finishDraft(draft)3637 expect(result).not.toBe(state)38 expect(result).toEqual([{index: 0}, {index: 1}, {index: 2}])39 expect(state).toEqual([{}, {}, {}])40 })4142 if (!isProd)43 it("cannot modify after finish", () => {44 const state = {a: 1}4546 const draft = createDraft(state)47 draft.a = 248 expect(finishDraft(draft)).toEqual({a: 2})49 expect(() => {50 draft.a = 351 }).toThrowErrorMatchingSnapshot()52 })Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 53–63why, one step back5354 it("cannot finishDraft twice", () => {55 const state = {a: 1}5657 const draft = createDraft(state)58 draft.a = 259 expect(finishDraft(draft)).toEqual({a: 2})60 expect(() => {61 finishDraft(draft)62 }).toThrowErrorMatchingSnapshot()63 })chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 64–71why, one step back6465 it("should support patches drafts", () => {66 const state = {a: 1}6768 const draft = createDraft(state)69 draft.a = 270 draft.b = 371chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5
line 72why, one step back72 const listener = vi.fn()chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 73–152why, one step back73 const result = finishDraft(draft, listener)7475 expect(result).not.toBe(state)76 expect(result).toEqual({a: 2, b: 3})77 expect(listener.mock.calls).toMatchSnapshot()78 })7980 it("should handle multiple create draft calls", () => {81 const state = {a: 1}8283 const draft = createDraft(state)84 draft.a = 28586 const draft2 = createDraft(state)87 draft2.b = 38889 const result = finishDraft(draft)9091 expect(result).not.toBe(state)92 expect(result).toEqual({a: 2})9394 draft2.a = 495 const result2 = finishDraft(draft2)96 expect(result2).not.toBe(result)97 expect(result2).toEqual({a: 4, b: 3})98 })99100 it("combines with produce - 1", () => {101 const state = {a: 1}102103 const draft = createDraft(state)104 draft.a = 2105 const res1 = produce(draft, d => {106 d.b = 3107 })108 draft.b = 4109 const res2 = finishDraft(draft)110 expect(res1).toEqual({a: 2, b: 3})111 expect(res2).toEqual({a: 2, b: 4})112 })113114 it("combines with produce - 2", () => {115 const state = {a: 1}116117 const res1 = produce(state, draft => {118 draft.b = 3119 const draft2 = createDraft(draft)120 draft.c = 4121 draft2.d = 5122 const res2 = finishDraft(draft2)123 expect(res2).toEqual({124 a: 1,125 b: 3,126 d: 5127 })128 draft.d = 2129 })130 expect(res1).toEqual({131 a: 1,132 b: 3,133 c: 4,134 d: 2135 })136 })137138 !global.USES_BUILD &&139 it("should not finish drafts from produce", () => {140 produce({x: 1}, draft => {141 expect(() => finishDraft(draft)).toThrowErrorMatchingSnapshot()142 })143 })144145 it("should not finish twice", () => {146 const draft = createDraft({a: 1})147 draft.a++148 finishDraft(draft)149 expect(() => finishDraft(draft)).toThrowErrorMatchingSnapshot()150 })151 })152}