-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathoriginal.test.ts
70 lines (65 loc) · 2.58 KB
/
original.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* eslint-disable no-param-reassign */
import { create, original, isDraft } from '../src';
describe('original', () => {
test('should return the original value', () => {
interface Item {
foo: string;
bar?: { foobar: string };
}
const value = create(
{
arr: [{ foo: 'bar' } as Item],
set: new Set<Item>([{ foo: 'bar' }]),
map: new Map<string, Item>([['foo', { foo: 'bar' }]]),
obj: { foo: 'bar' } as Item,
},
(draft) => {
draft.arr[0].foo = 'baz';
expect(isDraft(draft.arr[0])).toBe(true);
expect(original(draft.arr[0])).toEqual({ foo: 'bar' });
expect(() => original(draft.arr[0].bar!)).toThrowError();
// !new props
draft.arr[0].bar = { foobar: 'str' };
draft.arr[0].bar.foobar = 'baz';
expect(isDraft(draft.arr[0].bar)).toBe(false);
expect(() => original(draft.arr[0].bar)).toThrowError();
Array.from(draft.set.values())[0].foo = 'baz';
expect(isDraft(Array.from(draft.set.values())[0])).toBe(true);
expect(original(Array.from(draft.set.values())[0])).toEqual({
foo: 'bar',
});
// !new props
Array.from(draft.set.values())[0].bar = { foobar: 'str' };
Array.from(draft.set.values())[0].bar!.foobar = 'baz';
expect(isDraft(Array.from(draft.set.values())[0].bar)).toBe(false);
expect(() =>
original(Array.from(draft.set.values())[0].bar)
).toThrowError();
draft.map.get('foo')!.foo = 'baz';
expect(isDraft(draft.map.get('foo'))).toBe(true);
expect(original(draft.map.get('foo'))).toEqual({ foo: 'bar' });
// !new props
draft.map.get('foo')!.bar = { foobar: 'str' };
draft.map.get('foo')!.bar!.foobar = 'baz';
expect(isDraft(draft.map.get('foo')!.bar)).toBe(false);
expect(() => original(draft.map.get('foo')!.bar)).toThrowError();
draft.obj.foo = 'baz';
expect(isDraft(draft.obj)).toBe(true);
expect(original(draft.obj)).toEqual({ foo: 'bar' });
// !new props
draft.obj.bar = { foobar: 'str' };
draft.obj.bar!.foobar = 'baz';
expect(isDraft(draft.obj.bar)).toBe(false);
expect(() => original(draft.obj.bar)).toThrowError();
}
);
});
test('should return undefined for an object that is not proxied', () => {
expect(() => original({})).toThrowError(
`original() is only used for a draft, parameter: [object Object]`
);
expect(() => original(3)).toThrowError(
`original() is only used for a draft, parameter: 3`
);
});
});