diff --git a/package.json b/package.json index 6cbf0f20..e4b1fdea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mutative", - "version": "0.7.0", + "version": "0.7.1", "description": "A JavaScript library for efficient immutable updates", "main": "dist/index.js", "module": "dist/mutative.esm.js", diff --git a/src/draft.ts b/src/draft.ts index 057be616..a0e3e9d6 100644 --- a/src/draft.ts +++ b/src/draft.ts @@ -260,22 +260,6 @@ export function createDraft(createDraftOptions: { target.options.updatedValues ?? new WeakMap(); target.options.updatedValues.set(updatedValue, proxyDraft.original); } - if ( - proxyDraft.parent!.key !== undefined && - proxyDraft.parent!.parent!.proxy - ) { - const parent = - proxyDraft.parent!.parent!.proxy[proxyDraft.parent!.key]; - const parentProxyDraft = getProxyDraft(parent); - if (parentProxyDraft === undefined) { - // !case: handle assigning a non-draft with the same key - copy = parent; - const current = get(copy, key!); - if (!getProxyDraft(current)) { - updatedValue = current; - } - } - } // final update value set(copy, key!, updatedValue); } diff --git a/test/index.test.ts b/test/index.test.ts index 81014398..25d9505e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -3297,3 +3297,65 @@ test('#18: assigning a non-draft with the different key - enablePatches', () => expect(apply(baseState, created[1])).toEqual(created[0]); expect(apply(created[0], created[2])).toEqual(baseState); }); + +test('array: assigning a non-draft array', () => { + const baseState = { + data: [{ a: true }], + }; + + const state = create(baseState, (draft) => { + const a = draft.data.filter((item) => item.a); + // @ts-ignore + draft.data = []; + }); + + expect(state.data.length).toBe(0); +}); + +test('object: assigning a non-draft array', () => { + const baseState = { + data: { a: { b: true } }, + }; + + const state = create(baseState, (draft) => { + Object.values(draft.data).forEach((item) => { + item.b = false; + }); + // @ts-ignore + draft.data = {}; + }); + + expect(Object.values(state.data).length).toBe(0); +}); + +test('set: assigning a non-draft array', () => { + const baseState = { + data: new Set([{ b: true }]), + }; + + const state = create(baseState, (draft) => { + draft.data.forEach((item) => { + // + }); + // @ts-ignore + draft.data = new Set(); + }); + + expect(state.data.size).toBe(0); +}); + +test('map: assigning a non-draft array', () => { + const baseState = { + data: new Map([[0, { b: true }]]), + }; + + const state = create(baseState, (draft) => { + draft.data.forEach((item) => { + // + }); + // @ts-ignore + draft.data = new Map(); + }); + + expect(state.data.size).toBe(0); +});