Skip to content
Myr

curry.js

This file

Path

__tests__/curry.js

100 lines · 2.3 KiB · JavaScript

Last changed by

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

History of this file

Take it

The walk stopped after 200 commits: lines older than 6555173 point at it.

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 1why, one step back
1"use strict"

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 2–3why, one step back
2import {
3 produce,

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 4–5why, one step back
4 setUseProxies,
5 produceWithPatches,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 6why, one step back
6 enablePatches

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 7–8why, one step back
7} from "../src/immer"
8

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 9why, one step back
9enablePatches()

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 10–12why, one step back
10
11runTests("proxy", true)12

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 13why, one step back
13function runTests(name) {

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 14–100why, one step back
14 describe("curry - " + name, () => {
15 it("should check arguments", () => {16 expect(() => produce()).toThrowErrorMatchingSnapshot()17 expect(() => produce({})).toThrowErrorMatchingSnapshot()18 expect(() => produce({}, {})).toThrowErrorMatchingSnapshot()19 expect(() => produce({}, () => {}, [])).toThrowErrorMatchingSnapshot()20 })2122 it("should support currying", () => {23 const state = [{}, {}, {}]24 const mapper = produce((item, index) => {25 item.index = index26 })2728 expect(state.map(mapper)).not.toBe(state)29 expect(state.map(mapper)).toEqual([{index: 0}, {index: 1}, {index: 2}])30 expect(state).toEqual([{}, {}, {}])31 })3233 it("should support returning new states from curring", () => {34 const reducer = produce((item, index) => {35 if (!item) {36 return {hello: "world"}37 }38 item.index = index39 })4041 expect(reducer(undefined, 3)).toEqual({hello: "world"})42 expect(reducer({}, 3)).toEqual({index: 3})43 })4445 it("should support passing an initial state as second argument", () => {46 const reducer = produce(47 (item, index) => {48 item.index = index49 },50 {hello: "world"}51 )5253 expect(reducer(undefined, 3)).toEqual({hello: "world", index: 3})54 expect(reducer({}, 3)).toEqual({index: 3})55 expect(reducer()).toEqual({hello: "world", index: undefined})56 })5758 it("can has fun with change detection", () => {59 const spread = produce(Object.assign)6061 const base = {62 x: 1,63 y: 164 }6566 expect({...base}).not.toBe(base)67 expect(spread(base, {})).toBe(base)68 expect(spread(base, {y: 1})).toBe(base)69 expect(spread(base, {...base})).toBe(base)70 expect(spread(base, {...base, y: 2})).not.toBe(base)71 expect(spread(base, {...base, y: 2})).toEqual({x: 1, y: 2})72 expect(spread(base, {z: 3})).toEqual({x: 1, y: 1, z: 3})73 expect(spread(base, {y: 1})).toBe(base)74 })75 })7677 it("support currying for produceWithPatches", () => {78 const increment = produceWithPatches((draft, delta) => {79 draft.x += delta80 })8182 expect(increment({x: 5}, 2)).toEqual([83 {x: 7},84 [85 {86 op: "replace",87 path: ["x"],88 value: 789 }90 ],91 [92 {93 op: "replace",94 path: ["x"],95 value: 596 }97 ]98 ])99 })100}