import { enableMapSet, produce } from "./mod.ts"; import { assertEquals, assertNotStrictEquals, assertStrictEquals, } from "https://deno.land/std@0.173.0/testing/asserts.ts"; enableMapSet(); Deno.test("Immer", async (t) => { const before = new Map([["key1", { title: "test" }]]); const after = new Map([["key1", { title: "test" }]]); // 構造は同じだが別の参照 assertEquals(before, after); assertNotStrictEquals(before, after); await t.step("objectを入れ替えるときはreturnする", () => { const next = produce(before, () => after); assertNotStrictEquals(before, next); assertStrictEquals(after, next); }); await t.step("何か破壊的操作があると、値が同じでも別のobjectになる", () => { { const next = produce(before, (draft) => { draft.clear(); for (const [key, value] of after) { draft.set(key, value); } }); assertNotStrictEquals(before, next); } { const next = produce(before, (draft) => { const value = draft.get("key1"); if (!value) return; draft.clear(); draft.set("key1", value); }); assertNotStrictEquals(before, next); } }); await t.step("変更がなければ同じobjectを返す", () => { const next = produce(before, (draft) => {}); assertStrictEquals(before, next); }); });