From 04c9ece0badbd6b310b28f4f3e0db56b4abf26f6 Mon Sep 17 00:00:00 2001 From: unadlib Date: Fri, 21 Jun 2024 02:59:14 +0800 Subject: [PATCH] test(case): add test case about frozen --- test/immer-non-support.test.ts | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test/immer-non-support.test.ts b/test/immer-non-support.test.ts index 199eed37..0f724e0f 100644 --- a/test/immer-non-support.test.ts +++ b/test/immer-non-support.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-inner-declarations */ /* eslint-disable symbol-description */ /* eslint-disable no-unused-expressions */ /* eslint-disable @typescript-eslint/ban-ts-comment */ @@ -503,3 +504,65 @@ test('error key setting in array', () => { } } }); + +test(`Object values of a Map are not frozen anymore #1119`, () => { + { + enableMapSet(); + + interface Fruit { + key: string; + name: string; + } + + const fruits: Fruit[] = [ + { key: 'apple1', name: 'Red Delicious' }, + { key: 'apple2', name: 'Gala' }, + ]; + + let products = new Map(); + + function setFruitMap(fruits: Fruit[]): void { + products = produce(products, (draft) => { + draft.clear(); + fruits.forEach((fruit) => draft.set(fruit.key, fruit)); + }); + } + + setFruitMap(fruits); + + const product = products.get('apple1'); + // ! it should be frozen + expect(Object.isFrozen(product)).not.toBeTruthy(); + } + { + interface Fruit { + key: string; + name: string; + } + + const fruits: Fruit[] = [ + { key: 'apple1', name: 'Red Delicious' }, + { key: 'apple2', name: 'Gala' }, + ]; + + let products: Immutable> = new Map(); + + function setFruitMap(fruits: Fruit[]): void { + products = create( + products, + (draft) => { + draft.clear(); + fruits.forEach((fruit) => draft.set(fruit.key, fruit)); + }, + { + enableAutoFreeze: true, + } + ); + } + + setFruitMap(fruits); + + const product = products.get('apple1'); + expect(Object.isFrozen(product)).toBeTruthy(); + } +});