base.js
This file
Path
__tests__/base.js
639 lines · 22.6 KiB · JavaScript
Last changed by
Freeze new dataMichel Weststratebefore adoption8b6d3ea
History of this fileTake it
1"use strict"2import produce, {setAutoFreeze, setUseProxies} from "../src"3import deepFreeze from "deep-freeze"4import cloneDeep from "lodash.clonedeep"56jest.setTimeout(1000)78runBaseTest("proxy (no freeze)", true, false)9runBaseTest("proxy (autofreeze)", true, true)10runBaseTest("es5 (no freeze)", false, false)11runBaseTest("es5 (autofreeze)", false, true)1213function runBaseTest(name, useProxies, freeze) {14 describe(`base functionality - ${name}`, () => {15 let baseState16 let origBaseState1718 beforeEach(() => {19 setAutoFreeze(freeze)20 setUseProxies(useProxies)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 (useProxies) {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 (useProxies === true) {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 })432 }433434 it("should not throw error, see #53 - 1", () => {435 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}436 const result = produce(base, draft => {437 draft.arr = draft.arr.filter(item => item.count > 2)438 })439 expect(result.arr[0].count).toEqual(3)440 expect(result).toEqual({441 arr: [{count: 3}]442 })443 expect(result.arr[0]).toBe(base.arr[2])444 })445446 it("should not throw error, see #53 - 2", () => {447 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}448 const result = produce(base, draft => {449 draft.newArr = draft.arr.filter(item => item.count > 2)450 })451 expect(result.newArr[0].count).toEqual(3)452 expect(result.arr).toBe(base.arr)453 expect(result).toEqual({454 arr: [455 {456 count: 1457 },458 {459 count: 2460 },461 {462 count: 3463 }464 ],465 newArr: [466 {467 count: 3468 }469 ]470 })471 expect(result.newArr[0]).toBe(base.arr[2])472 expect(result.arr[2]).toBe(base.arr[2])473 })474475 it("should not throw error, see #53 - 3", () => {476 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}477 const result = produce(base, draft => {478 draft.newArr = draft.arr.filter(item => item.count > 2)479 delete draft.arr480 })481 expect(result.newArr[0].count).toEqual(3)482 expect(result).toEqual({483 newArr: [{count: 3}]484 })485 expect(result.newArr[0]).toBe(base.arr[2])486 })487488 it("should not throw error, see #53 - 4", () => {489 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 it("should not try to change immutable data, see #66", () => {580 const user = require("./test-data")581582 const base = {}583 const next = produce(base, draft => {584 draft.user = user585 })586 expect(next.user).toBe(user)587 expect(next).not.toBe(base)588 expect(next.user).toEqual(user)589 })590591 it("should not try to change immutable data, see #66 - 2", () => {592 const user = deepFreeze(cloneDeep(require("./test-data")))593594 const base = {}595 const next = produce(base, draft => {596 draft.user = user597 })598 expect(next.user).toBe(user)599 expect(next).not.toBe(base)600 expect(next.user).toEqual(user)601 })602603 if (freeze)604 it("should freeze new data well", () => {605 const base = {}606 const next = produce(base, draft => {607 draft.x = {y: [{z: true}]}608 })609 expect(Object.isFrozen(next)).toBe(true)610 expect(Object.isFrozen(next.x)).toBe(true)611 expect(Object.isFrozen(next.x.y)).toBe(true)612 expect(Object.isFrozen(next.x.y[0].z)).toBe(true)613 })614615 afterEach(() => {616 expect(baseState).toBe(origBaseState)617 expect(baseState).toEqual(createBaseState())618 })619620 function createBaseState() {621 const data = {622 anArray: [3, 2, {c: 3}, 1],623 aProp: "hi",624 anObject: {625 nested: {626 yummie: true627 },628 coffee: false629 }630 }631 return freeze ? deepFreeze(data) : data632 }633 })634}635636function enumerableOnly(x) {637 // this can be done better...638 return JSON.parse(JSON.stringify(x))639}