-
-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
706 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// 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 not finish drafts from produce 1`] = `"The draft provided was not created using \`createDraft\`"`; | ||
|
||
exports[`manual - es5 should not finish twice 1`] = `"The draft provided was has already been finished"`; | ||
|
||
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 not finish drafts from produce 1`] = `"The draft provided was not created using \`createDraft\`"`; | ||
|
||
exports[`manual - proxy should not finish twice 1`] = `"The draft provided was has already been finished"`; | ||
|
||
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", | ||
], | ||
}, | ||
], | ||
], | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
"use strict" | ||
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)).toThrowErrorMatchingSnapshot() | ||
const buf = new Buffer([]) | ||
expect(() => createDraft(buf)).toThrowErrorMatchingSnapshot() | ||
expect(() => finishDraft({})).toThrowErrorMatchingSnapshot() | ||
}) | ||
|
||
it("should support manual drafts", () => { | ||
const state = [{}, {}, {}] | ||
|
||
const draft = createDraft(state) | ||
draft.forEach((item, index) => { | ||
item.index = index | ||
}) | ||
|
||
const result = finishDraft(draft) | ||
|
||
expect(result).not.toBe(state) | ||
expect(result).toEqual([{index: 0}, {index: 1}, {index: 2}]) | ||
expect(state).toEqual([{}, {}, {}]) | ||
}) | ||
|
||
it("cannot modify after finish", () => { | ||
const state = {a: 1} | ||
|
||
const draft = createDraft(state) | ||
draft.a = 2 | ||
expect(finishDraft(draft)).toEqual({a: 2}) | ||
expect(() => { | ||
draft.a = 3 | ||
}).toThrowErrorMatchingSnapshot() | ||
}) | ||
|
||
it("should support patches drafts", () => { | ||
const state = {a: 1} | ||
|
||
const draft = createDraft(state) | ||
draft.a = 2 | ||
draft.b = 3 | ||
|
||
const listener = jest.fn() | ||
const result = finishDraft(draft, listener) | ||
|
||
expect(result).not.toBe(state) | ||
expect(result).toEqual({a: 2, b: 3}) | ||
expect(listener.mock.calls).toMatchSnapshot() | ||
}) | ||
|
||
it("should handle multiple create draft calls", () => { | ||
const state = {a: 1} | ||
|
||
const draft = createDraft(state) | ||
draft.a = 2 | ||
|
||
const draft2 = createDraft(state) | ||
draft2.b = 3 | ||
|
||
const result = finishDraft(draft) | ||
|
||
expect(result).not.toBe(state) | ||
expect(result).toEqual({a: 2}) | ||
|
||
draft2.a = 4 | ||
const result2 = finishDraft(draft2) | ||
expect(result2).not.toBe(result) | ||
expect(result2).toEqual({a: 4, b: 3}) | ||
}) | ||
|
||
it("combines with produce - 1", () => { | ||
const state = {a: 1} | ||
|
||
const draft = createDraft(state) | ||
draft.a = 2 | ||
const res1 = produce(draft, d => { | ||
d.b = 3 | ||
}) | ||
draft.b = 4 | ||
const res2 = finishDraft(draft) | ||
expect(res1).toEqual({a: 2, b: 3}) | ||
expect(res2).toEqual({a: 2, b: 4}) | ||
}) | ||
|
||
it("combines with produce - 2", () => { | ||
const state = {a: 1} | ||
|
||
const res1 = produce(state, draft => { | ||
draft.b = 3 | ||
const draft2 = createDraft(draft) | ||
draft.c = 4 | ||
draft2.d = 5 | ||
const res2 = finishDraft(draft2) | ||
expect(res2).toEqual({ | ||
a: 1, | ||
b: 3, | ||
d: 5 | ||
}) | ||
draft.d = 2 | ||
}) | ||
expect(res1).toEqual({ | ||
a: 1, | ||
b: 3, | ||
c: 4, | ||
d: 2 | ||
}) | ||
}) | ||
|
||
it("should not finish drafts from produce", () => { | ||
produce({x: 1}, draft => { | ||
expect(() => finishDraft(draft)).toThrowErrorMatchingSnapshot() | ||
}) | ||
}) | ||
|
||
it("should not finish twice", () => { | ||
const draft = createDraft({a: 1}) | ||
draft.a++ | ||
finishDraft(draft) | ||
expect(() => finishDraft(draft)).toThrowErrorMatchingSnapshot() | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.