-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editor.test.js
81 lines (69 loc) · 1.9 KB
/
Editor.test.js
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
71
72
73
74
75
76
77
78
79
80
81
/* globals describe, it, expect */
const {edit, edits, DELETED} = require('./Editor')
describe('editor', () => {
it('supports simple edits', () => {
const e = edit({ x: 5 })
e.x = 10
e.y = 20
expect(e.y).toEqual(20)
expect(Object.keys(e)).toEqual(['x', 'y'])
expect(e).toEqual({ x: 10, y: 20 })
})
it('leaves src untouched', () => {
const src = { x: 5 }
const e = edit(src)
e.x = 10
e.y = 20
expect(src).toEqual({ x: 5 })
})
it('supports deep edits', () => {
const src = { a: { b: { c: 1 } } }
const e = edit(src)
e.a.b.d = 2
expect(e.a.b.d).toEqual(2)
expect(e).toEqual({ a: { b: { c: 1, d: 2 } } })
// still leaves src unchanged
expect(src).toEqual({ a: { b: { c: 1 } } })
})
it('edits can extract edits from an edit object', () => {
const e = edit({})
expect(edits(e)).toBeUndefined()
e.a = 10
e.b = { c: 1 }
expect(edits(e)).toEqual({ a: 10, b: { c: 1 } })
})
it('computes the smallest edit possible', () => {
const e = edit({ a: { b: { c: 1 } } })
e.a.e = 2
expect(edits(e)).toEqual({ a: { e: 2 } })
})
it('exports DELETED Symbol', () => {
expect(DELETED).toBeDefined()
})
it('supports deletion', () => {
const src = {a: 1}
const e = edit(src)
delete e.a
expect(e.a).toBeUndefined()
expect(src.a).toEqual(1)
expect(Object.keys(edits(e))).toEqual(['a'])
})
it('sees deletion when edit is being done on an Edit object', () => {
const src = {a: 1}
const e1 = edit(src)
delete e1.a
const e2 = edit(e1)
expect(e2.a).toBeUndefined()
expect(src.a).toEqual(1)
})
it('deletion on deep object works', () => {
const src = {}
const e1 = edit(src)
e1.x = {a: 1}
delete e1.x.a
expect(e1.x.a).toBeUndefined()
expect(Object.keys(e1.x)).toEqual([])
expect(e1.x).toEqual({})
expect(edits(e1)).toEqual({x: {a: DELETED}})
})
})