Skip to content
Myr

base.js

This file

Path

__tests__/base.js

1,308 lines · 47.0 KiB · JavaScript

Last changed by

chore: improve some testsaleclarsonbefore adoptionefd47f6

History of this file

Take it

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

v1.2.0Michel Weststratebefore adoptione1e73c4

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 2why, one step back
2import {Immer, nothing, original, isDraft} from "../src/index"

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 3why, one step back
3import {shallowCopy} from "../src/common"

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 4–11why, one step back
4import deepFreeze from "deep-freeze"
5import cloneDeep from "lodash.clonedeep"6import * as lodash from "lodash"78jest.setTimeout(1000)910runBaseTest("proxy (no freeze)", true, false)11runBaseTest("proxy (autofreeze)", true, true)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 12why, one step back
12runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 13–14why, one step back
13runBaseTest("es5 (no freeze)", false, false)
14runBaseTest("es5 (autofreeze)", false, true)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 15why, one step back
15runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)

v1.2.0Michel Weststratebefore adoptione1e73c4

line 16why, one step back
16

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 17why, one step back
17function runBaseTest(name, useProxies, autoFreeze, useListener) {

test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64

lines 18–19why, one step back
18 const listener = useListener ? function() {} : undefined
19 const {produce} = createPatchedImmer({

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 20–23why, one step back
20 useProxies,
21 autoFreeze22 })23

test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64

lines 24–27why, one step back
24 // When `useListener` is true, append a function to the arguments of every
25 // uncurried `produce` call in every test. This makes tests easier to read.26 function createPatchedImmer(options) {27 const immer = new Immer(options)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 28why, one step back
28

test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64

lines 29–37why, one step back
29 const {produce} = immer
30 immer.produce = (...args) =>31 typeof args[1] === "function" && args.length < 332 ? produce(...args, listener)33 : produce(...args)3435 return immer36 }37

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 38–45why, one step back
38 describe(`base functionality - ${name}`, () => {
39 let baseState40 let origBaseState4142 beforeEach(() => {43 origBaseState = baseState = createBaseState()44 })45

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 46why, one step back
46 it("returns the original state when no changes are made", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 47–50why, one step back
47 const nextState = produce(baseState, s => {
48 expect(s.aProp).toBe("hi")49 expect(s.anObject.nested).toMatchObject({yummie: true})50 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 51–53why, one step back
51 expect(nextState).toBe(baseState)
52 })53

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 54–55why, one step back
54 it("does structural sharing", () => {
55 const random = Math.random()

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 56why, one step back
56 const nextState = produce(baseState, s => {

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 57why, one step back
57 s.aProp = random

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 58why, one step back
58 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 59why, one step back
59 expect(nextState).not.toBe(baseState)

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 60why, one step back
60 expect(nextState.aProp).toBe(random)

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 61–63why, one step back
61 expect(nextState.nested).toBe(baseState.nested)
62 })63

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 64–65why, one step back
64 it("preserves the prototype of drafts", () => {
65 const getProto = Object.getPrototypeOf

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 66why, one step back
66 const nextState = produce(baseState, s => {

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 67–70why, one step back
67 expect(getProto(s)).toBe(Object.prototype)
68 expect(getProto(s.anArray)).toBe(Array.prototype)69 s.aProp = Math.random()70 s.anArray.push(1)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 71why, one step back
71 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 72–73why, one step back
72 expect(getProto(nextState)).toBe(Object.prototype)
73 expect(getProto(nextState.anArray)).toBe(Array.prototype)

Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62

lines 74–76why, one step back
74 })
7576 it("deep change bubbles up", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 77–79why, one step back
77 const nextState = produce(baseState, s => {
78 s.anObject.nested.yummie = false79 })

Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62

lines 80–85why, one step back
80 expect(nextState).not.toBe(baseState)
81 expect(nextState.anObject).not.toBe(baseState.anObject)82 expect(baseState.anObject.nested.yummie).toBe(true)83 expect(nextState.anObject.nested.yummie).toBe(false)84 expect(nextState.anArray).toBe(baseState.anArray)85 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 86–87why, one step back
86
87 it("can add props", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 88–90why, one step back
88 const nextState = produce(baseState, s => {
89 s.anObject.cookie = {tasty: true}90 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 91–97why, one step back
91 expect(nextState).not.toBe(baseState)
92 expect(nextState.anObject).not.toBe(baseState.anObject)93 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)94 expect(nextState.anObject.cookie).toEqual({tasty: true})95 })9697 it("can delete props", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 98–100why, one step back
98 const nextState = produce(baseState, s => {
99 delete s.anObject.nested100 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 101–105why, one step back
101 expect(nextState).not.toBe(baseState)
102 expect(nextState.anObject).not.toBe(baseState.anObject)103 expect(nextState.anObject.nested).toBe(undefined)104 })105

test: deleting props added in the produceraleclarsonbefore adoption691e73b

lines 106–107why, one step back
106 // Found by: https://github.com/mweststrate/immer/pull/267
107 it("can delete props added in the producer", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 108–111why, one step back
108 const nextState = produce(baseState, s => {
109 s.anObject.test = true110 delete s.anObject.test111 })

test: deleting props added in the produceraleclarsonbefore adoption691e73b

lines 112–120why, one step back
112 if (useProxies) {
113 expect(nextState).not.toBe(baseState)114 expect(nextState).toEqual(baseState)115 } else {116 // The copy is avoided in ES5.117 expect(nextState).toBe(baseState)118 }119 })120

v1.2.0Michel Weststratebefore adoptione1e73c4

line 121why, one step back
121 it("ignores single non-modification", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 122–124why, one step back
122 const nextState = produce(baseState, s => {
123 s.aProp = "hi"124 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 125–127why, one step back
125 expect(nextState).toBe(baseState)
126 })127

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 128–129why, one step back
128 // Once a draft is marked as modified, it stays that way.
129 it("never removes modified properties from its internal state", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 130why, one step back
130 const nextState = produce(baseState, s => {

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 131–132why, one step back
131 const original = s.aProp
132 // This assignment marks the draft as modified.

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 133why, one step back
133 s.aProp = "hello"

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 134–136why, one step back
134 // This assignment reverts the property to its original value,
135 // but Immer cannot detect this, so the base state is copied anyway.136 s.aProp = original

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 137why, one step back
137 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 138–141why, one step back
138 expect(nextState).not.toBe(baseState)
139 expect(nextState).toEqual(baseState)140 })141

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 142–148why, one step back
142 describe("array drafts", () => {
143 it("supports Array.isArray()", () => {144 const nextState = produce(baseState, s => {145 expect(Array.isArray(s.anArray)).toBeTruthy()146 s.anArray.push(1)147 })148 expect(Array.isArray(nextState.anArray)).toBeTruthy()

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 149–150why, one step back
149 })
150

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 151–156why, one step back
151 it("supports index access", () => {
152 const value = baseState.anArray[0]153 const nextState = produce(baseState, s => {154 expect(s.anArray[0]).toBe(value)155 })156 expect(nextState).toBe(baseState)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 157why, one step back
157 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 158–174why, one step back
158
159 it("supports iteration", () => {160 const base = [{id: 1, a: 1}, {id: 2, a: 1}]161 const findById = (collection, id) => {162 for (const item of collection) {163 if (item.id === id) return item164 }165 return null166 }167 const result = produce(base, draft => {168 const obj1 = findById(draft, 1)169 const obj2 = findById(draft, 2)170 obj1.a = 2171 obj2.a = 2172 })173 expect(result[0].a).toEqual(2)174 expect(result[1].a).toEqual(2)

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 175–176why, one step back
175 })
176

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 177–183why, one step back
177 it("can assign an index via bracket notation", () => {
178 const nextState = produce(baseState, s => {179 s.anArray[3] = true180 })181 expect(nextState).not.toBe(baseState)182 expect(nextState.anArray).not.toBe(baseState.anArray)183 expect(nextState.anArray[3]).toEqual(true)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 184why, one step back
184 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 185why, one step back
185

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 186–192why, one step back
186 it("can use splice() to both add and remove items", () => {
187 const nextState = produce(baseState, s => {188 s.anArray.splice(1, 1, "a", "b")189 })190 expect(nextState.anArray).not.toBe(baseState.anArray)191 expect(nextState.anArray[1]).toBe("a")192 expect(nextState.anArray[2]).toBe("b")

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 193why, one step back
193 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 194why, one step back
194

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 195–201why, one step back
195 it("can truncate via the length property", () => {
196 const baseLength = baseState.anArray.length197 const nextState = produce(baseState, s => {198 s.anArray.length = baseLength - 1199 })200 expect(nextState.anArray).not.toBe(baseState.anArray)201 expect(nextState.anArray.length).toBe(baseLength - 1)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 202why, one step back
202 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 203why, one step back
203

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 204–211why, one step back
204 it("can extend via the length property", () => {
205 const baseLength = baseState.anArray.length206 const nextState = produce(baseState, s => {207 s.anArray.length = baseLength + 1208 })209 expect(nextState.anArray).not.toBe(baseState.anArray)210 expect(nextState.anArray.length).toBe(baseLength + 1)211 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 212why, one step back
212

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 213–219why, one step back
213 // Reported here: https://github.com/mweststrate/immer/issues/116
214 it("can pop then push", () => {215 const nextState = produce([1, 2, 3], s => {216 s.pop()217 s.push(100)218 })219 expect(nextState).toEqual([1, 2, 100])

test: rename and move a testaleclarsonbefore adoption8e0325c

lines 220–221why, one step back
220 })
221

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 222–228why, one step back
222 it("can be sorted", () => {
223 const baseState = [3, 1, 2]224 const nextState = produce(baseState, s => {225 s.sort()226 })227 expect(nextState).not.toBe(baseState)228 expect(nextState).toEqual([1, 2, 3])

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 229why, one step back
229 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 230why, one step back
230

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 231–239why, one step back
231 it("supports modifying nested objects", () => {
232 const baseState = [{a: 1}, {}]233 const nextState = produce(baseState, s => {234 s[0].a++235 s[1].a = 0236 })237 expect(nextState).not.toBe(baseState)238 expect(nextState[0].a).toBe(2)239 expect(nextState[1].a).toBe(0)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 240why, one step back
240 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 241–242why, one step back
241 })
242

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 243why, one step back
243 it("supports access of property descriptors", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 244–255why, one step back
244 const nextState = produce([], s => {
245 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)246 s.unshift("x")247 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({248 configurable: true,249 enumerable: true,250 value: "x",251 writable: true252 })253 expect(s.length).toBe(1)254 expect(s[0] === "x").toBe(true)255 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 256–257why, one step back
256 expect(nextState).toEqual(["x"])
257 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 258why, one step back
258 configurable: !autoFreeze,

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 259–260why, one step back
259 enumerable: true,
260 value: "x",

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 261why, one step back
261 writable: !autoFreeze

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 262–264why, one step back
262 })
263 })264

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 265why, one step back
265 it("can rename nested objects (no changes)", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 266–270why, one step back
266 const nextState = produce(baseState, s => {
267 const obj = s.anObject268 delete s.anObject269 s.renamed = obj270 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 271–272why, one step back
271 expect(nextState).not.toBe(baseState)
272 expect(nextState.anArray).toBe(baseState.anArray)

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 273why, one step back
273 expect(nextState.renamed.nested).toBe(baseState.anObject.nested)

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 274–276why, one step back
274 expect(enumerableOnly(nextState)).toEqual({
275 anArray: [3, 2, {c: 3}, 1],276 aProp: "hi",

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 277why, one step back
277 renamed: {

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 278–285why, one step back
278 nested: {
279 yummie: true280 },281 coffee: false282 }283 })284 })285

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

lines 286–288why, one step back
286 // Very similar to the test before, but the reused object has one
287 // property changed, one added, and one removed.288 it("can rename nested objects (with changes)", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 289–291why, one step back
289 const nextState = produce(baseState, s => {
290 const obj = s.anObject291 delete s.anObject

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 292why, one step back
292

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 293–295why, one step back
293 obj.coffee = true // change
294 obj.nested.yummy = true // add295 delete obj.nested.yummie // delete

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 296why, one step back
296

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 297–298why, one step back
297 s.renamed = obj
298 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 299–300why, one step back
299 expect(nextState).not.toBe(baseState)
300 expect(nextState.anArray).toBe(baseState.anArray)

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 301why, one step back
301 expect(nextState.renamed.nested).not.toBe(baseState.anObject.nested)

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 302–303why, one step back
302 expect(enumerableOnly(nextState)).toEqual({
303 anArray: [3, 2, {c: 3}, 1],

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

lines 304–305why, one step back
304 aProp: "hi",
305 renamed: {

v1.2.0Michel Weststratebefore adoptione1e73c4

line 306why, one step back
306 nested: {

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 307why, one step back
307 yummy: true

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 308–313why, one step back
308 },
309 coffee: true310 }311 })312 })313

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 314why, one step back
314 it("can nest a draft in a new object (no changes)", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 315–318why, one step back
315 const nextState = produce(baseState, s => {
316 s.foo = {bar: s.anObject}317 delete s.anObject318 })

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 319–323why, one step back
319 expect(nextState).not.toBe(baseState)
320 expect(nextState.foo.bar).toBe(baseState.anObject)321 })322323 it("can nest a draft in a new object (with changes)", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 324–326why, one step back
324 const nextState = produce(baseState, s => {
325 const obj = s.anObject326 delete s.anObject

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 327why, one step back
327

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 328–330why, one step back
328 obj.coffee = true // change
329 obj.nested.yummy = true // add330 delete obj.nested.yummie // delete

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 331why, one step back
331

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 332–333why, one step back
332 s.foo = {bar: obj}
333 })

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 334–343why, one step back
334 expect(nextState).not.toBe(baseState)
335 expect(nextState.foo.bar).not.toBe(baseState.anObject)336 expect(nextState.foo).toEqual({337 bar: {338 coffee: true,339 nested: {yummy: true}340 }341 })342 })343

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 344why, one step back
344 it("supports assigning undefined to an existing property", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 345–347why, one step back
345 const nextState = produce(baseState, s => {
346 s.aProp = undefined347 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 348why, one step back
348 expect(nextState).not.toBe(baseState)

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 349–357why, one step back
349 expect(nextState.aProp).toBe(undefined)
350 })351352 it("supports assigning undefined to a new property", () => {353 const baseState = {}354 const nextState = produce(baseState, s => {355 s.aProp = undefined356 })357 expect(nextState).not.toBe(baseState)

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 358–364why, one step back
358 expect(nextState.aProp).toBe(undefined)
359 })360361 // ES implementation does't protect against all outside modifications, just some..362 if (useProxies) {363 it("should revoke the proxy of the baseState after immer function is executed", () => {364 let proxy

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 365–368why, one step back
365 const nextState = produce(baseState, s => {
366 proxy = s367 s.aProp = "hello"368 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 369–387why, one step back
369 expect(nextState).not.toBe(baseState)
370 expect(baseState.aProp).toBe("hi")371 expect(nextState.aProp).toBe("hello")372373 expect(() => {374 proxy.aProp = "Hallo"375 }).toThrowError(/revoked/)376 expect(() => {377 const aProp = proxy.aProp378 }).toThrowError(/revoked/)379380 expect(nextState).not.toBe(baseState)381 expect(baseState.aProp).toBe("hi")382 expect(nextState.aProp).toBe("hello")383 })384 }385386 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {387 let proxy

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 388–390why, one step back
388 const nextState = produce(baseState, s => {
389 proxy = s.anObject390 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 391–400why, one step back
391 expect(nextState).toBe(baseState)
392 expect(() => {393 // In ES5 implemenation only protects existing props, but alas..394 proxy.coffee = "Hallo"395 }).toThrowError(/revoked/)396 expect(() => {397 const test = proxy.coffee398 }).toThrowError(/revoked/)399 })400

test: add missing edge casealeclarsonbefore adoptionc46af36

lines 401–408why, one step back
401 it("can access a child draft that was created before the draft was modified", () => {
402 produce({a: {}}, s => {403 const before = s.a404 s.b = 1405 expect(s.a).toBe(before)406 })407 })408

v1.2.0Michel Weststratebefore adoptione1e73c4

line 409why, one step back
409 it("should reflect all changes made in the draft immediately", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 410–412why, one step back
410 produce(baseState, draft => {
411 draft.anArray[0] = 5412 draft.anArray.unshift("test")

test: tiny improvementaleclarsonbefore adoptionee49da3

line 413why, one step back
413 expect(enumerableOnly(draft.anArray)).toEqual([

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 414–422why, one step back
414 "test",
415 5,416 2,417 {c: 3},418 1419 ])420 draft.stuffz = "coffee"421 expect(draft.stuffz).toBe("coffee")422 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 423–425why, one step back
423 })
424425 it("should be able to get property descriptors from objects", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 426–431why, one step back
426 produce({a: 1}, draft => {
427 expect("a" in draft).toBe(true)428 expect("b" in draft).toBe(false)429 expect(430 Reflect.ownKeys(draft).filter(x => typeof x === "string")431 ).toEqual(["a"])

v1.2.0Michel Weststratebefore adoptione1e73c4

line 432why, one step back
432

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 433–461why, one step back
433 expect(
434 Object.getOwnPropertyDescriptor(draft, "a")435 ).toMatchObject({436 configurable: true,437 enumerable: true438 })439 draft.a = 2440 expect(441 Object.getOwnPropertyDescriptor(draft, "a")442 ).toMatchObject({443 configurable: true,444 enumerable: true445 })446 expect(447 Object.getOwnPropertyDescriptor(draft, "b")448 ).toBeUndefined()449 draft.b = 2450 expect(451 Object.getOwnPropertyDescriptor(draft, "b")452 ).toMatchObject({453 configurable: true,454 enumerable: true455 })456 expect("a" in draft).toBe(true)457 expect("b" in draft).toBe(true)458 expect(459 Reflect.ownKeys(draft).filter(x => typeof x === "string")460 ).toEqual(["a", "b"])461 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 462–464why, one step back
462 })
463464 it("should be able to get property descriptors from arrays", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 465–473why, one step back
465 produce([1], draft => {
466 expect(0 in draft).toBe(true)467 expect(1 in draft).toBe(false)468 expect("0" in draft).toBe(true)469 expect("1" in draft).toBe(false)470 expect(length in draft).toBe(true)471 expect(472 Reflect.ownKeys(draft).filter(x => typeof x === "string")473 ).toEqual(["0", "length"])

v1.2.0Michel Weststratebefore adoptione1e73c4

line 474why, one step back
474

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 475–483why, one step back
475 expect(
476 Object.getOwnPropertyDescriptor(draft, "length")477 ).toMatchObject({478 configurable: false,479 enumerable: false480 })481 draft[0] = 2482 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(483 {

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 484–485why, one step back
484 configurable: true,
485 enumerable: true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 486–489why, one step back
486 }
487 )488 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(489 {

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 490–491why, one step back
490 configurable: true,
491 enumerable: true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 492–499why, one step back
492 }
493 )494 expect(495 Object.getOwnPropertyDescriptor(draft, 1)496 ).toBeUndefined()497 draft[1] = 2498 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(499 {

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 500–501why, one step back
500 configurable: true,
501 enumerable: true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 502–507why, one step back
502 }
503 )504 expect(505 Reflect.ownKeys(draft).filter(x => typeof x === "string")506 ).toEqual(["0", "1", "length"])507 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 508–512why, one step back
508 })
509510 if (useProxies === true) {511 it("should not be possible to set property descriptors", () => {512 expect(() => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 513–519why, one step back
513 produce({}, draft => {
514 Object.defineProperty(draft, "xx", {515 enumerable: true,516 writeable: true,517 value: 2518 })519 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 520–521why, one step back
520 }).toThrowError(/not support/)
521 })

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 522–543why, one step back
522
523 it("should not be possible to add properties to arrays", () => {524 expect(() => {525 produce([], d => {526 d.x = 3527 })528 }).toThrow(529 "Immer does not support setting non-numeric properties on arrays"530 )531 })532533 it("should not be possible to remove properties from arrays", () => {534 expect(() => {535 const base = []536 base.x = 7537 produce(base, d => {538 delete d.x539 })540 }).toThrow(541 "Immer does not support deleting properties from arrays"542 )543 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 544why, one step back
544 }

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 545–553why, one step back
545
546 it("non-numeric array properties will be lost", () => {547 const base = []548 base.x = 7549 const next = produce(base, d => {550 d.push(3)551 })552 expect(next.x).toBe(undefined)553 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 554–556why, one step back
554
555 it("should not throw error, see #53 - 1", () => {556 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 557–559why, one step back
557 const result = produce(base, draft => {
558 draft.arr = draft.arr.filter(item => item.count > 2)559 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 560–568why, one step back
560 expect(result.arr[0].count).toEqual(3)
561 expect(result).toEqual({562 arr: [{count: 3}]563 })564 expect(result.arr[0]).toBe(base.arr[2])565 })566567 it("should not throw error, see #53 - 2", () => {568 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 569–571why, one step back
569 const result = produce(base, draft => {
570 draft.newArr = draft.arr.filter(item => item.count > 2)571 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 572–597why, one step back
572 expect(result.newArr[0].count).toEqual(3)
573 expect(result.arr).toBe(base.arr)574 expect(result).toEqual({575 arr: [576 {577 count: 1578 },579 {580 count: 2581 },582 {583 count: 3584 }585 ],586 newArr: [587 {588 count: 3589 }590 ]591 })592 expect(result.newArr[0]).toBe(base.arr[2])593 expect(result.arr[2]).toBe(base.arr[2])594 })595596 it("should not throw error, see #53 - 3", () => {597 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 598–601why, one step back
598 const result = produce(base, draft => {
599 draft.newArr = draft.arr.filter(item => item.count > 2)600 delete draft.arr601 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 602–610why, one step back
602 expect(result.newArr[0].count).toEqual(3)
603 expect(result).toEqual({604 newArr: [{count: 3}]605 })606 expect(result.newArr[0]).toBe(base.arr[2])607 })608609 it("should not throw error, see #53 - 4", () => {610 const base = {bear: {age: 10}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 611–614why, one step back
611 const result = produce(base, draft => {
612 draft.bear.legs = 4613 draft.room = {elephant: {kiddo: draft.bear}}614 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 615–684why, one step back
615 expect(result).toEqual({
616 bear: {age: 10, legs: 4},617 room: {elephant: {kiddo: {age: 10, legs: 4}}}618 })619620 const result2 = produce(result, draft => {621 draft.bear.age = 11622 draft.room.elephant.kiddo.legs = 5623 })624 expect(result2).toEqual({625 bear: {age: 11, legs: 4},626 room: {elephant: {kiddo: {age: 10, legs: 5}}}627 })628 })629630 it("should handle constructor correctly", () => {631 const base = {632 arr: new Array(),633 obj: new Object()634 }635 const result = produce(base, draft => {636 draft.arrConstructed = draft.arr.constructor(1)637 draft.objConstructed = draft.obj.constructor(1)638 })639 expect(result.arrConstructed).toEqual(new Array().constructor(1))640 expect(result.objConstructed).toEqual(new Object().constructor(1))641 })642643 it("should handle dates correctly", () => {644 const data = {date: new Date()}645 const next = produce(data, draft => {646 draft.x = true647 })648 expect(next).toEqual({x: true, date: data.date})649 expect(next.date).toBe(data.date)650 const next2 = produce(next, draft => {651 draft.date.setYear(2015)652 })653 // This still holds; because produce won't proxy Date objects654 // and the original is actually modified!655 expect(next2).toEqual({x: true, date: data.date})656 expect(next2.date).toBe(next.date)657 expect(next2.date).toBe(data.date)658 expect(next2).toBe(next)659 })660661 it("should handle equality correctly - 1", () => {662 const base = {663 y: 3 / 0,664 z: NaN665 }666 const next = produce(base, draft => {667 draft.y = 4 / 0668 draft.z = NaN669 })670 expect(next).toEqual(base)671 expect(next).toBe(base)672 })673674 it("should handle equality correctly - 2", () => {675 const base = {676 x: -0677 }678 const next = produce(base, draft => {679 draft.x = +1680 })681 expect(next).not.toEqual(base)682 expect(next).not.toBe(base)683 })684

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 685–687why, one step back
685 // AKA: recursive produce calls
686 describe("a nested producer", () => {687 describe("when base state is not a draft", () => {

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 688–691why, one step back
688 // This test ensures the global state used to manage proxies is
689 // never left in a corrupted state by a nested `produce` call.690 it("never affects its parent producer implicitly", () => {691 const bear = {paw: {honey: true}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 692–698why, one step back
692 const next = produce(bear, draft => {
693 const paw2 = produce(bear.paw, draft => {694 draft.honey = false695 })696 expect(paw2.honey).toBe(false)697 expect(draft.paw.honey).toBe(true) // effects should not be visible outside698 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 699–701why, one step back
699 expect(next.paw.honey).toBe(true)
700 expect(next).toBe(bear)701 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 702why, one step back
702

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 703–704why, one step back
703 it("returns a normal object", () => {
704 const bear = {paw: {honey: true}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 705–713why, one step back
705 const next = produce(bear, draft => {
706 const paw2 = produce(bear.paw, draft => {707 draft.honey = false708 })709 expect(paw2.honey).toBe(false)710 expect(draft.paw.honey).toBe(true)711 draft.paw = paw2712 expect(draft.paw.honey).toBe(false)713 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 714–718why, one step back
714 expect(next.paw.honey).toBe(false)
715 expect(next).not.toBe(bear)716 })717 })718

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

line 719why, one step back
719 describe("when base state is a draft", () => {

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 720–721why, one step back
720 it("always reuses the draft", () => {
721 const bear = {paw: {honey: true}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 722–730why, one step back
722 const next = produce(bear, bear => {
723 const paw2 = produce(bear.paw, paw => {724 expect(paw).toBe(bear.paw)725 paw.honey = false726 })727 expect(paw2).toBe(bear.paw)728 expect(paw2.honey).toBe(false)729 expect(bear.paw.honey).toBe(false)730 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 731–732why, one step back
731 expect(next.paw.honey).toBe(false)
732 expect(next).not.toBe(bear)

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 733–772why, one step back
733 })
734 })735736 describe("when base state contains a draft", () => {737 it("wraps unowned draft with its own draft", () => {738 produce({a: {}}, parent => {739 produce({a: parent.a}, child => {740 expect(child.a).not.toBe(parent.a)741 expect(isDraft(child.a)).toBeTruthy()742 })743 })744 })745746 it("returns unowned draft if no changes were made", () => {747 produce({a: {}}, parent => {748 const result = produce({a: parent.a}, () => {})749 expect(result.a).toBe(parent.a)750 })751 })752753 it("clones the unowned draft when changes are made", () => {754 produce({a: {}}, parent => {755 const result = produce({a: parent.a}, child => {756 child.a.b = 1757 })758 expect(result.a).not.toBe(parent.a)759 expect(result.a.b).toBe(1)760 expect("b" in parent.a).toBeFalsy()761 })762 })763764 // We cannot auto-freeze the result of a nested producer,765 // because it may contain a draft from a parent producer.766 it("never auto-freezes the result", () => {767 produce({a: {}}, parent => {768 const r = produce({a: parent.a}, child => {769 child.b = 1 // Ensure a copy is returned.770 })771 expect(Object.isFrozen(r)).toBeFalsy()772 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 773–774why, one step back
773 })
774 })

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

lines 775–792why, one step back
775
776 // "Upvalues" are variables from a parent scope.777 it("does not finalize upvalue drafts", () => {778 produce({a: {}, b: {}}, parent => {779 expect(produce({}, () => parent)).toBe(parent)780 parent.x // Ensure proxy not revoked.781782 expect(produce({}, () => [parent])[0]).toBe(parent)783 parent.x // Ensure proxy not revoked.784785 expect(produce({}, () => parent.a)).toBe(parent.a)786 parent.a.x // Ensure proxy not revoked.787788 // Modified parent test789 parent.c = 1790 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)791 parent.b.x // Ensure proxy not revoked.792 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 793–796why, one step back
793 })
794795 it("works with interweaved Immer instances", () => {796 const options = {useProxies, autoFreeze}

test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64

lines 797–798why, one step back
797 const one = createPatchedImmer(options)
798 const two = createPatchedImmer(options)

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 799–813why, one step back
799
800 const base = {}801 const result = one.produce(base, s1 =>802 two.produce({s1}, s2 => {803 expect(original(s2.s1)).toBe(s1)804 s2.n = 1805 s2.s1 = one.produce({s2}, s3 => {806 expect(original(s3.s2)).toBe(s2)807 expect(original(s3.s2.s1)).toBe(s2.s1)808 return s3.s2.s1809 })810 })811 )812 expect(result.n).toBe(1)813 expect(result.s1).toBe(base)

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

line 814why, one step back
814 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 815–820why, one step back
815 })
816817 it("should not try to change immutable data, see #66", () => {818 const user = require("./test-data")819820 const base = {}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 821–823why, one step back
821 const next = produce(base, draft => {
822 draft.user = user823 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 824–832why, one step back
824 expect(next.user).toBe(user)
825 expect(next).not.toBe(base)826 expect(next.user).toEqual(user)827 })828829 it("should not try to change immutable data, see #66 - 2", () => {830 const user = deepFreeze(cloneDeep(require("./test-data")))831832 const base = {}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 833–835why, one step back
833 const next = produce(base, draft => {
834 draft.user = user835 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 836–842why, one step back
836 expect(next.user).toBe(user)
837 expect(next).not.toBe(base)838 expect(next.user).toEqual(user)839 })840841 it("should structurally share identical objects in the tree", () => {842 const base = {bear: {legs: 4}, eagle: {legs: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 843–850why, one step back
843 const next = produce(base, draft => {
844 const animal = draft.bear845 animal.legs = animal.legs + 1846 draft.bear = animal847 draft.eagle = animal848 draft.cow = animal849 draft.kiddo = animal850 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 851–862why, one step back
851 expect(next).toEqual({
852 bear: {legs: 5},853 eagle: {legs: 5},854 cow: {legs: 5},855 kiddo: {legs: 5}856 })857 expect(next.bear).toBe(next.cow)858 expect(next.kiddo).toBe(next.cow)859 })860861 if (useProxies)862 it("should not allow changing prototype", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 863–867why, one step back
863 produce({}, draft => {
864 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(865 /does not support `setPrototype/866 )867 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 868–870why, one step back
868 })
869870 it("'in' should work", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 871–873why, one step back
871 produce(createBaseState(), draft => {
872 expect("anArray" in draft).toBe(true)873 expect(Reflect.has(draft, "anArray")).toBe(true)

v1.2.0Michel Weststratebefore adoptione1e73c4

line 874why, one step back
874

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 875–876why, one step back
875 expect("bla" in draft).toBe(false)
876 expect(Reflect.has(draft, "bla")).toBe(false)

v1.2.0Michel Weststratebefore adoptione1e73c4

line 877why, one step back
877

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 878–881why, one step back
878 expect(0 in draft.anArray).toBe(true)
879 expect("0" in draft.anArray).toBe(true)880 expect(Reflect.has(draft.anArray, 0)).toBe(true)881 expect(Reflect.has(draft.anArray, "0")).toBe(true)

v1.2.0Michel Weststratebefore adoptione1e73c4

line 882why, one step back
882

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 883–887why, one step back
883 expect(17 in draft.anArray).toBe(false)
884 expect("17" in draft.anArray).toBe(false)885 expect(Reflect.has(draft.anArray, 17)).toBe(false)886 expect(Reflect.has(draft.anArray, "17")).toBe(false)887 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 888–891why, one step back
888 })
889890 it("'this' should work - 1", () => {891 const base = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 892–894why, one step back
892 const next1 = produce(base, function() {
893 this.x = 4894 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 895–908why, one step back
895 expect(next1).not.toBe(base)
896 expect(next1.x).toBe(4)897 })898899 it("'this' should work - 1", () => {900 const base = {x: 3}901 const incrementor = produce(function() {902 this.x = 4903 })904 const next1 = incrementor(base)905 expect(next1).not.toBe(base)906 expect(next1.x).toBe(4)907 })908

test: rename and expand a testaleclarsonbefore adoption733e16f

lines 909–914why, one step back
909 // See here: https://github.com/mweststrate/immer/issues/89
910 it("works with the spread operator", () => {911 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}912 const result = produce(base, draft => {913 draft.foo = {x: 1, ...draft.foo, y: 1}914 draft.bar = [1, ...draft.bar, 1]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 915why, one step back
915 })

test: rename and expand a testaleclarsonbefore adoption733e16f

lines 916–918why, one step back
916 expect(result).toEqual({
917 foo: {x: 0, y: 1},918 bar: [1, 0, 0, 1]

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 919–923why, one step back
919 })
920 })921922 it("processes with lodash.set", () => {923 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 924–926why, one step back
924 const result = produce(base, draft => {
925 lodash.set(draft, "[0].a", 2)926 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 927–932why, one step back
927 expect(base[0].a).toEqual(1)
928 expect(result[0].a).toEqual(2)929 })930931 it("processes with lodash.find", () => {932 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 933–936why, one step back
933 const result = produce(base, draft => {
934 const obj1 = lodash.find(draft, {id: 1})935 lodash.set(obj1, "a", 2)936 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 937–942why, one step back
937 expect(base[0].a).toEqual(1)
938 expect(result[0].a).toEqual(2)939 })940941 it("can produce from no state", () => {942 expect(

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 943–946why, one step back
943 produce(3, draft => {
944 expect(draft).toBe(3)945 return 5946 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 947–951why, one step back
947 ).toBe(5)
948 })949950 it("can return something new ", () => {951 const base = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 952–954why, one step back
952 const res = produce(base, draft => {
953 return {x: draft.x + 1}954 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 955–960why, one step back
955 expect(res).not.toBe(base)
956 expect(res).toEqual({x: 4})957 })958959 it("can return the draft new ", () => {960 const base = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 961–964why, one step back
961 const res = produce(base, draft => {
962 draft.x = 4963 return draft964 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 965–971why, one step back
965 expect(res).not.toBe(base)
966 expect(res).toEqual({x: 4})967 })968969 it("should throw if modifying the draft and returning something new", () => {970 const base = {x: 3}971 expect(() => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 972–975why, one step back
972 produce(base, draft => {
973 draft.x = 4974 return {x: 5}975 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 976–980why, one step back
976 }).toThrow(/An immer producer returned a new value/)
977 })978979 it("should fix #117", () => {980 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 981–988why, one step back
981 produce(state, draft => {
982 switch (action.type) {983 case "SET_STARTING_DOTS":984 return draft.availableStartingDots.map(a => a)985 default:986 break987 }988 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 989–1003why, one step back
989 const base = {
990 availableStartingDots: [991 {dots: 4, count: 1},992 {dots: 3, count: 2},993 {dots: 2, count: 3},994 {dots: 1, count: 4}995 ]996 }997 const next = reducer(base, {type: "SET_STARTING_DOTS"})998 expect(next).toEqual(base.availableStartingDots)999 expect(next).not.toBe(base.availableStartingDots)1000 })10011002 it("should fix #117 - 2", () => {1003 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1004–1013why, one step back
1004 produce(state, draft => {
1005 switch (action.type) {1006 case "SET_STARTING_DOTS":1007 return {1008 dots: draft.availableStartingDots.map(a => a)1009 }1010 default:1011 break1012 }1013 })

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 1014–1025why, one step back
1014 const base = {
1015 availableStartingDots: [1016 {dots: 4, count: 1},1017 {dots: 3, count: 2},1018 {dots: 2, count: 3},1019 {dots: 1, count: 4}1020 ]1021 }1022 const next = reducer(base, {type: "SET_STARTING_DOTS"})1023 expect(next).toEqual({dots: base.availableStartingDots})1024 })1025

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

lines 1026–1027why, one step back
1026 it("should return an unmodified primitive baseState (#148)", () => {
1027 const baseState = "some string"

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1028–1030why, one step back
1028 const nextState = produce(baseState, () => {
1029 /* no modification */1030 })

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

lines 1031–1035why, one step back
1031 expect(nextState).toBe(baseState)
1032 })10331034 it("should return an unmodified null baseState (#148)", () => {1035 const baseState = null

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1036–1038why, one step back
1036 const nextState = produce(baseState, () => {
1037 /* no modification */1038 })

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

line 1039why, one step back
1039 expect(nextState).toBe(baseState)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1040–1043why, one step back
1040 })
10411042 it("should not detect noop assignments - 0", () => {1043 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1044–1047why, one step back
1044 const nextState = produce(baseState, d => {
1045 const a = d.x1046 d.x = a1047 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1048–1052why, one step back
1048 expect(nextState).toBe(baseState)
1049 })10501051 it("should not detect noop assignments - 1", () => {1052 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1053–1057why, one step back
1053 const nextState = produce(baseState, d => {
1054 const a = d.x1055 d.x = 41056 d.x = a1057 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1058–1065why, one step back
1058 // Ideally, this should actually be the same instances
1059 // but this would be pretty expensive to detect,1060 // so we don't atm1061 expect(nextState).not.toBe(baseState)1062 })10631064 it("should not detect noop assignments - 2", () => {1065 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1066–1071why, one step back
1066 const nextState = produce(baseState, d => {
1067 const a = d.x1068 const stuff = a.y + 31069 d.x = 41070 d.x = a1071 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1072–1079why, one step back
1072 // Ideally, this should actually be the same instances
1073 // but this would be pretty expensive to detect,1074 // so we don't atm1075 expect(nextState).not.toBe(baseState)1076 })10771078 it("should not detect noop assignments - 3", () => {1079 const baseState = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1080–1082why, one step back
1080 const nextState = produce(baseState, d => {
1081 d.x = 31082 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1083–1087why, one step back
1083 expect(nextState).toBe(baseState)
1084 })10851086 it("should not detect noop assignments - 4", () => {1087 const baseState = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1088–1091why, one step back
1088 const nextState = produce(baseState, d => {
1089 d.x = 41090 d.x = 31091 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1092–1095why, one step back
1092 // Ideally, this should actually be the same instances
1093 // but this would be pretty expensive to detect,1094 // so we don't atm1095 expect(nextState).not.toBe(baseState)

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

lines 1096–1097why, one step back
1096 })
1097

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 1098–1101why, one step back
1098 it("immer should have no dependencies", () => {
1099 expect(require("../package.json").dependencies).toEqual(undefined)1100 })1101

Emphasized in readme that state should be a treeMichel Weststratebefore adoption211f5cf

lines 1102–1109why, one step back
1102 it("#174", () => {
1103 const nextState = produce([1, 2, 3], s => {1104 s.pop()1105 s.push(100)1106 })1107 expect(nextState).toEqual([1, 2, 100])1108 })1109

Tried to reproduce #195Michel Weststratebefore adoption4f27a93

lines 1110–1124why, one step back
1110 it("#195 should be able to find items", () => {
1111 const state = {1112 items: [1113 {1114 id: 0,1115 task: "drink milk"1116 },1117 {id: 1, task: "eat cookie"}1118 ]1119 }1120 produce(state, draft => {1121 expect(draft.items.find(({id}) => id === 1).task).toBe(1122 "eat cookie"1123 )1124 })

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 1125–1126why, one step back
1125 })
1126

add tests for function as base statealeclarsonbefore adoption4613b06

lines 1127–1135why, one step back
1127 it("allows a function as the base state", () => {
1128 let fn = () => {}1129 expect(1130 produce(fn, draft => {1131 expect(fn).toBe(draft)1132 })1133 ).toBe(fn)1134 })1135

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 1136–1150why, one step back
1136 it("cannot return and produce undefined!", () => {
1137 const base = 31138 expect(produce(base, () => 4)).toBe(4)1139 expect(produce(base, () => null)).toBe(null)1140 expect(produce(base, () => undefined)).toBe(3)1141 expect(produce(base, () => {})).toBe(3)1142 expect(produce(base, () => nothing)).toBe(undefined)11431144 expect(produce({}, () => undefined)).toEqual({})1145 expect(produce({}, () => nothing)).toBe(undefined)1146 expect(produce(3, () => nothing)).toBe(undefined)11471148 expect(produce(() => undefined)({})).toEqual({})1149 expect(produce(() => nothing)({})).toBe(undefined)1150 expect(produce(() => nothing)(3)).toBe(undefined)

Tried to reproduce #195Michel Weststratebefore adoption4f27a93

lines 1151–1152why, one step back
1151 })
1152

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1153why, one step back
1153 testBaseStateTypes(produce)

Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417

line 1154why, one step back
1154

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 1155–1170why, one step back
1155 afterEach(() => {
1156 expect(baseState).toBe(origBaseState)1157 expect(baseState).toEqual(createBaseState())1158 })11591160 function createBaseState() {1161 const data = {1162 anArray: [3, 2, {c: 3}, 1],1163 aProp: "hi",1164 anObject: {1165 nested: {1166 yummie: true1167 },1168 coffee: false1169 }1170 }

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1171why, one step back
1171 return autoFreeze ? deepFreeze(data) : data

v1.2.0Michel Weststratebefore adoptione1e73c4

line 1172why, one step back
1172 }

Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417

lines 1173–1213why, one step back
1173 })
11741175 describe(`isDraft - ${name}`, () => {1176 it("returns true for object drafts", () => {1177 produce({}, state => {1178 expect(isDraft(state)).toBeTruthy()1179 })1180 })1181 it("returns true for array drafts", () => {1182 produce([], state => {1183 expect(isDraft(state)).toBeTruthy()1184 })1185 })1186 it("returns true for objects nested in object drafts", () => {1187 produce({a: {b: {}}}, state => {1188 expect(isDraft(state.a)).toBeTruthy()1189 expect(isDraft(state.a.b)).toBeTruthy()1190 })1191 })1192 it("returns false for new objects added to a draft", () => {1193 produce({}, state => {1194 state.a = {}1195 expect(isDraft(state.a)).toBeFalsy()1196 })1197 })1198 it("returns false for objects returned by the producer", () => {1199 const object = produce(null, Object.create)1200 expect(isDraft(object)).toBeFalsy()1201 })1202 it("returns false for arrays returned by the producer", () => {1203 const array = produce(null, _ => [])1204 expect(isDraft(array)).toBeFalsy()1205 })1206 it("returns false for object drafts returned by the producer", () => {1207 const object = produce({}, state => state)1208 expect(isDraft(object)).toBeFalsy()1209 })1210 it("returns false for array drafts returned by the producer", () => {1211 const array = produce([], state => state)1212 expect(isDraft(array)).toBeFalsy()1213 })

v1.2.0Michel Weststratebefore adoptione1e73c4

line 1214why, one step back
1214 })

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1215–1296why, one step back
1215}
12161217function testBaseStateTypes(produce) {1218 class Foo {}1219 const primitives = {1220 "falsy number": 0,1221 "truthy number": 1,1222 "negative number": -1,1223 infinity: 1 / 0,1224 true: true,1225 false: false,1226 "empty string": "",1227 "truthy string": "1",1228 null: null,1229 undefined: undefined,12301231 /**1232 * These objects are treated as primitives because Immer1233 * chooses not to make drafts for them.1234 */1235 "regexp object": /.+/g,1236 "boxed number": new Number(0),1237 "boxed string": new String(""),1238 "boxed boolean": new Boolean(),1239 "date object": new Date(),1240 "class instance": new Foo()1241 }1242 for (const name in primitives) {1243 describe("base state type - " + name, () => {1244 const value = primitives[name]1245 it("does not create a draft", () => {1246 produce(value, draft => {1247 expect(draft).toBe(value)1248 })1249 })1250 it("returns the same value when the producer returns undefined", () => {1251 expect(produce(value, () => {})).toBe(value)1252 })1253 if (value && typeof value == "object") {1254 it("does not return a copy when the producer makes changes", () => {1255 expect(1256 produce(value, draft => {1257 draft.foo = true1258 })1259 ).toBe(value)1260 })1261 }1262 })1263 }1264 const objects = {1265 "empty object": {},1266 "plain object": {a: 1, b: {c: 1}},1267 "frozen object": Object.freeze({}),1268 "null-prototype object": Object.create(null),1269 "frozen null-prototype object": Object.freeze(Object.create(null)),1270 "empty array": [],1271 "plain array": [1, [2, [3, []]]],1272 "frozen array": Object.freeze([])1273 }1274 for (const name in objects) {1275 describe("base state type - " + name, () => {1276 const value = objects[name]1277 it("creates a draft", () => {1278 produce(value, draft => {1279 expect(draft).not.toBe(value)1280 expect(enumerableOnly(draft)).toEqual(value)1281 })1282 })1283 it("returns the same value when the producer does nothing", () => {1284 expect(produce(value, () => {})).toBe(value)1285 })1286 it("returns a copy when changes are made", () => {1287 const random = Math.random()1288 const result = produce(value, draft => {1289 draft[0] = random1290 })1291 expect(result).not.toBe(value)1292 expect(result.constructor).toBe(value.constructor)1293 expect(result[0]).toBe(random)1294 })1295 })1296 }

v1.2.0Michel Weststratebefore adoptione1e73c4

lines 1297–1299why, one step back
1297}
12981299function enumerableOnly(x) {

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

lines 1300–1307why, one step back
1300 const copy = shallowCopy(x)
1301 for (const key in copy) {1302 const value = copy[key]1303 if (value && typeof value === "object") {1304 copy[key] = enumerableOnly(value)1305 }1306 }1307 return copy

v1.2.0Michel Weststratebefore adoptione1e73c4

line 1308why, one step back
1308}