forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode-test.js
193 lines (175 loc) · 5.11 KB
/
encode-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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
jest.dontMock('../encode');
jest.dontMock('../ParseACL');
jest.dontMock('../ParseFile');
jest.dontMock('../ParseGeoPoint');
jest.dontMock('../ParseOp');
jest.dontMock('../ParseUser');
jest.dontMock('../CoreManager');
const mockObject = function (className) {
this.className = className;
};
mockObject.registerSubclass = function () {};
mockObject.prototype = {
_getServerData() {
return this._serverData;
},
toPointer() {
return 'POINTER';
},
toOfflinePointer() {
return 'OFFLINE_POINTER';
},
_getId() {
return 'local1234';
},
dirty() {},
toJSON() {
return this.attributes;
},
_toFullJSON(seen, offline) {
const json = {
__type: 'Object',
className: this.className,
};
for (const attr in this.attributes) {
json[attr] = encode(this.attributes[attr], false, false, seen.concat(this), offline);
}
return json;
},
};
jest.setMock('../ParseObject', mockObject);
const encode = require('../encode').default;
const ParseACL = require('../ParseACL').default;
const ParseFile = require('../ParseFile').default;
const ParseGeoPoint = require('../ParseGeoPoint').default;
const ParseObject = require('../ParseObject');
const ParseRelation = require('../ParseRelation').default;
const CoreManager = require('../CoreManager');
CoreManager.setParseObject(mockObject);
CoreManager.setParseOp(require('../ParseOp'));
CoreManager.setParseUser(require('../ParseUser').default);
describe('encode', () => {
it('ignores primitives', () => {
expect(encode(undefined)).toBe(undefined);
expect(encode(null)).toBe(null);
expect(encode(true)).toBe(true);
expect(encode(12)).toBe(12);
expect(encode('string')).toBe('string');
});
it('encodes dates', () => {
expect(encode(new Date(Date.UTC(2015, 1)))).toEqual({
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
});
expect(encode.bind(null, new Date(Date.parse(null)))).toThrow(
'Tried to encode an invalid date.'
);
});
it('encodes regular expressions', () => {
expect(encode(new RegExp('^hello'))).toEqual('^hello');
expect(encode(/a[^b]+c/g)).toEqual('a[^b]+c');
});
it('encodes GeoPoints', () => {
const point = new ParseGeoPoint(40.5, 50.4);
expect(encode(point)).toEqual({
__type: 'GeoPoint',
latitude: 40.5,
longitude: 50.4,
});
});
it('encodes Files', () => {
const file = new ParseFile('parse.txt');
expect(encode.bind(null, file)).toThrow('Tried to encode an unsaved file.');
file._url = 'https://files.parsetfss.com/a/parse.txt';
expect(encode(file)).toEqual({
__type: 'File',
name: 'parse.txt',
url: 'https://files.parsetfss.com/a/parse.txt',
});
});
it('encodes Relations', () => {
const rel = new ParseRelation();
encode(rel);
expect(rel.toJSON.mock.calls.length).toBe(1);
});
it('encodes ACLs', () => {
const acl = new ParseACL({ aUserId: { read: true, write: false } });
expect(encode(acl)).toEqual({
aUserId: {
read: true,
write: false,
},
});
});
it('encodes ParseObjects', () => {
const obj = new ParseObject('Item');
obj._serverData = {};
expect(encode(obj)).toEqual('POINTER');
obj._serverData = obj.attributes = {
str: 'string',
date: new Date(Date.UTC(2015, 1, 1)),
};
expect(encode(obj)).toEqual({
__type: 'Object',
className: 'Item',
str: 'string',
date: {
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
},
});
obj.attributes.self = obj;
expect(encode(obj)).toEqual({
__type: 'Object',
className: 'Item',
str: 'string',
date: {
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
},
self: 'POINTER',
});
});
it('encodes ParseObjects offline', () => {
const obj = new ParseObject('Item');
obj._serverData = {};
expect(encode(obj, false, false, undefined, true)).toEqual('OFFLINE_POINTER');
obj._serverData = obj.attributes = {
str: 'string',
date: new Date(Date.UTC(2015, 1, 1)),
};
obj.attributes.self = obj;
expect(encode(obj, false, false, undefined, true)).toEqual({
__type: 'Object',
className: 'Item',
str: 'string',
date: {
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
},
self: 'OFFLINE_POINTER',
});
});
it('does not encode ParseObjects when they are disallowed', () => {
const obj = new ParseObject('Item');
expect(encode.bind(null, obj, true)).toThrow('Parse Objects not allowed here');
});
it('iterates over arrays', () => {
let arr = [12, new Date(Date.UTC(2015, 1)), 'str'];
expect(encode(arr)).toEqual([12, { __type: 'Date', iso: '2015-02-01T00:00:00.000Z' }, 'str']);
arr = [arr];
expect(encode(arr)).toEqual([[12, { __type: 'Date', iso: '2015-02-01T00:00:00.000Z' }, 'str']]);
});
it('iterates over objects', () => {
const obj = {
num: 12,
date: new Date(Date.UTC(2015, 1)),
str: 'abc',
};
expect(encode(obj)).toEqual({
num: 12,
date: { __type: 'Date', iso: '2015-02-01T00:00:00.000Z' },
str: 'abc',
});
});
});