readme.js
This file
Path
__tests__/readme.js
51 lines · 1.3 KiB · JavaScript
Last changed by
Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
History of this fileTake it
Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 1why, one step back1"use strict"Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 2why, one step back2import produce from "../src/immer"Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 3–8why, one step back34describe("readme example", () => {5 it("works", () => {6 const baseState = [7 {8 todo: "Learn typescript",No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 9why, one step back9 done: trueMerge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 10–12why, one step back10 },11 {12 todo: "Try immer",No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
lines 13–14why, one step back13 done: false14 }Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 15–16why, one step back15 ]16`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 17why, one step back17 const nextState = produce(baseState, draft => {Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
lines 18–35why, one step back18 draft.push({todo: "Tweet about it"})19 draft[1].done = true20 })2122 // the new item is only added to the next state,23 // base state is unmodified24 expect(baseState.length).toBe(2)25 expect(nextState.length).toBe(3)2627 // same for the changed 'done' prop28 expect(baseState[1].done).toBe(false)29 expect(nextState[1].done).toBe(true)3031 // unchanged data is structurally shared32 expect(nextState[0]).toBe(baseState[0])33 // changed data not (dûh)34 expect(nextState[1]).not.toBe(baseState[1])35 })Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
lines 36–50why, one step back3637 it("it can use this", () => {38 const base = {counter: 0}3940 const next = produce(base, function() {41 this.counter++42 })43 expect(next.counter).toBe(1)4445 // OR46 const increment = produce(function() {47 this.counter++48 })49 expect(increment(base).counter).toBe(1)50 })Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 51why, one step back51})