-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathimmutable.spec.js
136 lines (115 loc) · 4.03 KB
/
immutable.spec.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
var Immutable = require('immutable');
var Serialize = require('../immutable');
var serialize = Serialize(Immutable);
var stringify = serialize.stringify;
var parse = serialize.parse;
var data = {
map: Immutable.Map({ a: 1, b: 2, c: 3, d: 4 }),
orderedMap: Immutable.OrderedMap({ b: 2, a: 1, c: 3, d: 4 }),
list: Immutable.List([1,2,3,4,5,6,7,8,9,10]),
range: Immutable.Range(0,7),
repeat: Immutable.Repeat('hi', 100),
set: Immutable.Set([10,9,8,7,6,5,4,3,2,1]),
orderedSet: Immutable.OrderedSet([10,9,8,7,6,5,4,3,2,1]),
seq: Immutable.Seq.of(1,2,3,4,5,6,7,8),
stack: Immutable.Stack.of('a', 'b', 'c')
};
describe('Immutable', function () {
var stringified = {};
describe('Stringify', function () {
Object.keys(data).forEach(function (key) {
it(key, function () {
stringified[key] = stringify(data[key]);
expect(stringified[key]).toMatchSnapshot();
});
});
});
describe('Parse', function () {
Object.keys(data).forEach(function(key) {
it(key, function() {
expect(parse(stringified[key])).toEqual(data[key]);
});
});
});
describe('Record', function () {
var ABRecord = Immutable.Record({ a:1, b:2 });
var myRecord = new ABRecord({ b:3 });
var serialize = Serialize(Immutable, [ABRecord]);
var stringify = serialize.stringify;
var parse = serialize.parse;
var stringifiedRecord;
it('stringify', function() {
stringifiedRecord = stringify(myRecord);
expect(stringifiedRecord).toMatchSnapshot();
});
it('parse', function() {
expect(parse(stringifiedRecord)).toEqual(myRecord);
});
});
describe('Nested', function () {
var ABRecord = Immutable.Record({
map: Immutable.OrderedMap({ seq: data.seq, stack: data.stack }),
repeat: data.repeat
});
var nestedData = Immutable.Set(ABRecord(), data.orderedSet, data.range);
var serialize = Serialize(Immutable, [ABRecord]);
var stringify = serialize.stringify;
var parse = serialize.parse;
var stringifiedNested;
it('stringify', function() {
stringifiedNested = stringify(nestedData);
expect(stringifiedNested).toMatchSnapshot();
});
it('parse', function() {
expect(parse(stringifiedNested)).toEqual(nestedData);
});
});
describe('With references', function() {
it('serializes and deserializes', function() {
var sharedValue = [];
var record = Immutable.Record({
prop: sharedValue
});
var refs = [record];
var obj = Immutable.Map({
fst: new record(),
scnd: new record()
});
var serialized = stringify(obj, Serialize(Immutable, refs).replacer, null, true);
var parsed = JSON.parse(serialized);
var fstProp = parsed.data.fst.data.prop;
var scndProp = parsed.data.scnd.data.prop;
expect(fstProp).toEqual(scndProp);
expect(Array.isArray(obj.get('fst').get('prop')));
});
});
describe('Custom replacer and reviver functions', function() {
var customOneRepresentation = 'one';
function customReplacer(key, value, defaultReplacer) {
if (value === 1) {
return { data: customOneRepresentation, __serializedType__: 'number' };
}
return defaultReplacer(key, value);
}
function customReviver(key, value, defaultReviver) {
if (typeof value === 'object'
&& value.__serializedType__ === 'number'
&& value.data === customOneRepresentation) {
return 1;
}
return defaultReviver(key, value);
}
var serializeCustom = Serialize(Immutable, null, customReplacer, customReviver);
Object.keys(data).forEach(function(key) {
var stringified = serializeCustom.stringify(data[key]);
it(key, function() {
var deserialized = serializeCustom.parse(stringified);
expect(deserialized).toEqual(data[key]);
if (key === 'map' || key === 'orderedMap') {
deserializedDefault = parse(stringified);
expect(deserializedDefault.get('a')).toEqual(customOneRepresentation);
}
});
});
})
});