-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
139 lines (128 loc) · 3.62 KB
/
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict'
const test = require('tape')
const enhance = require('./index')
test('throws without object', (t) => {
t.throws(() => enhance(''), /expecting value to be plain object/i)
t.end()
})
test('returns an object', (t) => {
t.equal(typeof enhance({}), 'object')
t.end()
})
test('enhances object', (t) => {
const enhanced = enhance({})
t.equal(typeof enhanced.$$history, 'function')
t.equal(typeof enhanced.$$undo, 'function')
t.equal(typeof enhanced.$$redo, 'function')
t.equal(typeof enhanced.$$index, 'number')
t.end()
})
test('preserves original properties', (t) => {
const enhanced = enhance({ lazarus: '' })
t.equal(enhanced.lazarus, '')
t.end()
})
test('records successive changes', (t) => {
const enhanced = enhance({})
enhanced.spongebob = 'squarepants'
enhanced.curb = { your: 'enthusiasm' }
const history = enhanced.$$history()
t.deepEqual(history[0], {})
t.deepEqual(history[1], { spongebob: 'squarepants' })
t.deepEqual(history[2], {
spongebob: 'squarepants',
curb: { your: 'enthusiasm' }
})
t.end()
})
test('can undo, redo a change', (t) => {
const enhanced = enhance({ value: 'Hello' })
enhanced.value = 'Hello Sam'
t.equal(enhanced.value, 'Hello Sam')
const history = enhanced.$$history(1)
t.deepEqual(history[0], { value: 'Hello' })
t.deepEqual(history[1], { value: 'Hello Sam' })
enhanced.$$undo()
t.equal(enhanced.value, 'Hello')
enhanced.$$redo()
t.equal(enhanced.value, 'Hello Sam')
t.end()
})
test('can redo multiple changes', (t) => {
const enhanced = enhance({ value: 'Hello' })
enhanced.value = 'Hello Sam'
enhanced.value = 'Hello David'
enhanced.value = 'Hello Ahmed'
enhanced.$$undo(3)
t.equal(enhanced.value, 'Hello')
enhanced.$$redo(3)
t.equal(enhanced.value, 'Hello Ahmed')
t.end()
})
test('forgets forward history with new changes after undo', (t) => {
const enhanced = enhance({ value: 'Hello' })
enhanced.value = 'Hello Sam'
enhanced.value = 'Hello David'
enhanced.value = 'Hello Ahmed'
t.equal(enhanced.$$index, 3)
t.deepEqual(enhanced.$$history(), [
{ value: 'Hello' },
{ value: 'Hello Sam' },
{ value: 'Hello David' },
{ value: 'Hello Ahmed' }
])
enhanced.$$undo()
enhanced.$$undo()
t.equal(enhanced.$$index, 1)
t.equal(enhanced.$$history().length, 4)
t.equal(enhanced.value, 'Hello Sam')
t.deepEqual(enhanced.$$history(), [
{ value: 'Hello' },
{ value: 'Hello Sam' },
{ value: 'Hello David' },
{ value: 'Hello Ahmed' }
])
enhanced.value = 'Hello Adam'
t.equal(enhanced.$$index, 2)
t.equal(enhanced.$$history().length, 3)
t.deepEqual(enhanced.$$history(), [
{ value: 'Hello' },
{ value: 'Hello Sam' },
{ value: 'Hello Adam' }
])
t.end()
})
test('can get backward history', (t) => {
const enhanced = enhance({})
enhanced.value = 'T.S. Eliot'
enhanced.value = 'Ham On Rye'
enhanced.value = 'Ginsberg'
enhanced.value = 'Kazuo Ishiguro'
t.equal(enhanced.$$index, 4)
t.deepEqual(enhanced.$$historyBackward(), [
{},
{ value: 'T.S. Eliot' },
{ value: 'Ham On Rye' },
{ value: 'Ginsberg' },
{ value: 'Kazuo Ishiguro' }
])
enhanced.value = 'Are you an echo?'
t.equal(enhanced.$$index, 5)
t.end()
})
test('can get forward history', (t) => {
const enhanced = enhance({})
enhanced.value = 'T.S. Eliot'
enhanced.value = 'Ham On Rye'
enhanced.value = 'Ginsberg'
enhanced.value = 'Kazuo Ishiguro'
enhanced.$$undo(2)
t.equal(enhanced.$$index, 2)
t.deepEqual(enhanced.$$historyForward(), [
{ value: 'Ginsberg' },
{ value: 'Kazuo Ishiguro' }
])
enhanced.value = 'Are you an echo?'
t.equal(enhanced.$$index, 3)
t.end()
})