Fixed & added tests
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 fd9ed2d · Browse files · 4 files·+45 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+45 added−3 removed
- tests:+43 added−2 removed
- other:+2 added−1 removed
Provenance
- Commit
af95dc8- Tree
28be29f- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
fd9ed2d
4 files changed+45 added−3 removed
modified:__tests__/base.js+20 added
953 unmodified lines954 // so we don't atm955 expect(nextState).not.toBe(baseState)956 })957 958+ it("should not detect noop assignments - 3", () => {959+ const baseState = {x: 3}960+ const nextState = produce(baseState, d => {961+ d.x = 3962+ })963+ expect(nextState).toBe(baseState)964+ })965+966+ it("should not detect noop assignments - 4", () => {967+ const baseState = {x: 3}968+ const nextState = produce(baseState, d => {969+ d.x = 4970+ d.x = 3971+ })972+ // Ideally, this should actually be the same instances973+ // but this would be pretty expensive to detect,974+ // so we don't atm975+ expect(nextState).not.toBe(baseState)976+ })977+978 it("immer should have no dependencies", () => {979 expect(require("../package.json").dependencies).toEqual(undefined)980 })981 25 unmodified linesmodified:__tests__/curry.js+2 added-2 removed
7 unmodified lines89101112121314151617171819202146 unmodified lines7 unmodified lines describe("curry - " + name, () => { setUseProxies(useProxies)
it("should check arguments", () => { expect(() => produce()).toThrow(/produce expects 1 or 2 arguments/) expect(() => produce()).toThrow(/produce expects 1 to 3 arguments/) expect(() => produce(new Buffer(""), () => {})).toThrow( /the first argument to an immer producer should be a primitive, plain object or array/ ) expect(() => produce({}, () => {}, [])).toThrow( /produce expects 1 or 2 arguments/ /third argument of a producer/ ) expect(() => produce({}, {})).toThrow(/should be a function/) expect(() => produce({})).toThrow( /if first argument is not a function, the second argument to produce should be a function/46 unmodified linesmodified:__tests__/patch.js+21 added
226 unmodified lines227 [{op: "replace", path: ["x"], value: {y: 3}}]228 )229 })230 231+describe("same value replacement - 3", () => {232+ runPatchTest(233+ {x: 3},234+ d => {235+ d.x = 3236+ },237+ []238+ )239+})240+241+describe("same value replacement - 4", () => {242+ runPatchTest(243+ {x: 3},244+ d => {245+ d.x = 4246+ d.x = 3247+ },248+ // immer does not detect this is not an actual change249+ [{op: "replace", path: ["x"], value: 3}]250+ )251+})252 describe("simple delete", () => {253 runPatchTest(254 {x: 2},255 d => {10 unmodified linesmodified:src/immer.js+2 added-1 removed
15 unmodified lines1617181920202122232419 unmodified lines44454647484950515214 unmodified lines15 unmodified lines * @returns {any} a new state, or the base state if nothing was modified */export default function produce(baseState, producer, patchListener) { // prettier-ignore if (arguments.length < 1 && arguments.length > 3) throw new Error("produce expects 1 to 3 arguments, got " + arguments.length) if (arguments.length < 1 || arguments.length > 3) throw new Error("produce expects 1 to 3 arguments, got " + arguments.length)
// curried invocation if (typeof baseState === "function") { // prettier-ignore19 unmodified lines
// prettier-ignore { if (typeof producer !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function") if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function") }
// if state is a primitive, don't bother proxying at all if (typeof baseState !== "object" || baseState === null) {14 unmodified lines