Skip to content
Myr

readme.js

This file

Path

__tests__/readme.js

149 lines · 4.4 KiB · JavaScript

Last changed by

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

History of this file

Take it

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

Readme improvementsMichel Weststratebefore adoptiona1f5c6a

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 2why, one step back
2import produce, {applyPatches} from "../src/immer"

Readme improvementsMichel Weststratebefore adoptiona1f5c6a

lines 3–16why, one step back
3
4describe("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 ]16

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 17why, one step back
17 const nextState = produce(baseState, draft => {

Readme improvementsMichel Weststratebefore adoptiona1f5c6a

lines 18–35why, one step back
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 })

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 36–49why, one step back
36
37 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)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 50–97why, one step back
50 })
5152 it("patches", () => {53 let state = {54 name: "Micheal",55 age: 3256 }5758 // Let's assume the user is in a wizard, and we don't know whether59 // his changes should be updated60 let fork = state61 // all the changes the user made in the wizard62 let changes = []63 // all the inverse patches64 let inverseChanges = []6566 fork = produce(67 fork,68 draft => {69 draft.age = 3370 },71 // The third argument to produce is a callback to which the patches will be fed72 (patches, inversePatches) => {73 changes.push(...patches)74 inverseChanges.push(...inversePatches)75 }76 )7778 // In the mean time, our original state is updated as well, as changes come in from the server79 state = produce(state, draft => {80 draft.name = "Michel"81 })8283 // When the wizard finishes (successfully) we can replay the changes made in the fork onto the *new* state!84 state = applyPatches(state, changes)8586 // state now contains the changes from both code paths!87 expect(state).toEqual({88 name: "Michel",89 age: 3390 })9192 // Even after finishing the wizard, the user might change his mind...93 state = applyPatches(state, inverseChanges)94 expect(state).toEqual({95 name: "Michel",96 age: 3297 })

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

line 98why, one step back
98 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 99–148why, one step back
99
100 it("can update set", () => {101 const state = {102 title: "hello",103 tokenSet: new Set()104 }105106 const nextState = produce(state, draft => {107 draft.title = draft.title.toUpperCase() // let immer do it's job108 // don't use the operations onSet, as that mutates the instance!109 // draft.tokenSet.add("c1342")110111 // instead: clone the set (once!)112 const newSet = new Set(draft.tokenSet)113 // mutate it once114 newSet.add("c1342")115 // update the draft with the new set116 draft.tokenSet = newSet117 })118119 expect(state).toEqual({title: "hello", tokenSet: new Set()})120 expect(nextState).toEqual({121 title: "HELLO",122 tokenSet: new Set(["c1342"])123 })124 })125126 it("can deep udpate map", () => {127 const state = {128 users: new Map([["michel", {name: "miche"}]])129 }130131 const nextState = produce(state, draft => {132 const newUsers = new Map(draft.users)133 // mutate the new map and set a _new_ user object134 // but leverage produce again to deeply update it's contents135 newUsers.set(136 "michel",137 produce(draft.users.get("michel"), draft => {138 draft.name = "michel"139 })140 )141 draft.users = newUsers142 })143144 expect(state).toEqual({users: new Map([["michel", {name: "miche"}]])})145 expect(nextState).toEqual({146 users: new Map([["michel", {name: "michel"}]])147 })148 })

Readme improvementsMichel Weststratebefore adoptiona1f5c6a

line 149why, one step back
149})