This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanBeSerialized-test.js
128 lines (113 loc) · 3.01 KB
/
canBeSerialized-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
jest.dontMock('../canBeSerialized');
function mockObject(id, attributes) {
this.id = id;
this.attributes = attributes;
}
mockObject.registerSubclass = function () {};
jest.setMock('../ParseObject', mockObject);
function mockFile(url) {
this._url = url;
}
mockFile.prototype.url = function () {
return this._url;
};
jest.setMock('../ParseFile', mockFile);
const canBeSerialized = require('../canBeSerialized').default;
const ParseFile = require('../ParseFile');
const ParseObject = require('../ParseObject');
const ParseRelation = require('../ParseRelation').default;
describe('canBeSerialized', () => {
it('returns true for anything that is not a ParseObject', () => {
expect(canBeSerialized(12)).toBe(true);
expect(canBeSerialized('string')).toBe(true);
expect(canBeSerialized(false)).toBe(true);
expect(canBeSerialized([])).toBe(true);
expect(canBeSerialized({})).toBe(true);
});
it('validates primitives', () => {
const o = new ParseObject('oid', {
a: 12,
b: 'string',
c: false,
});
expect(canBeSerialized(o)).toBe(true);
});
it('returns false when a child is an unsaved object or file', () => {
let o = new ParseObject('oid', {
a: new ParseObject(),
});
expect(canBeSerialized(o)).toBe(false);
o = new ParseObject('oid', {
a: new ParseObject('oid2', {}),
});
expect(canBeSerialized(o)).toBe(true);
o = new ParseObject('oid', {
a: new ParseFile(),
});
expect(canBeSerialized(o)).toBe(false);
o = new ParseObject('oid', {
a: new ParseFile('http://files.parsetfss.com/a/parse.txt'),
});
expect(canBeSerialized(o)).toBe(true);
});
it('returns true when all children have an id', () => {
const child = new ParseObject('child', {});
const parent = new ParseObject(undefined, {
child: child,
});
child.attributes.parent = parent;
expect(canBeSerialized(parent)).toBe(true);
expect(canBeSerialized(child)).toBe(false);
});
it('returns true for relations', () => {
const relation = new ParseRelation(null, null);
const parent = new ParseObject(undefined, {
child: relation,
});
expect(canBeSerialized(parent)).toBe(true);
});
it('traverses nested arrays and objects', () => {
let o = new ParseObject('oid', {
a: {
a: {
a: {
b: new ParseObject(),
},
},
},
});
expect(canBeSerialized(o)).toBe(false);
o = new ParseObject('oid', {
a: {
a: {
a: {
b: new ParseObject('oid2'),
},
},
},
});
expect(canBeSerialized(o)).toBe(true);
o = new ParseObject('oid', {
a: [
1,
2,
3,
{
b: new ParseObject(),
},
],
});
expect(canBeSerialized(o)).toBe(false);
o = new ParseObject('oid', {
a: [
1,
2,
3,
{
b: new ParseObject('oid2'),
},
],
});
expect(canBeSerialized(o)).toBe(true);
});
});