Skip to content

Commit

Permalink
test: use snapshots in createDraft tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Feb 2, 2019
1 parent 44ef0c8 commit 13b0ed0
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 40 deletions.
93 changes: 93 additions & 0 deletions __tests__/__snapshots__/manual.js.snap
@@ -0,0 +1,93 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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}"`;

exports[`manual - es5 should check arguments 1`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;

exports[`manual - es5 should check arguments 2`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;

exports[`manual - es5 should check arguments 3`] = `"First argument to finishDraft should be an object from createDraft."`;

exports[`manual - es5 should support patches drafts 1`] = `
Array [
Array [
Array [
Object {
"op": "replace",
"path": Array [
"a",
],
"value": 2,
},
Object {
"op": "add",
"path": Array [
"b",
],
"value": 3,
},
],
Array [
Object {
"op": "replace",
"path": Array [
"a",
],
"value": 1,
},
Object {
"op": "remove",
"path": Array [
"b",
],
},
],
],
]
`;

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}"`;

exports[`manual - proxy should check arguments 1`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;

exports[`manual - proxy should check arguments 2`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;

exports[`manual - proxy should check arguments 3`] = `"First argument to finishDraft should be an object from createDraft."`;

exports[`manual - proxy should support patches drafts 1`] = `
Array [
Array [
Array [
Object {
"op": "replace",
"path": Array [
"a",
],
"value": 2,
},
Object {
"op": "add",
"path": Array [
"b",
],
"value": 3,
},
],
Array [
Object {
"op": "replace",
"path": Array [
"a",
],
"value": 1,
},
Object {
"op": "remove",
"path": Array [
"b",
],
},
],
],
]
`;
55 changes: 15 additions & 40 deletions __tests__/manual.js
@@ -1,5 +1,11 @@
"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)
Expand All @@ -9,15 +15,10 @@ function runTests(name, useProxies) {
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", () => {
Expand All @@ -43,7 +44,7 @@ function runTests(name, useProxies) {
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", () => {
Expand All @@ -53,38 +54,12 @@ function runTests(name, useProxies) {
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", () => {
Expand Down

0 comments on commit 13b0ed0

Please sign in to comment.