chores: added additional invariants
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent c6cd632 · Browse files · 3 files·+28 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+28 added−1 removed
- tests:+21 added−0 removed
- other:+7 added−1 removed
Provenance
- Commit
b0d3ce3- Tree
85d0fbe- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
c6cd632
3 files changed+28 added−1 removed
modified:__tests__/__snapshots__/manual.js.snap+8 added
6 unmodified lines7 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 not finish drafts from produce 1`] = `"The draft provided was not created using \`createDraft\`"`;12+13+exports[`manual - es5 should not finish twice 1`] = `"The draft provided was has already been finished"`;14+15 exports[`manual - es5 should support patches drafts 1`] = `16 Array [17 Array [18 Array [38 unmodified lines57 exports[`manual - proxy should check arguments 2`] = `"First argument to createDraft should be a plain object, an array, or an immerable object."`;58 59 exports[`manual - proxy should check arguments 3`] = `"First argument to finishDraft should be an object from createDraft."`;60 61+exports[`manual - proxy should not finish drafts from produce 1`] = `"The draft provided was not created using \`createDraft\`"`;62+63+exports[`manual - proxy should not finish twice 1`] = `"The draft provided was has already been finished"`;64+65 exports[`manual - proxy should support patches drafts 1`] = `66 Array [67 Array [68 Array [33 unmodified linesmodified:__tests__/manual.js+13 added
117 unmodified linesExpand all118119120121122123124125126127128129130131132133134135136117 unmodified linesExpand all 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() }) })}modified:src/immer.js+7 added-1 removed
89 unmodified lines90 if (!isDraftable(base)) throw new Error("First argument to createDraft should be a plain object, an array, or an immerable object.") // prettier-ignore91 const scope = ImmerScope.enter()92 const proxy = this.createProxy(base)93 scope.leave()94+ proxy[DRAFT_STATE].customDraft = true95 return proxy96 }97 finishDraft(draft, patchListener) {98 if (!isDraft(draft)) throw new Error("First argument to finishDraft should be an object from createDraft.") // prettier-ignore98- const {scope} = draft[DRAFT_STATE]99+ const state = draft[DRAFT_STATE]100+ if (!state.customDraft) throw new Error("The draft provided was not created using `createDraft`") // prettier-ignore101+ if (state.finalized) throw new Error("The draft provided was has already been finished") // prettier-ignore102+ // TODO: check if created with createDraft103+ // TODO: check if not finsihed twice104+ const {scope} = state105 scope.usePatches(patchListener)106 return this.processResult(undefined, scope)107 }108 setAutoFreeze(value) {173 unmodified lines