forked from knockout/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mappingHelperBehaviors.js
117 lines (102 loc) · 4.36 KB
/
mappingHelperBehaviors.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
describe('Mapping helpers', function() {
it('ko.toJS should require a parameter', function() {
var didThrow = false;
try { ko.toJS() }
catch(ex) { didThow = true }
expect(didThow).toEqual(true);
});
it('ko.toJS should unwrap observable values', function() {
var atomicValues = ["hello", 123, true, null, undefined, { a : 1 }];
for (var i = 0; i < atomicValues.length; i++) {
var data = ko.observable(atomicValues[i]);
var result = ko.toJS(data);
expect(ko.isObservable(result)).toEqual(false);
expect(result).toEqual(atomicValues[i]);
}
});
it('ko.toJS should recursively unwrap observables whose values are themselves observable', function() {
var weirdlyNestedObservable = ko.observable(
ko.observable(
ko.observable(
ko.observable('Hello')
)
)
);
var result = ko.toJS(weirdlyNestedObservable);
expect(result).toEqual('Hello');
});
it('ko.toJS should unwrap observable properties, including nested ones', function() {
var data = {
a : ko.observable(123),
b : {
b1 : ko.observable(456),
b2 : [789, ko.observable('X')]
}
};
var result = ko.toJS(data);
expect(result.a).toEqual(123);
expect(result.b.b1).toEqual(456);
expect(result.b.b2[0]).toEqual(789);
expect(result.b.b2[1]).toEqual('X');
});
it('ko.toJS should unwrap observable arrays and things inside them', function() {
var data = ko.observableArray(['a', 1, { someProp : ko.observable('Hey') }]);
var result = ko.toJS(data);
expect(result.length).toEqual(3);
expect(result[0]).toEqual('a');
expect(result[1]).toEqual(1);
expect(result[2].someProp).toEqual('Hey');
});
it('ko.toJS should resolve reference cycles', function() {
var obj = {};
obj.someProp = { owner : ko.observable(obj) };
var result = ko.toJS(obj);
expect(result.someProp.owner).toEqual(result);
});
it('ko.toJS should treat Date instances as primitives (and not walk their subproperties)', function () {
var date = new Date();
var result = ko.toJS({ due: ko.observable(date) });
expect(result.due instanceof Date).toEqual(true);
expect(result.due).toEqual(date);
});
it('ko.toJSON should unwrap everything and then stringify', function() {
var data = ko.observableArray(['a', 1, { someProp : ko.observable('Hey') }]);
var result = ko.toJSON(data);
// Check via parsing so the specs are independent of browser-specific JSON string formatting
expect(typeof result).toEqual('string');
var parsedResult = ko.utils.parseJson(result);
expect(parsedResult.length).toEqual(3);
expect(parsedResult[0]).toEqual('a');
expect(parsedResult[1]).toEqual(1);
expect(parsedResult[2].someProp).toEqual('Hey');
});
it('ko.toJSON should respect .toJSON functions on arrays', function() {
var data = {
a: [1, 2],
b: ko.observableArray([3, 4])
};
data.a.toJSON = function() { return "a-mapped" };
data.b().toJSON = function() { return "b-mapped" };
var result = ko.toJSON(data);
// Check via parsing so the specs are independent of browser-specific JSON string formatting
expect(typeof result).toEqual('string');
var parsedResult = ko.utils.parseJson(result);
expect(parsedResult).toEqual({ a: "a-mapped", b: "b-mapped" });
});
it('ko.toJSON should respect replacer/space options', function() {
var data = { a: 1 };
// Without any options
expect(ko.toJSON(data)).toEqual("{\"a\":1}");
// With a replacer
function myReplacer(x, obj) {
expect(obj).toEqual(data);
return "my replacement";
};
expect(ko.toJSON(data, myReplacer)).toEqual("\"my replacement\"");
// With spacer
expect(ko.toJSON(data, undefined, " ")).toEqualOneOf([
"{\n \"a\":1\n}", // Firefox 3.6, for some reason, omits the space after the colon. Doesn't really matter to us.
"{\n \"a\": 1\n}" // All other browsers produce this format
]);
});
})