chore: improve more tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- Removed a bunch of obsolete tests - Refactored a ton of code - Added some missing edge cases
authored by aleclarson · committed by Alec Larson · parent b4964ad · Browse files · 1 file·+197 added−509 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+197 added−509 removed
- tests:+197 added−509 removed
Provenance
- Commit
4f59b12- Tree
916a85c- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
b4964ad
1 file changed+197 added−509 removed
modified:__tests__/base.js+197 added-509 removed
5 unmodified lines6 import * as lodash from "lodash"7 8 jest.setTimeout(1000)9 10+test("immer should have no dependencies", () => {11+ expect(require("../package.json").dependencies).toBeUndefined()12+})13+14 runBaseTest("proxy (no freeze)", true, false)15 runBaseTest("proxy (autofreeze)", true, true)16 runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)17 runBaseTest("es5 (no freeze)", false, false)46 unmodified lines64 expect(nextState.aProp).toBe(random)65 expect(nextState.nested).toBe(baseState.nested)66 })67 64- it("preserves the prototype of drafts", () => {65- const getProto = Object.getPrototypeOf66- const nextState = produce(baseState, s => {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)71- })72- expect(getProto(nextState)).toBe(Object.prototype)73- expect(getProto(nextState.anArray)).toBe(Array.prototype)74- })75-68 it("deep change bubbles up", () => {69 const nextState = produce(baseState, s => {70 s.anObject.nested.yummie = false71 })37 unmodified lines109 expect(nextState).toBe(baseState)110 }111 })112 121- it("ignores single non-modification", () => {122- const nextState = produce(baseState, s => {123- s.aProp = "hi"124- })125- expect(nextState).toBe(baseState)126- })113+ it("can get property descriptors", () => {114+ const getDescriptor = Object.getOwnPropertyDescriptor115+ const baseState = deepFreeze([{a: 1}])116+ produce(baseState, draft => {117+ const obj = draft[0]118 128- // Once a draft is marked as modified, it stays that way.129- it("never removes modified properties from its internal state", () => {130- const nextState = produce(baseState, s => {131- const original = s.aProp132- // This assignment marks the draft as modified.133- s.aProp = "hello"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 = original119+ // Known property120+ expect(getDescriptor(obj, "a")).toMatchObject({121+ configurable: true,122+ enumerable: true123+ })124+ expect(getDescriptor(draft, 0)).toMatchObject({125+ configurable: true,126+ enumerable: true127+ })128+129+ // Deleted property130+ delete obj.a131+ draft.pop()132+ expect(getDescriptor(obj, "a")).toBeUndefined()133+ expect(getDescriptor(draft, 0)).toBeUndefined()134+135+ // Unknown property136+ expect(getDescriptor(obj, "b")).toBeUndefined()137+ expect(getDescriptor(draft, 100)).toBeUndefined()138+139+ // Added property140+ obj.b = 2141+ draft[100] = 1142+ expect(getDescriptor(obj, "b")).toBeDefined()143+ expect(getDescriptor(draft, 100)).toBeDefined()144 })138- expect(nextState).not.toBe(baseState)139- expect(nextState).toEqual(baseState)145 })146 147 describe("array drafts", () => {148 it("supports Array.isArray()", () => {93 unmodified lines242 expect(nextState).not.toBe(baseState)243 expect(nextState[0].a).toBe(2)244 expect(nextState[1].a).toBe(0)245 })241- })246 243- it("supports access of property descriptors", () => {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: true247+ it("never preserves non-numeric properties", () => {248+ const baseState = []249+ baseState.x = 7250+ const nextState = produce(baseState, s => {251+ s.push(3)252 })253- expect(s.length).toBe(1)254- expect(s[0] === "x").toBe(true)253+ expect("x" in nextState).toBeFalsy()254 })256- expect(nextState).toEqual(["x"])257- expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({258- configurable: !autoFreeze,259- enumerable: true,260- value: "x",261- writable: !autoFreeze262- })255+256+ if (useProxies) {257+ it("throws when a non-numeric property is added", () => {258+ expect(() => {259+ produce([], d => {260+ d.x = 3261+ })262+ }).toThrow(/does not support/)263+ })264+265+ it("throws when a non-numeric property is deleted", () => {266+ expect(() => {267+ const baseState = []268+ baseState.x = 7269+ produce(baseState, d => {270+ delete d.x271+ })272+ }).toThrow(/does not support/)273+ })274+ }275 })276 277 it("can rename nested objects (no changes)", () => {266- const nextState = produce(baseState, s => {267- const obj = s.anObject268- delete s.anObject269- s.renamed = obj278+ const nextState = produce({obj: {}}, s => {279+ s.foo = s.obj280+ delete s.obj281 })271- expect(nextState).not.toBe(baseState)272- expect(nextState.anArray).toBe(baseState.anArray)273- expect(nextState.renamed.nested).toBe(baseState.anObject.nested)274- expect(enumerableOnly(nextState)).toEqual({275- anArray: [3, 2, {c: 3}, 1],276- aProp: "hi",277- renamed: {278- nested: {279- yummie: true280- },281- coffee: false282- }283- })282+ expect(nextState).toEqual({foo: {}})283 })284 285 // Very similar to the test before, but the reused object has one286 // property changed, one added, and one removed.287 it("can rename nested objects (with changes)", () => {289- const nextState = produce(baseState, s => {290- const obj = s.anObject291- delete s.anObject288+ const nextState = produce({obj: {a: 1, b: 1}}, s => {289+ s.obj.a = true // change290+ delete s.obj.b // delete291+ s.obj.c = true // add292 293- obj.coffee = true // change294- obj.nested.yummy = true // add295- delete obj.nested.yummie // delete296-297- s.renamed = obj293+ s.foo = s.obj294+ delete s.obj295 })299- expect(nextState).not.toBe(baseState)300- expect(nextState.anArray).toBe(baseState.anArray)301- expect(nextState.renamed.nested).not.toBe(baseState.anObject.nested)302- expect(enumerableOnly(nextState)).toEqual({303- anArray: [3, 2, {c: 3}, 1],304- aProp: "hi",305- renamed: {306- nested: {307- yummy: true308- },309- coffee: true310- }311- })296+ expect(nextState).toEqual({foo: {a: true, c: true}})297 })298 299 it("can nest a draft in a new object (no changes)", () => {300+ const baseState = {obj: {}}301 const nextState = produce(baseState, s => {316- s.foo = {bar: s.anObject}317- delete s.anObject302+ s.foo = {bar: s.obj}303+ delete s.obj304 })319- expect(nextState).not.toBe(baseState)320- expect(nextState.foo.bar).toBe(baseState.anObject)305+ expect(nextState.foo.bar).toBe(baseState.obj)306 })307 323- it("can nest a draft in a new object (with changes)", () => {324- const nextState = produce(baseState, s => {325- const obj = s.anObject326- delete s.anObject308+ it("can nest a modified draft in a new object", () => {309+ const nextState = produce({obj: {a: 1, b: 1}}, s => {310+ s.obj.a = true // change311+ delete s.obj.b // delete312+ s.obj.c = true // add313 328- obj.coffee = true // change329- obj.nested.yummy = true // add330- delete obj.nested.yummie // delete331-332- s.foo = {bar: obj}314+ s.foo = {bar: s.obj}315+ delete s.obj316 })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- })317+ expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})318 })319 320 it("supports assigning undefined to an existing property", () => {321 const nextState = produce(baseState, s => {96 unmodified lines418 expect(draft.stuffz).toBe("coffee")419 })420 })421 446- it("should be able to get property descriptors from objects", () => {447- produce({a: 1}, draft => {448- expect("a" in draft).toBe(true)449- expect("b" in draft).toBe(false)450- expect(451- Reflect.ownKeys(draft).filter(x => typeof x === "string")452- ).toEqual(["a"])453-454- expect(455- Object.getOwnPropertyDescriptor(draft, "a")456- ).toMatchObject({457- configurable: true,458- enumerable: true459- })460- draft.a = 2461- expect(462- Object.getOwnPropertyDescriptor(draft, "a")463- ).toMatchObject({464- configurable: true,465- enumerable: true466- })467- expect(468- Object.getOwnPropertyDescriptor(draft, "b")469- ).toBeUndefined()470- draft.b = 2471- expect(472- Object.getOwnPropertyDescriptor(draft, "b")473- ).toMatchObject({474- configurable: true,475- enumerable: true476- })477- expect("a" in draft).toBe(true)478- expect("b" in draft).toBe(true)479- expect(480- Reflect.ownKeys(draft).filter(x => typeof x === "string")481- ).toEqual(["a", "b"])482- })483- })484-485- it("should be able to get property descriptors from arrays", () => {486- produce([1], draft => {487- expect(0 in draft).toBe(true)488- expect(1 in draft).toBe(false)489- expect("0" in draft).toBe(true)490- expect("1" in draft).toBe(false)491- expect(length in draft).toBe(true)492- expect(493- Reflect.ownKeys(draft).filter(x => typeof x === "string")494- ).toEqual(["0", "length"])495-496- expect(497- Object.getOwnPropertyDescriptor(draft, "length")498- ).toMatchObject({499- configurable: false,500- enumerable: false501- })502- draft[0] = 2503- expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(504- {505- configurable: true,506- enumerable: true507- }508- )509- expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(510- {511- configurable: true,512- enumerable: true513- }514- )515- expect(516- Object.getOwnPropertyDescriptor(draft, 1)517- ).toBeUndefined()518- draft[1] = 2519- expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(520- {521- configurable: true,522- enumerable: true523- }524- )525- expect(526- Reflect.ownKeys(draft).filter(x => typeof x === "string")527- ).toEqual(["0", "1", "length"])528- })529- })530-531- if (useProxies === true) {532- it("should not be possible to set property descriptors", () => {422+ if (useProxies)423+ it("throws when Object.defineProperty() is used on drafts", () => {424 expect(() => {425 produce({}, draft => {426 Object.defineProperty(draft, "xx", {427 enumerable: true,428 writeable: true,429 value: 2430 })431 })541- }).toThrowError(/not support/)432+ }).toThrow(/does not support/)433 })434 544- it("should not be possible to add properties to arrays", () => {545- expect(() => {546- produce([], d => {547- d.x = 3548- })549- }).toThrow(550- "Immer does not support setting non-numeric properties on arrays"551- )552- })553-554- it("should not be possible to remove properties from arrays", () => {555- expect(() => {556- const base = []557- base.x = 7558- produce(base, d => {559- delete d.x560- })561- }).toThrow(562- "Immer does not support deleting properties from arrays"563- )564- })565- }566-567- it("non-numeric array properties will be lost", () => {568- const base = []569- base.x = 7570- const next = produce(base, d => {571- d.push(3)572- })573- expect(next.x).toBe(undefined)574- })575-576- it("should not throw error, see #53 - 1", () => {577- const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}578- const result = produce(base, draft => {579- draft.arr = draft.arr.filter(item => item.count > 2)580- })581- expect(result.arr[0].count).toEqual(3)582- expect(result).toEqual({583- arr: [{count: 3}]584- })585- expect(result.arr[0]).toBe(base.arr[2])586- })587-588- it("should not throw error, see #53 - 2", () => {589- const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}590- const result = produce(base, draft => {591- draft.newArr = draft.arr.filter(item => item.count > 2)592- })593- expect(result.newArr[0].count).toEqual(3)594- expect(result.arr).toBe(base.arr)595- expect(result).toEqual({596- arr: [597- {598- count: 1599- },600- {601- count: 2602- },603- {604- count: 3605- }606- ],607- newArr: [608- {609- count: 3610- }611- ]612- })613- expect(result.newArr[0]).toBe(base.arr[2])614- expect(result.arr[2]).toBe(base.arr[2])615- })616-617- it("should not throw error, see #53 - 3", () => {618- const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}619- const result = produce(base, draft => {620- draft.newArr = draft.arr.filter(item => item.count > 2)621- delete draft.arr622- })623- expect(result.newArr[0].count).toEqual(3)624- expect(result).toEqual({625- newArr: [{count: 3}]626- })627- expect(result.newArr[0]).toBe(base.arr[2])628- })629-630- it("should not throw error, see #53 - 4", () => {631- const base = {bear: {age: 10}}632- const result = produce(base, draft => {633- draft.bear.legs = 4634- draft.room = {elephant: {kiddo: draft.bear}}635- })636- expect(result).toEqual({637- bear: {age: 10, legs: 4},638- room: {elephant: {kiddo: {age: 10, legs: 4}}}639- })640-641- const result2 = produce(result, draft => {642- draft.bear.age = 11643- draft.room.elephant.kiddo.legs = 5644- })645- expect(result2).toEqual({646- bear: {age: 11, legs: 4},647- room: {elephant: {kiddo: {age: 10, legs: 5}}}648- })649- })650-435 it("should handle constructor correctly", () => {652- const base = {436+ const baseState = {437 arr: new Array(),438 obj: new Object()439 }656- const result = produce(base, draft => {440+ const result = produce(baseState, draft => {441 draft.arrConstructed = draft.arr.constructor(1)442 draft.objConstructed = draft.obj.constructor(1)443 })444 expect(result.arrConstructed).toEqual(new Array().constructor(1))445 expect(result.objConstructed).toEqual(new Object().constructor(1))446 })447 664- it("should handle dates correctly", () => {665- const data = {date: new Date()}666- const next = produce(data, draft => {667- draft.x = true668- })669- expect(next).toEqual({x: true, date: data.date})670- expect(next.date).toBe(data.date)671- const next2 = produce(next, draft => {672- draft.date.setYear(2015)673- })674- // This still holds; because produce won't proxy Date objects675- // and the original is actually modified!676- expect(next2).toEqual({x: true, date: data.date})677- expect(next2.date).toBe(next.date)678- expect(next2.date).toBe(data.date)679- expect(next2).toBe(next)680- })681-448 it("should handle equality correctly - 1", () => {683- const base = {449+ const baseState = {450 y: 3 / 0,451 z: NaN452 }687- const next = produce(base, draft => {453+ const nextState = produce(baseState, draft => {454 draft.y = 4 / 0455 draft.z = NaN456 })691- expect(next).toEqual(base)692- expect(next).toBe(base)457+ expect(nextState).toBe(baseState)458 })459 460 it("should handle equality correctly - 2", () => {696- const base = {461+ const baseState = {462 x: -0463 }699- const next = produce(base, draft => {700- draft.x = +1464+ const nextState = produce(baseState, draft => {465+ draft.x = +0466 })702- expect(next).not.toEqual(base)703- expect(next).not.toBe(base)467+ expect(nextState).not.toBe(baseState)468+ expect(nextState).not.toEqual(baseState)469 })470 471 // AKA: recursive produce calls707- describe("a nested producer", () => {472+ describe("nested producers", () => {473 describe("when base state is not a draft", () => {474 // This test ensures the global state used to manage proxies is475 // never left in a corrupted state by a nested `produce` call.476 it("never affects its parent producer implicitly", () => {122 unmodified lines599 expect(result.s1).toBe(base)600 })601 })602 838- it("should not try to change immutable data, see #66", () => {839- const user = require("./test-data")840-841- const base = {}842- const next = produce(base, draft => {843- draft.user = user844- })845- expect(next.user).toBe(user)846- expect(next).not.toBe(base)847- expect(next.user).toEqual(user)848- })849-850- it("should not try to change immutable data, see #66 - 2", () => {851- const user = deepFreeze(cloneDeep(require("./test-data")))852-853- const base = {}854- const next = produce(base, draft => {855- draft.user = user856- })857- expect(next.user).toBe(user)858- expect(next).not.toBe(base)859- expect(next.user).toEqual(user)860- })861-862- it("should structurally share identical objects in the tree", () => {863- const base = {bear: {legs: 4}, eagle: {legs: 3}}864- const next = produce(base, draft => {865- const animal = draft.bear866- animal.legs = animal.legs + 1867- draft.bear = animal868- draft.eagle = animal869- draft.cow = animal870- draft.kiddo = animal871- })872- expect(next).toEqual({873- bear: {legs: 5},874- eagle: {legs: 5},875- cow: {legs: 5},876- kiddo: {legs: 5}877- })878- expect(next.bear).toBe(next.cow)879- expect(next.kiddo).toBe(next.cow)880- })881-603 if (useProxies)883- it("should not allow changing prototype", () => {604+ it("throws when Object.setPrototypeOf() is used on a draft", () => {605 produce({}, draft => {606 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(886- /does not support `setPrototype/607+ /does not support/608 )609 })610 })611 891- it("'in' should work", () => {892- produce(createBaseState(), draft => {612+ it("supports the 'in' operator", () => {613+ produce(baseState, draft => {614+ // Known property615 expect("anArray" in draft).toBe(true)616 expect(Reflect.has(draft, "anArray")).toBe(true)617 618+ // Unknown property619 expect("bla" in draft).toBe(false)620 expect(Reflect.has(draft, "bla")).toBe(false)621 622+ // Known index623 expect(0 in draft.anArray).toBe(true)624 expect("0" in draft.anArray).toBe(true)625 expect(Reflect.has(draft.anArray, 0)).toBe(true)626 expect(Reflect.has(draft.anArray, "0")).toBe(true)627 628+ // Unknown index629 expect(17 in draft.anArray).toBe(false)630 expect("17" in draft.anArray).toBe(false)631 expect(Reflect.has(draft.anArray, 17)).toBe(false)632 expect(Reflect.has(draft.anArray, "17")).toBe(false)8 unmodified lines641 expect(next1).not.toBe(base)642 expect(next1.x).toBe(4)643 })644 920- it("'this' should work - 1", () => {645+ it("'this' should work - 2", () => {646 const base = {x: 3}647 const incrementor = produce(function() {648 this.x = 4649 })2 unmodified lines652 expect(next1.x).toBe(4)653 })654 655 // See here: https://github.com/mweststrate/immer/issues/89931- it("works with the spread operator", () => {656+ it("supports the spread operator", () => {657 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}658 const result = produce(base, draft => {659 draft.foo = {x: 1, ...draft.foo, y: 1}660 draft.bar = [1, ...draft.bar, 1]22 unmodified lines683 expect(base[0].a).toEqual(1)684 expect(result[0].a).toEqual(2)685 })686 962- it("can produce from no state", () => {963- expect(964- produce(3, draft => {965- expect(draft).toBe(3)966- return 5687+ describe("recipe functions", () => {688+ it("can return a new object", () => {689+ const base = {x: 3}690+ const res = produce(base, draft => {691+ return {x: draft.x + 1}692 })968- ).toBe(5)969- })970-971- it("can return something new ", () => {972- const base = {x: 3}973- const res = produce(base, draft => {974- return {x: draft.x + 1}693+ expect(res).not.toBe(base)694+ expect(res).toEqual({x: 4})695 })976- expect(res).not.toBe(base)977- expect(res).toEqual({x: 4})978- })696 980- it("can return the draft new ", () => {981- const base = {x: 3}982- const res = produce(base, draft => {983- draft.x = 4984- return draft697+ it("can return the draft", () => {698+ const base = {x: 3}699+ const res = produce(base, draft => {700+ draft.x = 4701+ return draft702+ })703+ expect(res).not.toBe(base)704+ expect(res).toEqual({x: 4})705 })986- expect(res).not.toBe(base)987- expect(res).toEqual({x: 4})706 })707 990- it("should throw if modifying the draft and returning something new", () => {708+ it("throws when the draft is modified and another object is returned", () => {709 const base = {x: 3}710 expect(() => {711 produce(base, draft => {712 draft.x = 41 unmodified line714 })715 }).toThrow(/An immer producer returned a new value/)716 })717 1000- it("should fix #117", () => {718+ it("should fix #117 - 1", () => {719 const reducer = (state, action) =>720 produce(state, draft => {721 switch (action.type) {722 case "SET_STARTING_DOTS":38 unmodified lines761 const next = reducer(base, {type: "SET_STARTING_DOTS"})762 expect(next).toEqual({dots: base.availableStartingDots})763 })764 1047- it("should return an unmodified primitive baseState (#148)", () => {1048- const baseState = "some string"1049- const nextState = produce(baseState, () => {1050- /* no modification */1051- })1052- expect(nextState).toBe(baseState)1053- })1054-1055- it("should return an unmodified null baseState (#148)", () => {1056- const baseState = null1057- const nextState = produce(baseState, () => {1058- /* no modification */1059- })1060- expect(nextState).toBe(baseState)1061- })1062-1063- it("should not detect noop assignments - 0", () => {765+ it("cannot always detect noop assignments - 0", () => {766 const baseState = {x: {y: 3}}767 const nextState = produce(baseState, d => {768 const a = d.x769 d.x = a770 })771 expect(nextState).toBe(baseState)772 })773 1072- it("should not detect noop assignments - 1", () => {774+ it("cannot always detect noop assignments - 1", () => {775 const baseState = {x: {y: 3}}776 const nextState = produce(baseState, d => {777 const a = d.x778 d.x = 44 unmodified lines783 // so we don't atm784 expect(nextState).not.toBe(baseState)785 })786 1085- it("should not detect noop assignments - 2", () => {787+ it("cannot always detect noop assignments - 2", () => {788 const baseState = {x: {y: 3}}789 const nextState = produce(baseState, d => {790 const a = d.x791 const stuff = a.y + 35 unmodified lines797 // so we don't atm798 expect(nextState).not.toBe(baseState)799 })800 1099- it("should not detect noop assignments - 3", () => {801+ it("cannot always detect noop assignments - 3", () => {802 const baseState = {x: 3}803 const nextState = produce(baseState, d => {804 d.x = 3805 })806 expect(nextState).toBe(baseState)807 })808 1107- it("should not detect noop assignments - 4", () => {809+ it("cannot always detect noop assignments - 4", () => {810 const baseState = {x: 3}811 const nextState = produce(baseState, d => {812 d.x = 4813 d.x = 33 unmodified lines817 // so we don't atm818 expect(nextState).not.toBe(baseState)819 })820 1119- it("immer should have no dependencies", () => {1120- expect(require("../package.json").dependencies).toEqual(undefined)1121- })1122-1123- it("#174", () => {1124- const nextState = produce([1, 2, 3], s => {1125- s.pop()1126- s.push(100)1127- })1128- expect(nextState).toEqual([1, 2, 100])1129- })1130-1131- it("#195 should be able to find items", () => {1132- const state = {1133- items: [1134- {1135- id: 0,1136- task: "drink milk"1137- },1138- {id: 1, task: "eat cookie"}1139- ]1140- }1141- produce(state, draft => {1142- expect(draft.items.find(({id}) => id === 1).task).toBe(1143- "eat cookie"1144- )1145- })1146- })1147-1148- it("allows a function as the base state", () => {1149- let fn = () => {}1150- expect(1151- produce(fn, draft => {1152- expect(fn).toBe(draft)1153- })1154- ).toBe(fn)1155- })1156-1157- it("cannot return and produce undefined!", () => {821+ it("cannot produce undefined by returning undefined", () => {822 const base = 3823 expect(produce(base, () => 4)).toBe(4)824 expect(produce(base, () => null)).toBe(null)825 expect(produce(base, () => undefined)).toBe(3)8 unmodified lines834 expect(produce(() => nothing)({})).toBe(undefined)835 expect(produce(() => nothing)(3)).toBe(undefined)836 })837 838+ describe("base state type", () => {1174- testBaseStateTypes(produce)839+ testObjectTypes(produce)840+ testLiteralTypes(produce)841+ })842 843 afterEach(() => {844 expect(baseState).toBe(origBaseState)845 expect(baseState).toEqual(createBaseState())846 })847 848+ class Foo {}849 function createBaseState() {850 const data = {851+ anInstance: new Foo(),852 anArray: [3, 2, {c: 3}, 1],853 aProp: "hi",854 anObject: {855 nested: {47 unmodified lines903 })904 })905 }906 1238-function testBaseStateTypes(produce) {907+function testObjectTypes(produce) {908+ const values = {909+ "empty object": {},910+ "plain object": {a: 1, b: 2},911+ "object (no prototype)": Object.create(null),912+ "empty array": [],913+ "plain array": [1, 2]914+ }915+ for (const name in values) {916+ const value = values[name]917+ const copy = shallowCopy(value)918+ testObjectType(name, value)919+ testObjectType(name + " (frozen)", Object.freeze(copy))920+ }921+ function testObjectType(name, base) {922+ describe(name, () => {923+ it("creates a draft", () => {924+ produce(base, draft => {925+ expect(draft).not.toBe(base)926+ expect(shallowCopy(draft, true)).toEqual(base)927+ })928+ })929+930+ it("preserves the prototype", () => {931+ const proto = Object.getPrototypeOf(base)932+ produce(base, draft => {933+ expect(Object.getPrototypeOf(draft)).toBe(proto)934+ })935+ })936+937+ it("returns the base state when no changes are made", () => {938+ expect(produce(base, () => {})).toBe(base)939+ })940+941+ it("returns a copy when changes are made", () => {942+ const random = Math.random()943+ const result = produce(base, draft => {944+ draft[0] = random945+ })946+ expect(result).not.toBe(base)947+ expect(result.constructor).toBe(base.constructor)948+ expect(result[0]).toBe(random)949+ })950+ })951+ }952+}953+954+function testLiteralTypes(produce) {955 class Foo {}1240- const primitives = {956+ const values = {957 "falsy number": 0,958 "truthy number": 1,959 "negative number": -1,960+ NaN: NaN,961 infinity: 1 / 0,962 true: true,963 false: false,964 "empty string": "",1 unmodified line966 null: null,967 undefined: undefined,968 969 /**1253- * These objects are treated as primitives because Immer1254- * chooses not to make drafts for them.970+ * These objects are treated as literals because Immer971+ * does not know how to draft them.972 */973+ function: () => {},974 "regexp object": /.+/g,975 "boxed number": new Number(0),976 "boxed string": new String(""),977 "boxed boolean": new Boolean(),978 "date object": new Date(),979 "class instance": new Foo()980 }1263- for (const name in primitives) {1264- describe("base state type - " + name, () => {1265- const value = primitives[name]981+ for (const name in values) {982+ describe(name, () => {983+ const value = values[name]984+985 it("does not create a draft", () => {986 produce(value, draft => {987 expect(draft).toBe(value)988 })989 })1271- it("returns the same value when the producer returns undefined", () => {990+991+ it("returns the base state when no changes are made", () => {992 expect(produce(value, () => {})).toBe(value)993 })994+995 if (value && typeof value == "object") {1275- it("does not return a copy when the producer makes changes", () => {996+ it("does not return a copy when changes are made", () => {997 expect(998 produce(value, draft => {999 draft.foo = true1000 })1 unmodified line1002 })1003 }1004 })1005 }1285- const objects = {1286- "empty object": {},1287- "plain object": {a: 1, b: {c: 1}},1288- "frozen object": Object.freeze({}),1289- "null-prototype object": Object.create(null),1290- "frozen null-prototype object": Object.freeze(Object.create(null)),1291- "empty array": [],1292- "plain array": [1, [2, [3, []]]],1293- "frozen array": Object.freeze([])1294- }1295- for (const name in objects) {1296- describe("base state type - " + name, () => {1297- const value = objects[name]1298- it("creates a draft", () => {1299- produce(value, draft => {1300- expect(draft).not.toBe(value)1301- expect(enumerableOnly(draft)).toEqual(value)1302- })1303- })1304- it("returns the same value when the producer does nothing", () => {1305- expect(produce(value, () => {})).toBe(value)1306- })1307- it("returns a copy when changes are made", () => {1308- const random = Math.random()1309- const result = produce(value, draft => {1310- draft[0] = random1311- })1312- expect(result).not.toBe(value)1313- expect(result.constructor).toBe(value.constructor)1314- expect(result[0]).toBe(random)1315- })1316- })1317- }1006 }1007 1008 function enumerableOnly(x) {1009 const copy = shallowCopy(x)8 unmodified lines