forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsavedChildren-test.js
200 lines (179 loc) · 4.46 KB
/
unsavedChildren-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
194
195
196
197
198
199
200
jest.dontMock('../ParseFile');
jest.dontMock('../unsavedChildren');
jest.dontMock('../CoreManager');
function mockObject({ className, localId, id, attributes, dirty }) {
this.className = className;
this.localId = localId;
this.id = id;
this.attributes = attributes;
this._dirty = !!dirty;
}
mockObject.registerSubclass = function () {};
mockObject.prototype = {
_getId() {
return this.id || this.localId;
},
dirty() {
return this._dirty;
},
};
jest.setMock('../ParseObject', mockObject);
const CoreManager = require('../CoreManager');
CoreManager.setParseObject(mockObject);
const ParseFile = require('../ParseFile').default;
const ParseObject = require('../ParseObject');
const ParseRelation = require('../ParseRelation').default;
const unsavedChildren = require('../unsavedChildren').default;
describe('unsavedChildren', () => {
it('finds unsaved files', () => {
const files = [
new ParseFile('parse1.txt', [61, 170, 236, 120]),
new ParseFile('parse2.txt', [61, 170, 236, 120]),
new ParseFile('parse3.txt', [61, 170, 236, 120]),
];
const f = new ParseObject({
className: 'Folder',
id: '121',
attributes: {
a: files[0],
b: files[1],
c: files[2],
},
});
expect(unsavedChildren(f)).toEqual([files[0], files[1], files[2]]);
f.attributes = {
files: files,
};
expect(unsavedChildren(f)).toEqual([files[0], files[1], files[2]]);
f.attributes = {
files: {
a: files[0],
b: files[1],
c: files[2],
},
};
expect(unsavedChildren(f)).toEqual([files[0], files[1], files[2]]);
});
it('only returns unique files', () => {
const file = new ParseFile('parse1.txt', [61, 170, 236, 120]);
const f = new ParseObject({
className: 'Folder',
id: '121',
attributes: {
a: file,
b: file,
c: file,
},
});
expect(unsavedChildren(f)).toEqual([file]);
});
it('finds unsaved child objects', () => {
const a = new ParseObject({
className: 'File',
localId: 'local0',
attributes: {},
dirty: true,
});
const b = new ParseObject({
className: 'File',
localId: 'local1',
attributes: {},
dirty: true,
});
const f = new ParseObject({
className: 'Folder',
id: '121',
attributes: {
a: a,
b: b,
},
});
expect(unsavedChildren(f)).toEqual([a, b]);
f.attributes = {
contents: [a, b],
};
expect(unsavedChildren(f)).toEqual([a, b]);
f.attributes = {
contents: {
a: a,
b: b,
},
};
expect(unsavedChildren(f)).toEqual([a, b]);
});
it('throws on nested objects without ids', () => {
const a = new ParseObject({
className: 'File',
localId: 'local0',
attributes: {},
dirty: true,
});
const b = new ParseObject({
className: 'File',
localId: 'local1',
attributes: {
a: a,
},
dirty: true,
});
const f = new ParseObject({
className: 'Folder',
id: '121',
attributes: {
b: b,
},
});
expect(unsavedChildren.bind(null, f)).toThrow('Cannot create a pointer to an unsaved Object.');
});
it('can explicitly allow nested objects without ids', () => {
const a = new ParseObject({
className: 'Folder',
localId: 'local0',
dirty: true,
attributes: {},
});
const b = new ParseObject({
className: 'Folder',
localId: 'local1',
dirty: true,
attributes: {},
});
const c = new ParseObject({
className: 'File',
localId: 'local2',
dirty: true,
attributes: {},
});
a.attributes.items = [b];
b.attributes.items = [c];
expect(unsavedChildren(a, true)).toEqual([b, c]);
});
it('does not revisit objects', () => {
const a = new ParseObject({
className: 'File',
id: '130',
attributes: {
b: new ParseObject({
className: 'File',
localId: '131',
attributes: {},
dirty: true,
}),
},
dirty: true,
});
a.attributes.b.attributes.a = a;
expect(unsavedChildren(a)).toEqual([a.attributes.b]);
});
it('skips Relation', () => {
const relation = new ParseRelation(null, null);
const f = new ParseObject({
className: 'Folder',
id: '121',
attributes: {
r: relation,
},
});
expect(unsavedChildren(f)).toEqual([]);
});
});