-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate.spec.js
311 lines (251 loc) · 14.5 KB
/
create.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
describe('DSJsonApiAdapter.create(resourceConfig, attrs, options)', function () {
describe('Adapter CREATE', function () {
var response = {};
beforeEach(function () {
response.model = [{ Id: '5', age: 32, author: 'John', type: 'person' }];
response.jsonApiData = new DSJsonApiAdapter.JsonApi.JsonApiRequest()
.WithData(new DSJsonApiAdapter.JsonApi.JsonApiData('posts')
.WithId('5')
.WithAttribute('author', 'John')
.WithAttribute('age', 32)
.WithAttribute('type', 'person')
//.WithLink('self', '/container/1/posts/5')
);
});
it('should make a POST request, and NOT include id when not specified by client', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts');
assert.equal(_this.requests[0].method, 'POST');
assert.isDefined(_this.requests[0].requestHeaders, 'Request Contains headers');
assert.equal(_this.requests[0].requestHeaders['Accept'], 'application/vnd.api+json', 'Contains json api accept required header');
assert.include(_this.requests[0].requestHeaders['Content-Type'], 'application/vnd.api+json', 'Contains json api content-type header');
var request = JSON.parse(_this.requests[0].requestBody);
var expectedData = {
//id:'100', Do not set..
type: 'posts',
attributes: { author: 'John', age: 32 }
//links: {},
//relationships: {}
};
assert.deepEqual(request.data, expectedData, 'Json data serialized to jsonApi data correctly, without client id');
_this.requests[0].respond(200, { 'Content-Type': 'application/vnd.api+json' }, DSUtils.toJson(response.jsonApiData));
}, 30);
return dsHttpAdapter.create(Post, { author: 'John', age: 32 }).then(function (data) {
ignoreMetaData(data);
assert.deepEqual(data[0], response.model[0], 'post should have been created when no id supplied by client');
});
});
it('should make a POST request, and INCLUDE client id when specified by client', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts');
assert.equal(_this.requests[0].method, 'POST');
assert.isDefined(_this.requests[0].requestHeaders, 'Request Contains headers');
assert.equal(_this.requests[0].requestHeaders['Accept'], 'application/vnd.api+json', 'Contains json api accept required header');
assert.include(_this.requests[0].requestHeaders['Content-Type'], 'application/vnd.api+json', 'Contains json api content-type header');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({
data: {
id: '5',
type: 'posts',
attributes: { author: 'John', age: 32 }
//links: {},
//relationships: {}
}
}), 'Json data serialized to jsonApi data correctly, with client id');
_this.requests[0].respond(200, { 'Content-Type': 'application/vnd.api+json' }, DSUtils.toJson(response.jsonApiData));
}, 30);
return dsHttpAdapter.create(Post, { Id: '5', author: 'John', age: 32 }).then(function (data) {
ignoreMetaData(data);
assert.deepEqual(data[0], response.model[0], 'post should have been created when no id supplied by client');
});
});
it('should make a POST request, to create single object', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts');
assert.equal(_this.requests[0].method, 'POST');
assert.isDefined(_this.requests[0].requestHeaders, 'Request Contains headers');
assert.equal(_this.requests[0].requestHeaders['Accept'], 'application/vnd.api+json', 'Contains json api accept required header');
assert.include(_this.requests[0].requestHeaders['Content-Type'], 'application/vnd.api+json', 'Contains json api content-type header');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({
data: {
//id: '',
type: 'posts',
attributes: { author: 'John', age: 32 }
//links: {},
//relationships: {}
}
}), 'Json data serialized to jsonApi data correctly');
_this.requests[0].respond(200, { 'Content-Type': 'application/vnd.api+json' }, DSUtils.toJson(response.jsonApiData));
}, 30);
return dsHttpAdapter.create(Post, { author: 'John', age: 32 }).then(function (data) {
// We are not testing meta data yet
ignoreMetaData(data);
assert.deepEqual(data, response.model, 'post should have been created#1');
setTimeout(function () {
assert.equal(2, _this.requests.length);
assert.equal(_this.requests[1].url, 'api2/posts');
assert.isDefined(_this.requests[1].requestHeaders);
assert.equal(_this.requests[1].requestHeaders['Accept'], 'application/vnd.api+json', 'Contains json api accept required header');
assert.include(_this.requests[1].requestHeaders['Content-Type'], 'application/vnd.api+json', 'Contains json api content-type header');
assert.equal(_this.requests[1].requestBody, DSUtils.toJson({
data: {
//id: '',
type: 'posts',
attributes: { author: 'John', age: 32 }
//links: {},
//relationships: {}
}
}));
_this.requests[1].respond(200, { 'Content-Type': 'application/vnd.api+json' }, DSUtils.toJson(response.jsonApiData));
}, 30);
return dsHttpAdapter.create(Post, { author: 'John', age: 32 }, { basePath: 'api2' }).then(function (data) {
ignoreMetaData(data);
assert.deepEqual(data, response.model, 'post should have been created#2');
assert.equal(queryTransform.callCount, 2, 'queryTransform should have been called twice');
});
});
});
it('should make a POST request, to create single object and NOT cache response', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts');
assert.equal(_this.requests[0].method, 'POST');
assert.isDefined(_this.requests[0].requestHeaders, 'Request Contains headers');
assert.equal(_this.requests[0].requestHeaders['Accept'], 'application/vnd.api+json', 'Contains json api accept required header');
assert.include(_this.requests[0].requestHeaders['Content-Type'], 'application/vnd.api+json', 'Contains json api content-type header');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({
data: {
//id: '',
type: 'posts',
attributes: { author: 'John', age: 32 }
//links: {},
//relationships: {}
}
}), 'Json data serialized to jsonApi data correctly');
_this.requests[0].respond(200, { 'Content-Type': 'application/vnd.api+json' }, DSUtils.toJson(response.jsonApiData));
}, 30);
return dsHttpAdapter.create(Post, { author: 'John', age: 32 }, { cacheResponse: false }).then(function (data) {
// We are not testing meta data yet
ignoreMetaData(data);
assert.deepEqual(data, response.model, 'post should have been created#1');
});
});
it('should make a POST request, to create multiple data objects', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts');
assert.equal(_this.requests[0].method, 'POST');
assert.isDefined(_this.requests[0].requestHeaders, 'Request Contains headers');
assert.equal(_this.requests[0].requestHeaders['Accept'], 'application/vnd.api+json', 'Contains json api accept required header');
assert.include(_this.requests[0].requestHeaders['Content-Type'], 'application/vnd.api+json', 'Contains json api content-type header');
assert.equal(_this.requests[0].requestBody, DSUtils.toJson({
data: {
//id: '',
type: 'posts',
attributes: { author: 'John', age: 32 }
//links: {},
//relationships: {}
}
}), 'Json data serialized to jsonApi data correctly');
_this.requests[0].respond(200, { 'Content-Type': 'application/vnd.api+json' }, DSUtils.toJson(response.jsonApiData));
}, 30);
return dsHttpAdapter.create(Post, { author: 'John', age: 32 }).then(function (data) {
// We are not testing meta data yet
ignoreMetaData(data);
assert.deepEqual(response.model, data, 'post should have been created#1');
})
});
});
describe('DS Create:', function () {
var ds;
var testData = { config: {} };
beforeEach(function () {
//Create js-data
ds = new JSData.DS();
var dsHttpAdapter = new DSJsonApiAdapter.JsonApiAdapter({
queryTransform: queryTransform
});
ds.registerAdapter('jsonApi', dsHttpAdapter, { default: true });
// Configure js-data resources
testData.config.Author = ds.defineResource({
name: 'author',
idAttribute: 'id',
relations: {
hasMany: {
article: {
localField: 'articles',
foreignKey: 'authorid'
}
},
hasOne: {
address: {
localField: 'address',
foreignKey: 'authorid'
}
}
}
});
testData.config.Address = ds.defineResource({
name: 'address',
idAttribute: 'id'
});
testData.config.Article = ds.defineResource({
name: 'article',
idAttribute: 'id'
});
});
it('Should make a POST request and send relationship data', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length, "First Call");
var request = _this.requests[_this.requests.length - 1];
assert.equal(request.url, 'author/3');
assert.equal(request.method, 'PATCH');
assert.isDefined(request.requestHeaders, 'Request Contains headers');
assert.equal(request.requestHeaders['Accept'], 'application/vnd.api+json', 'Contains json api accept required header');
assert.include(request.requestHeaders['Content-Type'], 'application/vnd.api+json', 'Contains json api content-type header');
assert.equal(request.requestBody, DSUtils.toJson({
data: {
id: '3',
type: 'author',
attributes: { author: 'John', age: 32 },
relationships: {
// To many relation
articles: {
links: {},
data: [
{ id: '1', type: 'article' },
{ id: '2', type: 'article' }
]
},
// To one relation
address: {
links: {},
data: { id: '1', type: 'address' }
}
},
}
}), 'Json data serialized to jsonApi data correctly, with related data');
request.respond(200, { 'Content-Type': 'application/vnd.api+json' }, request.requestBody);
}, 30);
var article1 = ds.inject('article', { id: '1', name: 'author#1' });
var address = ds.inject('address', { id: '1', street: 'Street' });
var article2 = ds.inject('article', { id: '2', name: 'author#2' });
var author1 = ds.inject('author', { id:'3', author: 'John', age: 32 });
article1.authorid = 3;
article2.authorid = 3;
address.authorid = 3;
console.log('Author Changes', author1.DSChanges());
console.log('Article#1 Changes', article1.DSChanges());
return testData.config.Author.save(3, {changes:false, jsonApi: { updateRelationships: true } }).then(function (data) {
})
});
});
});