Add additional tests for array method cases
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mark Erikson · committed by Mark Erikson · parent 5945e47 · Browse files · 2 files·+54 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+54 added−7 removed
- tests:+54 added−7 removed
Provenance
- Commit
0f158b3- Tree
c5db59a- Author
Mark Erikson <mark@isquaredsoftware.com>- Committer
Mark Erikson <mark@isquaredsoftware.com>- Parent
5945e47
2 files changed+54 added−7 removed
modified:__tests__/base.js+31 added-5 removed
58 unmodified lines59 useStrictShallowCopy,60 useListener,61 useArrayMethods = false62 ) {63- const listener = useListener ? function () {} : undefined63+ const listener = useListener ? function() {} : undefined64 65 const {produce, produceWithPatches} = createPatchedImmer({66 autoFreeze,67 useStrictShallowCopy4 unmodified lines72 function createPatchedImmer(options) {73 const immer = new Immer(options)74 75 const {produce} = immer76- immer.produce = function (...args) {76+ immer.produce = function(...args) {77 return typeof args[1] === "function" && args.length < 378 ? produce(...args, listener)79 : produce(...args)80 }246 unmodified lines327 d.shifted.shift()328 })329 expect(next).toBe(base)330 })331+332+ it("no-op push() on an array inside a Set preserves the array's identity", () => {333+ const arr = [1, 2, 3]334+ const base = new Set([arr, {x: 1}])335+ const next = produce(base, d => {336+ for (const v of d) {337+ if (Array.isArray(v)) v.push(...[])338+ else v.x = 2339+ }340+ })341+ expect(next).not.toBe(base)342+ expect([...next].find(Array.isArray)).toBe(arr)343+ })344+345+ it("no-op splice() on an array inside a Set preserves the array's identity", () => {346+ const arr = [1, 2, 3]347+ const base = new Set([arr, {x: 1}])348+ const next = produce(base, d => {349+ for (const v of d) {350+ if (Array.isArray(v)) v.splice(1, 0)351+ else v.x = 2352+ }353+ })354+ expect(next).not.toBe(base)355+ expect([...next].find(Array.isArray)).toBe(arr)356+ })357 })358 359 // Reported here: https://github.com/mweststrate/immer/issues/116360 it("can pop then push", () => {2931 unmodified lines3292 })3293 3294 it("'this' should not be bound anymore - 1", () => {3295 const base = {x: 3}3270- const next1 = produce(base, function () {3296+ const next1 = produce(base, function() {3297 expect(this).toBe(undefined)3298 })3299 })3300 3301 it("'this' should not be bound anymore - 2", () => {3276- const incrementor = produce(function () {3302+ const incrementor = produce(function() {3303 expect(this).toBe(undefined)3304 })3305 incrementor()3306 })3307 3308 it("should be possible to use dynamic bound this", () => {3309 const world = {3310 counter: {count: 1},3285- inc: produce(function (draft) {3311+ inc: produce(function(draft) {3312 expect(this).toBe(world)3313 draft.counter.count = this.counter.count + 13314 })3315 }978 unmodified linesmodified:__tests__/map-set.js+23 added-2 removed
21 unmodified lines22 runBaseTest("proxy (autofreeze)", true, true)23 runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)24 25 function runBaseTest(name, autoFreeze, useListener) {26- const listener = useListener ? function () {} : undefined26+ const listener = useListener ? function() {} : undefined27 const {produce, produceWithPatches} = createPatchedImmer({28 autoFreeze29 })30 2 unmodified lines33 function createPatchedImmer(options) {34 const immer = new Immer(options)35 36 const {produce} = immer37- immer.produce = function (...args) {37+ immer.produce = function(...args) {38 return typeof args[1] === "function" && args.length < 339 ? produce(...args, listener)40 : produce(...args)41 }352 unmodified lines394 })395 396 expect(Array.from(newSet)).toEqual([objs[0], objs[1]])397 })398+399+ test("reading (but not modifying) a nested object inside a Set does not leak the copy", () => {400+ const untouched = {nested: {a: 1}}401+ const base = new Set([untouched, {x: 1}])402+ const next = produce(base, draft => {403+ for (const v of draft) {404+ if (v.nested) {405+ // Read only - creates a child draft (and a stray copy_406+ // on the parent) but modifies nothing407+ expect(v.nested.a).toBe(1)408+ } else {409+ v.x = 2410+ }411+ }412+ })413+ expect(next).not.toBe(base)414+ const finalUntouched = Array.from(next).find(v => v.nested)415+ expect(finalUntouched).toBe(untouched)416+ expect(finalUntouched.nested.a).toBe(1)417+ expect(isDraft(finalUntouched.nested)).toBe(false)418+ })419 })420 }421 422 // Bug reproduction: DraftSet leakage when placed in new object and set into Map113 unmodified lines