-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathupdate.spec.ts
49 lines (42 loc) · 1.51 KB
/
update.spec.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
import { expect, test } from 'vitest';
import update from './update';
test('returns undefined by default', () => {
expect(update()(null, {})).toBeUndefined();
});
test('returns updated record when found', () => {
const data = [{ id: 1, value: 'foo', bar: 'baz' }];
expect(update(data)(null, { id: 1, value: 'bar' })).toEqual({
id: 1,
value: 'bar',
bar: 'baz',
});
});
test('returns undefined when not found', () => {
const data = [
{ id: 1, value: 'foo' },
{ id: 2, value: 'bar' },
];
expect(update(data)(null, { id: 3 })).toBeUndefined();
});
test('updates record when found', () => {
const data = [{ id: 1, value: 'foo' }];
update(data)(null, { id: 1, value: 'bar', bar: 'baz' });
expect(data).toEqual([{ id: 1, value: 'bar', bar: 'baz' }]);
});
test('updates record with string id', () => {
const data = [{ id: 'abc', value: 'foo' }];
update(data)(null, { id: 'abc', value: 'bar', bar: 'baz' });
expect(data).toEqual([{ id: 'abc', value: 'bar', bar: 'baz' }]);
});
test('removes property when setting the value to undefined', () => {
const data = [{ id: 1, value: 'foo' }];
update(data)(null, { id: 1, value: undefined });
expect(data).toEqual([{ id: 1 }]);
});
test("doesn't confuse undefined id with the id 'undefined'", () => {
const data = [{ value: 'foo' }];
expect(
update(data)(null, { id: 'undefined', value: 'bar', bar: 'baz' }),
).toBeUndefined();
expect(data).toEqual([{ value: 'foo' }]);
});