readme.js
This file
Path
__tests__/readme.js
171 lines · 5.1 KiB · JavaScript
Last changed by
feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298
History of this fileTake it
The walk stopped after 200 commits: lines older than 620f9bb point at it.
Merge pull request #214 from rgbkrk/patch-1Michel Weststratebefore adoption620f9bb
line 1why, one step back1"use strict"docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae
line 2why, one step back2import produce, {applyPatches, immerable} from "../src/index"Merge pull request #214 from rgbkrk/patch-1Michel Weststratebefore adoption620f9bb
lines 3–132why, one step back34describe("readme example", () => {5 it("works", () => {6 const baseState = [7 {8 todo: "Learn typescript",9 done: true10 },11 {12 todo: "Try immer",13 done: false14 }15 ]1617 const nextState = produce(baseState, draft => {18 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 })3637 it("patches", () => {38 let state = {39 name: "Micheal",40 age: 3241 }4243 // Let's assume the user is in a wizard, and we don't know whether44 // his changes should be updated45 let fork = state46 // all the changes the user made in the wizard47 let changes = []48 // all the inverse patches49 let inverseChanges = []5051 fork = produce(52 fork,53 draft => {54 draft.age = 3355 },56 // The third argument to produce is a callback to which the patches will be fed57 (patches, inversePatches) => {58 changes.push(...patches)59 inverseChanges.push(...inversePatches)60 }61 )6263 // In the mean time, our original state is updated as well, as changes come in from the server64 state = produce(state, draft => {65 draft.name = "Michel"66 })6768 // When the wizard finishes (successfully) we can replay the changes made in the fork onto the *new* state!69 state = applyPatches(state, changes)7071 // state now contains the changes from both code paths!72 expect(state).toEqual({73 name: "Michel",74 age: 3375 })7677 // Even after finishing the wizard, the user might change his mind...78 state = applyPatches(state, inverseChanges)79 expect(state).toEqual({80 name: "Michel",81 age: 3282 })83 })8485 it("can update set", () => {86 const state = {87 title: "hello",88 tokenSet: new Set()89 }9091 const nextState = produce(state, draft => {92 draft.title = draft.title.toUpperCase() // let immer do it's job93 // don't use the operations onSet, as that mutates the instance!94 // draft.tokenSet.add("c1342")9596 // instead: clone the set (once!)97 const newSet = new Set(draft.tokenSet)98 // mutate it once99 newSet.add("c1342")100 // update the draft with the new set101 draft.tokenSet = newSet102 })103104 expect(state).toEqual({title: "hello", tokenSet: new Set()})105 expect(nextState).toEqual({106 title: "HELLO",107 tokenSet: new Set(["c1342"])108 })109 })110111 it("can deep udpate map", () => {112 const state = {113 users: new Map([["michel", {name: "miche"}]])114 }115116 const nextState = produce(state, draft => {117 const newUsers = new Map(draft.users)118 // mutate the new map and set a _new_ user object119 // but leverage produce again to deeply update it's contents120 newUsers.set(121 "michel",122 produce(draft.users.get("michel"), draft => {123 draft.name = "michel"124 })125 )126 draft.users = newUsers127 })128129 expect(state).toEqual({users: new Map([["michel", {name: "miche"}]])})130 expect(nextState).toEqual({131 users: new Map([["michel", {name: "michel"}]])132 })docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae
lines 133–169why, one step back133 })134135 it("supports immerable", () => {136 class Clock {137 constructor(hours = 0, minutes = 0) {138 this.hours = hours139 this.minutes = minutes140 }141142 increment(hours, minutes = 0) {143 return produce(this, d => {144 d.hours += hours145 d.minutes += minutes146 })147 }148149 toString() {150 return `${("" + this.hours).padStart(2, 0)}:${(151 "" + this.minutes152 ).padStart(2, 0)}`153 }154 }155 Clock[immerable] = true156157 const midnight = new Clock()158 const lunch = midnight.increment(12, 30)159160 expect(midnight).not.toBe(lunch)161 expect(lunch).toBeInstanceOf(Clock)162 expect(midnight.toString()).toBe("00:00")163 expect(lunch.toString()).toBe("12:30")164165 const diner = lunch.increment(6)166167 expect(diner).not.toBe(lunch)168 expect(lunch).toBeInstanceOf(Clock)169 expect(diner.toString()).toBe("18:30")Merge pull request #214 from rgbkrk/patch-1Michel Weststratebefore adoption620f9bb
lines 170–171why, one step back170 })171})