base.js
This file
Path
__tests__/base.js
603 lines · 21.3 KiB · JavaScript
Last changed by
Making several improvements at onceMichel Weststratebefore adoption1116ccf
History of this fileTake it
1"use strict"2import * as immerProxy from "../src/immer"3import * as immerEs5 from "../src/es5"4import deepFreeze from "deep-freeze"56jest.setTimeout(1000)78runBaseTest("proxy (no freeze)", immerProxy, false)9runBaseTest("proxy (autofreeze)", immerProxy, true)10runBaseTest("es5 (no freeze)", immerEs5, false)11runBaseTest("es5 (autofreeze)", immerEs5, true)1213function runBaseTest(name, lib, freeze) {14 describe(`base functionality - ${name}`, () => {15 const produce = lib.default16 let baseState17 let origBaseState1819 beforeEach(() => {20 lib.setAutoFreeze(freeze)21 origBaseState = baseState = createBaseState()22 })2324 it("should return the original without modifications", () => {25 const nextState = produce(baseState, () => {})26 expect(nextState).toBe(baseState)27 })2829 it("should return the original without modifications when reading stuff", () => {30 const nextState = produce(baseState, s => {31 expect(s.aProp).toBe("hi")32 expect(s.anObject.nested).toMatchObject({yummie: true})33 })34 expect(nextState).toBe(baseState)35 })3637 it("should not return any value: thunk", () => {38 const warning = jest.spyOn(console, "warn")39 produce(baseState, () => ({bad: "don't do this"}))40 produce(baseState, () => [1, 2, 3])41 produce(baseState, () => false)42 produce(baseState, () => "")4344 expect(warning).toHaveBeenCalledTimes(4)45 warning.mockClear()46 })4748 it("should return a copy when modifying stuff", () => {49 const nextState = produce(baseState, s => {50 s.aProp = "hello world"51 })52 expect(nextState).not.toBe(baseState)53 expect(baseState.aProp).toBe("hi")54 expect(nextState.aProp).toBe("hello world")55 // structural sharing?56 expect(nextState.nested).toBe(baseState.nested)57 })5859 if (60 ("should preserve type",61 () => {62 const nextState = produce(baseState, s => {63 expect(Array.isArray(s)).toBe(true)64 expect(s.protoType).toBe(Object)65 s.anArray.push(3)66 s.aProp = "hello world"67 expect(Array.isArray(s)).toBe(true)68 expect(s.protoType).toBe(Object)69 })70 expect(Array.isArray(nextState)).toBe(true)71 expect(nextState.protoType).toBe(Object)72 })73 )74 it("deep change bubbles up", () => {75 const nextState = produce(baseState, s => {76 s.anObject.nested.yummie = false77 })78 expect(nextState).not.toBe(baseState)79 expect(nextState.anObject).not.toBe(baseState.anObject)80 expect(baseState.anObject.nested.yummie).toBe(true)81 expect(nextState.anObject.nested.yummie).toBe(false)82 expect(nextState.anArray).toBe(baseState.anArray)83 })8485 it("can add props", () => {86 const nextState = produce(baseState, s => {87 s.anObject.cookie = {tasty: true}88 })89 expect(nextState).not.toBe(baseState)90 expect(nextState.anObject).not.toBe(baseState.anObject)91 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)92 expect(nextState.anObject.cookie).toEqual({tasty: true})93 })9495 it("can delete props", () => {96 const nextState = produce(baseState, s => {97 delete s.anObject.nested98 })99 expect(nextState).not.toBe(baseState)100 expect(nextState.anObject).not.toBe(baseState.anObject)101 expect(nextState.anObject.nested).toBe(undefined)102 })103104 it("ignores single non-modification", () => {105 const nextState = produce(baseState, s => {106 s.aProp = "hi"107 })108 expect(nextState).toBe(baseState)109 })110111 it("processes single modification", () => {112 const nextState = produce(baseState, s => {113 s.aProp = "hello"114 s.aProp = "hi"115 })116 expect(nextState).not.toBe(baseState)117 expect(nextState).toEqual(baseState)118 })119120 it("should support reading arrays", () => {121 const nextState = produce(baseState, s => {122 s.anArray.slice()123 })124 expect(nextState.anArray).toBe(baseState.anArray)125 expect(nextState).toBe(baseState)126 })127128 it("should support changing arrays", () => {129 const nextState = produce(baseState, s => {130 s.anArray[3] = true131 })132 expect(nextState).not.toBe(baseState)133 expect(nextState.anArray).not.toBe(baseState.anArray)134 expect(nextState.anArray[3]).toEqual(true)135 })136137 it("should support changing arrays - 2", () => {138 const nextState = produce(baseState, s => {139 s.anArray.splice(1, 1, "a", "b")140 })141 expect(nextState).not.toBe(baseState)142 expect(nextState.anArray).not.toBe(baseState.anArray)143144 expect(enumerableOnly(nextState.anArray)).toEqual([145 3,146 "a",147 "b",148 {c: 3},149 1150 ])151 })152153 it("can delete array items", () => {154 const nextState = produce(baseState, s => {155 s.anArray.length = 3156 })157 expect(nextState).not.toBe(baseState)158 expect(nextState.anObject).toBe(baseState.anObject)159 expect(nextState.anArray).not.toBe(baseState.anArray)160 expect(nextState.anArray).toEqual([3, 2, {c: 3}])161 })162163 it("should support sorting arrays", () => {164 const nextState = produce(baseState, s => {165 s.anArray[2].c = 4166 s.anArray.sort()167 s.anArray[3].c = 5168 })169 expect(nextState).not.toBe(baseState)170 expect(nextState.anArray).not.toBe(baseState.anArray)171 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])172 })173174 it("should expose property descriptors", () => {175 const nextState = produce([], s => {176 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)177 s.unshift("x")178 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({179 configurable: true,180 enumerable: true,181 value: "x",182 writable: true183 })184 expect(s.length).toBe(1)185 expect(s[0] === "x").toBe(true)186 })187 expect(nextState).toEqual(["x"])188 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({189 configurable: !freeze,190 enumerable: true,191 value: "x",192 writable: !freeze193 })194 })195196 it("should support sorting arrays - 2", () => {197 const nextState = produce(baseState, s => {198 s.anArray.unshift("x")199 s.anArray[3].c = 4200 s.anArray.sort()201 s.anArray[3].c = 5202 s.anArray.unshift("y")203 })204 expect(nextState).not.toBe(baseState)205 expect(nextState.anArray).not.toBe(baseState.anArray)206 expect(enumerableOnly(nextState.anArray)).toEqual([207 "y",208 1,209 2,210 3,211 {c: 5},212 "x"213 ])214 })215216 it("should updating inside arrays", () => {217 const nextState = produce(baseState, s => {218 s.anArray[2].test = true219 })220 expect(nextState).not.toBe(baseState)221 expect(nextState.anArray).not.toBe(baseState.anArray)222 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])223 })224225 it("reusing object should work", () => {226 const nextState = produce(baseState, s => {227 const obj = s.anObject228 delete s.anObject229 s.messy = obj230 })231 expect(nextState).not.toBe(baseState)232 expect(nextState.anArray).toBe(baseState.anArray)233 expect(enumerableOnly(nextState)).toEqual({234 anArray: [3, 2, {c: 3}, 1],235 aProp: "hi",236 messy: {237 nested: {238 yummie: true239 },240 coffee: false241 }242 })243 expect(nextState.messy.nested).toBe(baseState.anObject.nested)244 })245246 it("refs should be transparent", () => {247 const nextState = produce(baseState, s => {248 const obj = s.anObject249 s.aProp = "hello"250 delete s.anObject251 obj.coffee = true252 s.messy = obj253 })254 expect(nextState).not.toBe(baseState)255 expect(nextState.anArray).toBe(baseState.anArray)256 expect(enumerableOnly(nextState)).toEqual({257 anArray: [3, 2, {c: 3}, 1],258 aProp: "hello",259 messy: {260 nested: {261 yummie: true262 },263 coffee: true264 }265 })266 expect(nextState.messy.nested).toBe(baseState.anObject.nested)267 })268269 it("should allow setting to undefined a defined draft property", () => {270 const nextState = produce(baseState, s => {271 s.aProp = undefined272 })273 expect(nextState).not.toBe(baseState)274 expect(baseState.aProp).toBe("hi")275 expect(nextState.aProp).toBe(undefined)276 })277278 // ES implementation does't protect against all outside modifications, just some..279 if (lib === immerProxy) {280 it("should revoke the proxy of the baseState after immer function is executed", () => {281 let proxy282 const nextState = produce(baseState, s => {283 proxy = s284 s.aProp = "hello"285 })286 expect(nextState).not.toBe(baseState)287 expect(baseState.aProp).toBe("hi")288 expect(nextState.aProp).toBe("hello")289290 expect(() => {291 proxy.aProp = "Hallo"292 }).toThrowError(/revoked/)293 expect(() => {294 const aProp = proxy.aProp295 }).toThrowError(/revoked/)296297 expect(nextState).not.toBe(baseState)298 expect(baseState.aProp).toBe("hi")299 expect(nextState.aProp).toBe("hello")300 })301 }302303 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {304 let proxy305 const nextState = produce(baseState, s => {306 proxy = s.anObject307 })308 expect(nextState).toBe(baseState)309 expect(() => {310 // In ES5 implemenation only protects existing props, but alas..311 proxy.coffee = "Hallo"312 }).toThrowError(/revoked/)313 expect(() => {314 const test = proxy.coffee315 }).toThrowError(/revoked/)316 })317318 it("should reflect all changes made in the draft immediately", () => {319 produce(baseState, draft => {320 draft.anArray[0] = 5321 draft.anArray.unshift("test")322 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols323 expect(draft.anArray.slice()).toMatchObject([324 "test",325 5,326 2,327 {c: 3},328 1329 ])330 draft.stuffz = "coffee"331 expect(draft.stuffz).toBe("coffee")332 })333 })334335 it("should be able to get property descriptors from objects", () => {336 produce({a: 1}, draft => {337 expect("a" in draft).toBe(true)338 expect("b" in draft).toBe(false)339 expect(340 Reflect.ownKeys(draft).filter(x => typeof x === "string")341 ).toEqual(["a"])342343 expect(344 Object.getOwnPropertyDescriptor(draft, "a")345 ).toMatchObject({346 configurable: true,347 enumerable: true348 })349 draft.a = 2350 expect(351 Object.getOwnPropertyDescriptor(draft, "a")352 ).toMatchObject({353 configurable: true,354 enumerable: true355 })356 expect(357 Object.getOwnPropertyDescriptor(draft, "b")358 ).toBeUndefined()359 draft.b = 2360 expect(361 Object.getOwnPropertyDescriptor(draft, "b")362 ).toMatchObject({363 configurable: true,364 enumerable: true365 })366 expect("a" in draft).toBe(true)367 expect("b" in draft).toBe(true)368 expect(369 Reflect.ownKeys(draft).filter(x => typeof x === "string")370 ).toEqual(["a", "b"])371 })372 })373374 it("should be able to get property descriptors from arrays", () => {375 produce([1], draft => {376 expect(0 in draft).toBe(true)377 expect(1 in draft).toBe(false)378 expect("0" in draft).toBe(true)379 expect("1" in draft).toBe(false)380 expect(length in draft).toBe(true)381 expect(382 Reflect.ownKeys(draft).filter(x => typeof x === "string")383 ).toEqual(["0", "length"])384385 expect(386 Object.getOwnPropertyDescriptor(draft, "length")387 ).toMatchObject({388 configurable: false,389 enumerable: false390 })391 draft[0] = 2392 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(393 {394 configurable: true,395 enumerable: true396 }397 )398 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(399 {400 configurable: true,401 enumerable: true402 }403 )404 expect(405 Object.getOwnPropertyDescriptor(draft, 1)406 ).toBeUndefined()407 draft[1] = 2408 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(409 {410 configurable: true,411 enumerable: true412 }413 )414 expect(415 Reflect.ownKeys(draft).filter(x => typeof x === "string")416 ).toEqual(["0", "1", "length"])417 })418 })419420 if (lib === immerProxy)421 it("should not be possible to set property descriptors", () => {422 expect(() => {423 produce({}, draft => {424 Object.defineProperty(draft, "xx", {425 enumerable: true,426 writeable: true,427 value: 2428 })429 })430 }).toThrowError(/not support/)431 })432433 it("should not throw error, see #53 - 1", () => {434 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}435 const result = produce(base, draft => {436 draft.arr = draft.arr.filter(item => item.count > 2)437 })438 expect(result.arr[0].count).toEqual(3)439 expect(result).toEqual({440 arr: [{count: 3}]441 })442 expect(result.arr[0]).toBe(base.arr[2])443 })444445 it("should not throw error, see #53 - 2", () => {446 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}447 const result = produce(base, draft => {448 draft.newArr = draft.arr.filter(item => item.count > 2)449 })450 expect(result.newArr[0].count).toEqual(3)451 expect(result.arr).toBe(base.arr)452 expect(result).toEqual({453 arr: [454 {455 count: 1456 },457 {458 count: 2459 },460 {461 count: 3462 }463 ],464 newArr: [465 {466 count: 3467 }468 ]469 })470 expect(result.newArr[0]).toBe(base.arr[2])471 expect(result.arr[2]).toBe(base.arr[2])472 })473474 it("should not throw error, see #53 - 3", () => {475 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}476 const result = produce(base, draft => {477 draft.newArr = draft.arr.filter(item => item.count > 2)478 delete draft.arr479 })480 expect(result.newArr[0].count).toEqual(3)481 expect(result).toEqual({482 newArr: [{count: 3}]483 })484 expect(result.newArr[0]).toBe(base.arr[2])485 })486487 it("should not throw error, see #53 - 4", () => {488 debugger489 const base = {bear: {age: 10}}490 const result = produce(base, draft => {491 draft.bear.legs = 4492 draft.room = {elephant: {kiddo: draft.bear}}493 })494 expect(result).toEqual({495 bear: {age: 10, legs: 4},496 room: {elephant: {kiddo: {age: 10, legs: 4}}}497 })498499 const result2 = produce(result, draft => {500 draft.bear.age = 11501 draft.room.elephant.kiddo.legs = 5502 })503 expect(result2).toEqual({504 bear: {age: 11, legs: 4},505 room: {elephant: {kiddo: {age: 10, legs: 5}}}506 })507 })508509 it("should handle dates correctly", () => {510 const data = {date: new Date()}511 const next = produce(data, draft => {512 draft.x = true513 })514 expect(next).toEqual({x: true, date: data.date})515 expect(next.date).toBe(data.date)516 const next2 = produce(next, draft => {517 draft.date.setYear(2015)518 })519 // This still holds; because produce won't proxy Date objects520 // and the original is actually modified!521 expect(next2).toEqual({x: true, date: data.date})522 expect(next2.date).toBe(next.date)523 expect(next2.date).toBe(data.date)524 expect(next2).toBe(next)525 })526527 it("should handle equality correctly - 1", () => {528 const base = {529 y: 3 / 0,530 z: NaN531 }532 const next = produce(base, draft => {533 draft.y = 4 / 0534 draft.z = NaN535 })536 expect(next).toEqual(base)537 expect(next).toBe(base)538 })539540 it("should handle equality correctly - 2", () => {541 const base = {542 x: -0543 }544 const next = produce(base, draft => {545 draft.x = +1546 })547 expect(next).not.toEqual(base)548 expect(next).not.toBe(base)549 })550551 it("should handle nested immer calls correctly - 1", () => {552 const bear = {paw: {honey: true}}553 const next = produce(bear, draft => {554 const paw2 = produce(bear.paw, draft => {555 draft.honey = false556 })557 expect(paw2.honey).toBe(false)558 expect(draft.paw.honey).toBe(true) // effects should not be visible outside559 })560 expect(next.paw.honey).toBe(true)561 expect(next).toBe(bear)562 })563564 it("should handle nested immer calls correctly - 2", () => {565 const bear = {paw: {honey: true}}566 const next = produce(bear, draft => {567 const paw2 = produce(bear.paw, draft => {568 draft.honey = false569 })570 expect(paw2.honey).toBe(false)571 expect(draft.paw.honey).toBe(true)572 draft.paw = paw2573 expect(draft.paw.honey).toBe(false)574 })575 expect(next.paw.honey).toBe(false)576 expect(next).not.toBe(bear)577 })578579 afterEach(() => {580 expect(baseState).toBe(origBaseState)581 expect(baseState).toEqual(createBaseState())582 })583584 function createBaseState() {585 const data = {586 anArray: [3, 2, {c: 3}, 1],587 aProp: "hi",588 anObject: {589 nested: {590 yummie: true591 },592 coffee: false593 }594 }595 return freeze ? deepFreeze(data) : data596 }597 })598}599600function enumerableOnly(x) {601 // this can be done better...602 return JSON.parse(JSON.stringify(x))603}