readme.js
This file
Path
__tests__/readme.js
263 lines · 5.6 KiB · JavaScript
Last changed by
Immer 7Michel Weststratebefore adoption467ea5d
History of this fileTake it
The walk stopped after 200 commits: lines older than efd47f6 point at it.
chore: improve some testsaleclarsonbefore adoptionefd47f6
line 1why, one step back1"use strict"Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda
lines 2–4why, one step back2import produce, {3 applyPatches,4 immerable,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 5–8why, one step back5 produceWithPatches,6 enableAllPlugins,7 setAutoFreeze8} from "../src/immer"chore: improve some testsaleclarsonbefore adoptionefd47f6
line 9why, one step back9Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 10–11why, one step back10enableAllPlugins()11chore: improve some testsaleclarsonbefore adoptionefd47f6
line 12why, one step back12describe("readme example", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 13–23why, one step back13 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 back24style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 25–28why, one step back25 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 back29style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 30–33why, one step back30 // 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 back34style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 35–37why, one step back35 // same for the changed 'done' prop36 expect(baseState[1].done).toBe(false)37 expect(nextState[1].done).toBe(true)chore: improve some testsaleclarsonbefore adoptionefd47f6
line 38why, one step back38style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 39–43why, one step back39 // unchanged data is structurally shared40 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 back44style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 45–49why, one step back45 it("patches", () => {46 let state = {47 name: "Micheal",48 age: 3249 }chore: improve some testsaleclarsonbefore adoptionefd47f6
line 50why, one step back50style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 51–57why, one step back51 // Let's assume the user is in a wizard, and we don't know whether52 // 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 back58style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 59–69why, one step back59 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 back70style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 71–74why, one step back71 // In the mean time, our original state is updated as well, as changes come in from the server72 state = produce(state, draft => {73 draft.name = "Michel"74 })chore: improve some testsaleclarsonbefore adoptionefd47f6
line 75why, one step back75style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 76–77why, one step back76 // 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 back78style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 79–83why, one step back79 // 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 back84style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 85–91why, one step back85 // 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 back92style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 93–97why, one step back93 it("can update set", () => {94 const state = {95 title: "hello",96 tokenSet: new Set()97 }chore: improve some testsaleclarsonbefore adoptionefd47f6
line 98why, one step back98style: use tabsuse-tabsbefore adoptiona9c0fd0
line 99why, one step back99 const nextState = produce(state, draft => {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 100–101why, one step back100 draft.title = draft.title.toUpperCase()101 draft.tokenSet.add("c1342")style: use tabsuse-tabsbefore adoptiona9c0fd0
line 102why, one step back102 })chore: improve some testsaleclarsonbefore adoptionefd47f6
line 103why, one step back103style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 104–109why, one step back104 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 back110feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 111why, one step back111 it("can deep update map", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 112why, one step back112 const state = {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 113why, one step back113 users: new Map([["michel", {name: "miche", age: 27}]])style: use tabsuse-tabsbefore adoptiona9c0fd0
line 114why, one step back114 }chore: improve some testsaleclarsonbefore adoptionefd47f6
line 115why, one step back115style: use tabsuse-tabsbefore adoptiona9c0fd0
line 116why, one step back116 const nextState = produce(state, draft => {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 117why, one step back117 draft.users.get("michel").name = "michel"style: use tabsuse-tabsbefore adoptiona9c0fd0
line 118why, one step back118 })chore: improve some testsaleclarsonbefore adoptionefd47f6
line 119why, one step back119feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 120–122why, one step back120 expect(state).toEqual({121 users: new Map([["michel", {name: "miche", age: 27}]])122 })style: use tabsuse-tabsbefore adoptiona9c0fd0
line 123why, one step back123 expect(nextState).toEqual({feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 124why, one step back124 users: new Map([["michel", {name: "michel", age: 27}]])style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 125–126why, one step back125 })126 })docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae
line 127why, one step back127style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 128–133why, one step back128 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 back134style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 135–140why, one step back135 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 back141style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 142–148why, one step back142 toString() {143 return `${("" + this.hours).padStart(2, 0)}:${(144 "" + this.minutes145 ).padStart(2, 0)}`146 }147 }148 Clock[immerable] = truedocs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae
line 149why, one step back149style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 150–151why, one step back150 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 back152style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 153–156why, one step back153 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 back157style: use tabsuse-tabsbefore adoptiona9c0fd0
line 158why, one step back158 const diner = lunch.increment(6)docs: removed deprecation note on this, see #308Michel Weststratebefore adoptiona5ce2ae
line 159why, one step back159style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 160–162why, one step back160 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 back163 })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 back193 })chore: improve some testsaleclarsonbefore adoptionefd47f6
line 194why, one step back194})docs: added Map exampleMichel Weststratebefore adoption85361a7
lines 195–196why, one step back195196test("Producers can update Maps", () => {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 197why, one step back197 setAutoFreeze(true)docs: added Map exampleMichel Weststratebefore adoption85361a7
lines 198–200why, one step back198 const usersById_v1 = new Map()199200 const usersById_v2 = produce(usersById_v1, draft => {docs: more of thatMichel Weststratebefore adoption41809f8
line 201why, one step back201 // Modifying a map results in a new mapdocs: added Map exampleMichel Weststratebefore adoption85361a7
lines 202–205why, one step back202 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 back206 // Making a change deep inside a map, results in a new map as well!docs: added Map exampleMichel Weststratebefore adoption85361a7
line 207why, one step back207 draft.get("michel").country = "UK"Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 208why, one step back208 debuggerdocs: added Map exampleMichel Weststratebefore adoption85361a7
lines 209–210why, one step back209 })210docs: more of thatMichel Weststratebefore adoption41809f8
line 211why, one step back211 // We got a new map each time!docs: added Map exampleMichel Weststratebefore adoption85361a7
lines 212–213why, one step back212 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 back214 // With different content obviouslydocs: added Map exampleMichel Weststratebefore adoption85361a7
lines 215–231why, one step back215 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 back232 // The old one was never modified233 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 back235 expect(() => usersById_v3.clear()).toThrowErrorMatchingSnapshot()docs: added Map exampleMichel Weststratebefore adoption85361a7
line 236why, one step back236})Immer 7Michel Weststratebefore adoption467ea5d
lines 237–263why, one step back237238test("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})