test: use snapshots in createDraft tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by aleclarson · parent 44ef0c8 · Browse files · 2 files·+108 added−40 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+108 added−40 removed
- tests:+108 added−40 removed
Provenance
- Commit
13b0ed0- Tree
d301cc9- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
44ef0c8
2 files changed+108 added−40 removed
added:__tests__/__snapshots__/manual.js.snap+93 added
1+// Jest Snapshot v1, https://goo.gl/fbAQLP2+3+exports[`manual - es5 cannot modify after finish 1`] = `"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {\\"a\\":2}"`;4+5+exports[`manual - es5 should check arguments 1`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;6+7+exports[`manual - es5 should check arguments 2`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;8+9+exports[`manual - es5 should check arguments 3`] = `"First argument to finishDraft should be an object from createDraft."`;10+11+exports[`manual - es5 should support patches drafts 1`] = `12+Array [13+ Array [14+ Array [15+ Object {16+ "op": "replace",17+ "path": Array [18+ "a",19+ ],20+ "value": 2,21+ },22+ Object {23+ "op": "add",24+ "path": Array [25+ "b",26+ ],27+ "value": 3,28+ },29+ ],30+ Array [31+ Object {32+ "op": "replace",33+ "path": Array [34+ "a",35+ ],36+ "value": 1,37+ },38+ Object {39+ "op": "remove",40+ "path": Array [41+ "b",42+ ],43+ },44+ ],45+ ],46+]47+`;48+49+exports[`manual - proxy cannot modify after finish 1`] = `"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {\\"a\\":2}"`;50+51+exports[`manual - proxy should check arguments 1`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;52+53+exports[`manual - proxy should check arguments 2`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;54+55+exports[`manual - proxy should check arguments 3`] = `"First argument to finishDraft should be an object from createDraft."`;56+57+exports[`manual - proxy should support patches drafts 1`] = `58+Array [59+ Array [60+ Array [61+ Object {62+ "op": "replace",63+ "path": Array [64+ "a",65+ ],66+ "value": 2,67+ },68+ Object {69+ "op": "add",70+ "path": Array [71+ "b",72+ ],73+ "value": 3,74+ },75+ ],76+ Array [77+ Object {78+ "op": "replace",79+ "path": Array [80+ "a",81+ ],82+ "value": 1,83+ },84+ Object {85+ "op": "remove",86+ "path": Array [87+ "b",88+ ],89+ },90+ ],91+ ],92+]93+`;modified:__tests__/manual.js+15 added-40 removed
12234567891011121314151617121314151617181920181920212223242517 unmodified lines43444546464748495051525354555656575859575859606163646566676869707172737475767778798081828384858687626364656657 unmodified lines"use strict"import {setUseProxies, createDraft, finishDraft, produce} from "../src/index"import { setUseProxies, createDraft, finishDraft, produce, isDraft} from "../src/index"
runTests("proxy", true)runTests("es5", false)
function runTests(name, useProxies) { describe("manual - " + name, () => { setUseProxies(useProxies)
it("should check arguments", () => { expect(() => createDraft(3)).toThrow( "argument to createDraft should be a plain" ) expect(() => createDraft(new Buffer([]))).toThrow( "argument to createDraft should be a plain" ) expect(() => finishDraft({})).toThrow( "First argument to finishDraft should be " ) expect(() => createDraft(3)).toThrowErrorMatchingSnapshot() const buf = new Buffer([]) expect(() => createDraft(buf)).toThrowErrorMatchingSnapshot() expect(() => finishDraft({})).toThrowErrorMatchingSnapshot() })
it("should support manual drafts", () => { const state = [{}, {}, {}]17 unmodified lines draft.a = 2 expect(finishDraft(draft)).toEqual({a: 2}) expect(() => { draft.a = 3 }).toThrow("Cannot use a proxy that has been revoked") }).toThrowErrorMatchingSnapshot() })
it("should support patches drafts", () => { const state = {a: 1}
const draft = createDraft(state) draft.a = 2 draft.b = 3
const patches = [] const result = finishDraft(draft, (p, ip) => { patches.push(p, ip) }) const listener = jest.fn() const result = finishDraft(draft, listener)
expect(result).not.toBe(state) expect(result).toEqual({a: 2, b: 3}) expect(patches).toEqual([ [ { op: "replace", path: ["a"], value: 2 }, { op: "add", path: ["b"], value: 3 } ], [ { op: "replace", path: ["a"], value: 1 }, { op: "remove", path: ["b"] } ] ]) expect(listener.mock.calls).toMatchSnapshot() })
it("should handle multiple create draft calls", () => { const state = {a: 1}57 unmodified lines