Skip to content

Commit cc98492

Browse files
committed
test: restore full coverage of apply() symbol copies and Set items added back
1 parent e687ece commit cc98492

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎test/apply.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1959,17 +1959,21 @@ describe('array methods on the draft copy', () => {
19591959
describe('patch values that apply() copies', () => {
19601960
test('own symbol keys are copied', () => {
19611961
const key = Symbol('key');
1962+
const flag = Symbol('flag');
1963+
const empty = Symbol('empty');
19621964
const base: any = { item: null };
19631965
const [state, patches] = create(
19641966
base,
19651967
(draft: any) => {
1966-
draft.item = { [key]: { deep: 1 }, x: 1 };
1968+
draft.item = { [key]: { deep: 1 }, [flag]: true, [empty]: null, x: 1 };
19671969
},
19681970
{ enablePatches: true }
19691971
);
19701972
const next = apply(base, patches);
19711973
expect(next.item[key]).toStrictEqual({ deep: 1 });
19721974
expect(next.item[key]).not.toBe(state.item[key]);
1975+
expect(next.item[flag]).toBe(true);
1976+
expect(next.item[empty]).toBeNull();
19731977
expect(next).toStrictEqual(state);
19741978
});
19751979

‎test/moved-child-patches.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ describe('patches of drafts that left their key', () => {
8282
]);
8383
});
8484

85+
test('a Set item added back after additions is carried by the add patch', () => {
86+
const a = { v: 1 };
87+
const b = { v: 2 };
88+
const base = new Set([a, b]);
89+
const [state, patches] = replay(base, (draft) => {
90+
draft.add({ v: 3 });
91+
const [first] = draft;
92+
expect(first.v).toBe(1);
93+
draft.delete(first);
94+
draft.add(a);
95+
// The item is drafted again at a position past the base Set's size.
96+
for (const item of draft) if (item.v === 1) item.v = 10;
97+
});
98+
expect([...state]).toStrictEqual([{ v: 2 }, { v: 3 }, { v: 10 }]);
99+
expect(patches).toStrictEqual([
100+
{ op: 'remove', path: [0], value: { v: 1 } },
101+
{ op: 'add', path: [1], value: { v: 3 } },
102+
{ op: 'add', path: [2], value: { v: 10 } },
103+
]);
104+
});
105+
85106
test('a changed nested draft whose parent moved away', () => {
86107
const base = { list: [{ inner: { v: 3 } }, 'gap', { inner: { v: 1 } }] };
87108
const [state] = replay(base, (draft) => {

0 commit comments

Comments
 (0)