-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtype_replace.js
More file actions
143 lines (121 loc) · 4.29 KB
/
type_replace.js
File metadata and controls
143 lines (121 loc) · 4.29 KB
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
140
141
142
143
import test from 'ava'
import { decode, TYPE } from '../'
import { testEncodeDecode, testPatchUnpatch } from './utils'
test('Valid type', function (t) {
const patch = { convert: TYPE.Replace({ hello: 'world' }) }
const expected = { convert: { $e: { hello: 'world' } } }
testEncodeDecode(t, patch, expected)
})
test('Escape', function (t) {
const patch = { escape: { $e: 1 } }
const expected = { escape: { $escape: { $e: 1 } } }
testEncodeDecode(t, patch, expected)
})
test('Ignore', function (t) {
const patch = { ignore: { $e: 1, $other: 1 } }
const expected = { ignore: { $e: 1, $other: 1 } }
testEncodeDecode(t, patch, expected)
})
test('$escaping', function (t) {
const patch = {
convert: TYPE.Replace([1, 2, 3]),
escape: { $e: 1 },
}
const expected = {
convert: { $e: [1, 2, 3] },
escape: { $escape: { $e: 1 } },
}
testEncodeDecode(t, patch, expected)
})
test('This should not be escaped because $e has another valid prop', function (t) {
const patch = {
convert: TYPE.Replace([1, 2, 3]),
ignored: { $e: [1, 2, 3], $escape: [1, 2, 3] },
}
const expected = {
convert: { $e: [1, 2, 3] },
ignored: { $e: [1, 2, 3], $escape: [1, 2, 3] },
}
testEncodeDecode(t, patch, expected)
})
test('This should not be escaped because $e has another valid prop 2', function (t) {
const patch = {
convert: TYPE.Replace([1, 2, 3]),
escape: { $escape: [1, 2, 3], $e: TYPE.Replace([1, 2, 3]) },
}
const expected = {
convert: { $e: [1, 2, 3] },
escape: { $escape: [1, 2, 3], $e: { $e: [1, 2, 3] } },
}
testEncodeDecode(t, patch, expected)
})
test('Decode alone', function (t) {
const patch = {
convert: { $e: [1, 2, 3] },
string: { $e: 'string' },
escape: { $escape: { $e: [1, 2, 3] } },
ignore: {
$escape: { $e: [1, 2, 3] },
two: { $e: [1, 2, 3] },
},
}
const expected = {
convert: TYPE.Replace([1, 2, 3]),
string: TYPE.Replace('string'),
escape: { $e: [1, 2, 3] },
ignore: {
$escape: TYPE.Replace([1, 2, 3]),
two: TYPE.Replace([1, 2, 3]),
},
}
t.deepEqual(decode(patch), expected)
})
test('patch array', function (t) {
const target = { value: { a: 1, b: 2 } }
const patch = { value: TYPE.Replace([1, 2, 3]) }
const expected = { value: [1, 2, 3] }
testPatchUnpatch(t, target, patch, expected)
})
test('patch object', function (t) {
const target = { value: { a: 1, b: 2 } }
const patch = { value: TYPE.Replace({ c: 3 }) }
const expected = { value: { c: 3 } }
testPatchUnpatch(t, target, patch, expected)
})
test('patch should replace the complete object', function (t) {
const obj_to_replace = { c: 3 }
const target = { value: { a: 1, b: 2 } }
const patch = { value: TYPE.Replace(obj_to_replace) }
const expected = { value: { c: 3 } }
const copyvalue = target.value
testPatchUnpatch(t, target, patch, expected, false, false)
t.is(obj_to_replace, target.value)
t.not(copyvalue, target.value)
})
test('testing that last test works correctly without replace', function (t) {
const target = { value: { a: 1, b: 2 } }
const patch = { value: { c: 3 } }
const expected = { value: { a: 1, b: 2, c: 3 } }
const copyvalue = target.value
testPatchUnpatch(t, target, patch, expected)
t.is(copyvalue, target.value)
t.deepEqual(copyvalue, target.value)
})
test('replace array', function (t) {
const target = { value: { a: 1, b: 2 } }
const patch = TYPE.Replace([1, 2])
const expected = [1, 2]
testPatchUnpatch(t, target, patch, expected)
})
test('same behavior as replace array', function (t) {
const target = { value: { a: 1, b: 2 } }
const patch = [1, 2]
const expected = [1, 2]
testPatchUnpatch(t, target, patch, expected)
})
test('inner types', function (t) {
const target = { value: { a: 1, b: 2 } }
const patch = { value: TYPE.Replace({ a: 1, b: TYPE.Swap(1, 2) }) }
const expected = { value: { a: 1, b: TYPE.Swap(1, 2) } }
testPatchUnpatch(t, target, patch, expected, false)
})