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 fbae585 point at it.

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 1–4why, one step back
1"use strict"
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"

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

line 9why, one step back
9

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

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

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 12–196why, one step back
12describe("readme example", () => {
13 it("works", () => {14 const baseState = [15 {16 todo: "Learn typescript",17 done: true18 },19 {20 todo: "Try immer",21 done: false22 }23 ]2425 const nextState = produce(baseState, draft => {26 draft.push({todo: "Tweet about it"})27 draft[1].done = true28 })2930 // 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)3435 // same for the changed 'done' prop36 expect(baseState[1].done).toBe(false)37 expect(nextState[1].done).toBe(true)3839 // 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 })4445 it("patches", () => {46 let state = {47 name: "Micheal",48 age: 3249 }5051 // 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 = []5859 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 )7071 // 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 })7576 // When the wizard finishes (successfully) we can replay the changes made in the fork onto the *new* state!77 state = applyPatches(state, changes)7879 // state now contains the changes from both code paths!80 expect(state).toEqual({81 name: "Michel",82 age: 3383 })8485 // 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 })9293 it("can update set", () => {94 const state = {95 title: "hello",96 tokenSet: new Set()97 }9899 const nextState = produce(state, draft => {100 draft.title = draft.title.toUpperCase()101 draft.tokenSet.add("c1342")102 })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 update map", () => {112 const state = {113 users: new Map([["michel", {name: "miche", age: 27}]])114 }115116 const nextState = produce(state, draft => {117 draft.users.get("michel").name = "michel"118 })119120 expect(state).toEqual({121 users: new Map([["michel", {name: "miche", age: 27}]])122 })123 expect(nextState).toEqual({124 users: new Map([["michel", {name: "michel", age: 27}]])125 })126 })127128 it("supports immerable", () => {129 class Clock {130 constructor(hours = 0, minutes = 0) {131 this.hours = hours132 this.minutes = minutes133 }134135 increment(hours, minutes = 0) {136 return produce(this, d => {137 d.hours += hours138 d.minutes += minutes139 })140 }141142 toString() {143 return `${("" + this.hours).padStart(2, 0)}:${(144 "" + this.minutes145 ).padStart(2, 0)}`146 }147 }148 Clock[immerable] = true149150 const midnight = new Clock()151 const lunch = midnight.increment(12, 30)152153 expect(midnight).not.toBe(lunch)154 expect(lunch).toBeInstanceOf(Clock)155 expect(midnight.toString()).toBe("00:00")156 expect(lunch.toString()).toBe("12:30")157158 const diner = lunch.increment(6)159160 expect(diner).not.toBe(lunch)161 expect(lunch).toBeInstanceOf(Clock)162 expect(diner.toString()).toBe("18:30")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 ])193 })194})195196test("Producers can update Maps", () => {

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

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

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 198–207why, one step back
198 const usersById_v1 = new Map()
199200 const usersById_v2 = produce(usersById_v1, draft => {201 // Modifying a map results in a new map202 draft.set("michel", {name: "Michel Weststrate", country: "NL"})203 })204205 const usersById_v3 = produce(usersById_v2, draft => {206 // Making a change deep inside a map, results in a new map as well!207 draft.get("michel").country = "UK"

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

line 208why, one step back
208 debugger

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

lines 209–234why, one step back
209 })
210211 // We got a new map each time!212 expect(usersById_v2).not.toBe(usersById_v1)213 expect(usersById_v3).not.toBe(usersById_v2)214 // With different content obviously215 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 `)232 // 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 back
235 expect(() => usersById_v3.clear()).toThrowErrorMatchingSnapshot()

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

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})