curry.js
This file
Path
__tests__/curry.js
104 lines · 2.4 KiB · JavaScript
Last changed by
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
History of this fileTake it
The walk stopped after 200 commits: lines older than fbae585 point at it.
docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
line 1why, one step back1"use strict"Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 2–8why, one step back2import produce, {3 setUseProxies,4 produceWithPatches,5 enableAllPlugins6} from "../src/immer"78enableAllPlugins()docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 9–104why, one step back910runTests("proxy", true)11runTests("es5", false)1213function runTests(name, useProxies) {14 describe("curry - " + name, () => {15 beforeAll(() => {16 setUseProxies(useProxies)17 })1819 it("should check arguments", () => {20 expect(() => produce()).toThrowErrorMatchingSnapshot()21 expect(() => produce({})).toThrowErrorMatchingSnapshot()22 expect(() => produce({}, {})).toThrowErrorMatchingSnapshot()23 expect(() => produce({}, () => {}, [])).toThrowErrorMatchingSnapshot()24 })2526 it("should support currying", () => {27 const state = [{}, {}, {}]28 const mapper = produce((item, index) => {29 item.index = index30 })3132 expect(state.map(mapper)).not.toBe(state)33 expect(state.map(mapper)).toEqual([{index: 0}, {index: 1}, {index: 2}])34 expect(state).toEqual([{}, {}, {}])35 })3637 it("should support returning new states from curring", () => {38 const reducer = produce((item, index) => {39 if (!item) {40 return {hello: "world"}41 }42 item.index = index43 })4445 expect(reducer(undefined, 3)).toEqual({hello: "world"})46 expect(reducer({}, 3)).toEqual({index: 3})47 })4849 it("should support passing an initial state as second argument", () => {50 const reducer = produce(51 (item, index) => {52 item.index = index53 },54 {hello: "world"}55 )5657 expect(reducer(undefined, 3)).toEqual({hello: "world", index: 3})58 expect(reducer({}, 3)).toEqual({index: 3})59 expect(reducer()).toEqual({hello: "world", index: undefined})60 })6162 it("can has fun with change detection", () => {63 const spread = produce(Object.assign)6465 const base = {66 x: 1,67 y: 168 }6970 expect({...base}).not.toBe(base)71 expect(spread(base, {})).toBe(base)72 expect(spread(base, {y: 1})).toBe(base)73 expect(spread(base, {...base})).toBe(base)74 expect(spread(base, {...base, y: 2})).not.toBe(base)75 expect(spread(base, {...base, y: 2})).toEqual({x: 1, y: 2})76 expect(spread(base, {z: 3})).toEqual({x: 1, y: 1, z: 3})77 expect(spread(base, {y: 1})).toBe(base)78 })79 })8081 it("support currying for produceWithPatches", () => {82 const increment = produceWithPatches((draft, delta) => {83 draft.x += delta84 })8586 expect(increment({x: 5}, 2)).toEqual([87 {x: 7},88 [89 {90 op: "replace",91 path: ["x"],92 value: 793 }94 ],95 [96 {97 op: "replace",98 path: ["x"],99 value: 5100 }101 ]102 ])103 })104}