Skip to content
Myr

readme.js

This file

Path

__tests__/readme.js

263 lines · 5.6 KiB · JavaScript

Last changed by

Immer 7Michel Weststratebefore adoption467ea5d

History of this file

Take it

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

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

Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 5–8why, one step back
5 produceWithPatches,
6 enableAllPlugins,7 setAutoFreeze8} from "../src/immer"

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 9why, one step back
9

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 10–11why, one step back
10enableAllPlugins()
11

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 12why, one step back
12describe("readme example", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 13–23why, one step back
13 it("works", () => {
14 const baseState = [15 {16 todo: "Learn typescript",17 done: true18 },19 {20 todo: "Try immer",21 done: false22 }23 ]

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 24why, one step back
24

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 25–28why, one step back
25 const nextState = produce(baseState, draft => {
26 draft.push({todo: "Tweet about it"})27 draft[1].done = true28 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 29why, one step back
29

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 30–33why, one step back
30 // the new item is only added to the next state,
31 // base state is unmodified32 expect(baseState.length).toBe(2)33 expect(nextState.length).toBe(3)

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 34why, one step back
34

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 35–37why, one step back
35 // same for the changed 'done' prop
36 expect(baseState[1].done).toBe(false)37 expect(nextState[1].done).toBe(true)

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 38why, one step back
38

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 39–43why, one step back
39 // unchanged data is structurally shared
40 expect(nextState[0]).toBe(baseState[0])41 // changed data not (dûh)42 expect(nextState[1]).not.toBe(baseState[1])43 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 44why, one step back
44

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 45–49why, one step back
45 it("patches", () => {
46 let state = {47 name: "Micheal",48 age: 3249 }

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 50why, one step back
50

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 51–57why, one step back
51 // Let's assume the user is in a wizard, and we don't know whether
52 // his changes should be updated53 let fork = state54 // all the changes the user made in the wizard55 let changes = []56 // all the inverse patches57 let inverseChanges = []

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 58why, one step back
58

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 59–69why, one step back
59 fork = produce(
60 fork,61 draft => {62 draft.age = 3363 },64 // The third argument to produce is a callback to which the patches will be fed65 (patches, inversePatches) => {66 changes.push(...patches)67 inverseChanges.push(...inversePatches)68 }69 )

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 70why, one step back
70

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 71–74why, one step back
71 // In the mean time, our original state is updated as well, as changes come in from the server
72 state = produce(state, draft => {73 draft.name = "Michel"74 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 75why, one step back
75

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 76–77why, one step back
76 // When the wizard finishes (successfully) we can replay the changes made in the fork onto the *new* state!
77 state = applyPatches(state, changes)

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 78why, one step back
78

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 79–83why, one step back
79 // state now contains the changes from both code paths!
80 expect(state).toEqual({81 name: "Michel",82 age: 3383 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 84why, one step back
84

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 85–91why, one step back
85 // Even after finishing the wizard, the user might change his mind...
86 state = applyPatches(state, inverseChanges)87 expect(state).toEqual({88 name: "Michel",89 age: 3290 })91 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 92why, one step back
92

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 93–97why, one step back
93 it("can update set", () => {
94 const state = {95 title: "hello",96 tokenSet: new Set()97 }

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 98why, one step back
98

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 99why, one step back
99 const nextState = produce(state, draft => {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 100–101why, one step back
100 draft.title = draft.title.toUpperCase()
101 draft.tokenSet.add("c1342")

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 102why, one step back
102 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 103why, one step back
103

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 104–109why, one step back
104 expect(state).toEqual({title: "hello", tokenSet: new Set()})
105 expect(nextState).toEqual({106 title: "HELLO",107 tokenSet: new Set(["c1342"])108 })109 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 110why, one step back
110

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 111why, one step back
111 it("can deep update map", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 112why, one step back
112 const state = {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 113why, one step back
113 users: new Map([["michel", {name: "miche", age: 27}]])

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 114why, one step back
114 }

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 115why, one step back
115

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 116why, one step back
116 const nextState = produce(state, draft => {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 117why, one step back
117 draft.users.get("michel").name = "michel"

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 118why, one step back
118 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 119why, one step back
119

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 120–122why, one step back
120 expect(state).toEqual({
121 users: new Map([["michel", {name: "miche", age: 27}]])122 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 123why, one step back
123 expect(nextState).toEqual({

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 124why, one step back
124 users: new Map([["michel", {name: "michel", age: 27}]])

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 125–126why, one step back
125 })
126 })

docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae

line 127why, one step back
127

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 128–133why, one step back
128 it("supports immerable", () => {
129 class Clock {130 constructor(hours = 0, minutes = 0) {131 this.hours = hours132 this.minutes = minutes133 }

docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae

line 134why, one step back
134

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 135–140why, one step back
135 increment(hours, minutes = 0) {
136 return produce(this, d => {137 d.hours += hours138 d.minutes += minutes139 })140 }

docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae

line 141why, one step back
141

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 142–148why, one step back
142 toString() {
143 return `${("" + this.hours).padStart(2, 0)}:${(144 "" + this.minutes145 ).padStart(2, 0)}`146 }147 }148 Clock[immerable] = true

docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae

line 149why, one step back
149

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 150–151why, one step back
150 const midnight = new Clock()
151 const lunch = midnight.increment(12, 30)

docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae

line 152why, one step back
152

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 153–156why, one step back
153 expect(midnight).not.toBe(lunch)
154 expect(lunch).toBeInstanceOf(Clock)155 expect(midnight.toString()).toBe("00:00")156 expect(lunch.toString()).toBe("12:30")

docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae

line 157why, one step back
157

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 158why, one step back
158 const diner = lunch.increment(6)

docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae

line 159why, one step back
159

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 160–162why, one step back
160 expect(diner).not.toBe(lunch)
161 expect(lunch).toBeInstanceOf(Clock)162 expect(diner.toString()).toBe("18:30")

Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda

lines 163–192why, one step back
163 })
164165 test("produceWithPatches", () => {166 const result = produceWithPatches(167 {168 age: 33169 },170 draft => {171 draft.age++172 }173 )174 expect(result).toEqual([175 {176 age: 34177 },178 [179 {180 op: "replace",181 path: ["age"],182 value: 34183 }184 ],185 [186 {187 op: "replace",188 path: ["age"],189 value: 33190 }191 ]192 ])

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 193why, one step back
193 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 194why, one step back
194})

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 195–196why, one step back
195
196test("Producers can update Maps", () => {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 197why, one step back
197 setAutoFreeze(true)

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 198–200why, one step back
198 const usersById_v1 = new Map()
199200 const usersById_v2 = produce(usersById_v1, draft => {

docs: more of thatMichel Weststratebefore adoption41809f8

line 201why, one step back
201 // Modifying a map results in a new map

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 202–205why, one step back
202 draft.set("michel", {name: "Michel Weststrate", country: "NL"})
203 })204205 const usersById_v3 = produce(usersById_v2, draft => {

docs: more of thatMichel Weststratebefore adoption41809f8

line 206why, one step back
206 // Making a change deep inside a map, results in a new map as well!

docs: added Map exampleMichel Weststratebefore adoption85361a7

line 207why, one step back
207 draft.get("michel").country = "UK"

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 208why, one step back
208 debugger

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 209–210why, one step back
209 })
210

docs: more of thatMichel Weststratebefore adoption41809f8

line 211why, one step back
211 // We got a new map each time!

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 212–213why, one step back
212 expect(usersById_v2).not.toBe(usersById_v1)
213 expect(usersById_v3).not.toBe(usersById_v2)

docs: more of thatMichel Weststratebefore adoption41809f8

line 214why, one step back
214 // With different content obviously

docs: added Map exampleMichel Weststratebefore adoption85361a7

lines 215–231why, one step back
215 expect(usersById_v1).toMatchInlineSnapshot(`Map {}`)
216 expect(usersById_v2).toMatchInlineSnapshot(`217 Map {218 "michel" => Object {219 "country": "NL",220 "name": "Michel Weststrate",221 },222 }223 `)224 expect(usersById_v3).toMatchInlineSnapshot(`225 Map {226 "michel" => Object {227 "country": "UK",228 "name": "Michel Weststrate",229 },230 }231 `)

docs: more of thatMichel Weststratebefore adoption41809f8

lines 232–234why, one step back
232 // The old one was never modified
233 expect(usersById_v1.size).toBe(0)234 // And trying to change a Map outside a producers is going to: NO!

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 235why, one step back
235 expect(() => usersById_v3.clear()).toThrowErrorMatchingSnapshot()

docs: added Map exampleMichel Weststratebefore adoption85361a7

line 236why, one step back
236})

Immer 7Michel Weststratebefore adoption467ea5d

lines 237–263why, one step back
237
238test("clock class", () => {239 class Clock {240 [immerable] = true241242 constructor(hour, minute) {243 this.hour = hour244 this.minute = minute245 }246247 get time() {248 return `${this.hour}:${this.minute}`249 }250251 tick() {252 return produce(this, draft => {253 draft.minute++254 })255 }256 }257258 const clock1 = new Clock(12, 10)259 const clock2 = clock1.tick()260 expect(clock1.time).toEqual("12:10") // 12:10261 expect(clock2.time).toEqual("12:11") // 12:11262 expect(clock2).toBeInstanceOf(Clock)263})