forked from js-data/js-data-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.spec.js
156 lines (128 loc) · 5.18 KB
/
find.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
describe('DSHttpAdapter.find(resourceConfig, id, options)', function () {
it('should make a GET request', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts/1');
assert.equal(_this.requests[0].method, 'GET');
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(p1));
}, 30);
return dsHttpAdapter.find(Post, 1).then(function (data) {
assert.deepEqual(data, p1, 'post should have been found');
setTimeout(function () {
assert.equal(2, _this.requests.length);
assert.equal(_this.requests[1].url, 'api2/posts/1');
assert.equal(_this.requests[1].method, 'GET');
_this.requests[1].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(p1));
}, 30);
return dsHttpAdapter.find(Post, 1, { basePath: 'api2' });
}).then(function (data) {
assert.deepEqual(data, p1, 'post should have been found');
assert.equal(queryTransform.callCount, 2, 'queryTransform should have been called twice');
});
});
it('should allow overriding urlPath', function () {
var _this = this;
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/foo/bar/beep/boop/1');
assert.equal(_this.requests[0].method, 'GET');
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(p1));
}, 30);
return Post.find(1, { urlPath: '/foo/bar/beep/boop/1' }).then(function (data) {
assert.equal(data.id, p1.id, 'post should have been found');
assert.equal(queryTransform.callCount, 1, 'queryTransform should have been called twice');
});
});
it('should use default configs', function () {
var _this = this;
dsHttpAdapter.defaults.httpConfig.params = { test: 'test' };
dsHttpAdapter.defaults.httpConfig.headers = { Authorization: 'test' };
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts/1?test=test');
assert.equal(_this.requests[0].method, 'GET');
assert.deepEqual({
Authorization: 'test',
Accept: 'application/json, text/plain, */*'
}, _this.requests[0].requestHeaders);
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(p1));
}, 30);
return dsHttpAdapter.find(Post, 1).then(function (data) {
assert.deepEqual(data, p1, 'post should have been found');
delete dsHttpAdapter.defaults.httpConfig.params;
delete dsHttpAdapter.defaults.httpConfig.headers;
});
});
it('should log errors', function () {
var _this = this;
var loggedError;
dsHttpAdapter.defaults.error = function (err) {
loggedError = err;
};
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts/1');
assert.equal(_this.requests[0].method, 'GET');
_this.requests[0].respond(404, { 'Content-Type': 'text/plain' }, 'Not Found');
}, 30);
return dsHttpAdapter.find(Post, 1).then(function () {
throw new Error('Should not have succeeded!');
}, function () {
console.log(loggedError);
assert.isString(loggedError);
assert.isTrue(loggedError.indexOf('api/posts/1') !== -1, loggedError);
});
});
it('should use suffixes', function () {
var _this = this;
var Thing = datastore.defineResource({
name: 'thing',
endpoint: 'things',
suffix: '.xml'
});
var otherAdapter = new DSHttpAdapter({
suffix: '.json'
});
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'things/1.xml');
assert.equal(_this.requests[0].method, 'GET');
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ id: 1 }));
}, 30);
return dsHttpAdapter.find(Thing, 1).then(function () {
setTimeout(function () {
assert.equal(2, _this.requests.length);
assert.equal(_this.requests[1].url, 'api/posts/1.json');
assert.equal(_this.requests[1].method, 'GET');
_this.requests[1].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ id: 1 }));
}, 30);
return otherAdapter.find(Post, 1);
});
});
it('should use parent', function () {
var _this = this;
var Thing = datastore.defineResource({
name: 'thing',
endpoint: 'things',
relations: {
belongsTo: {
user: {
localKey: 'userId',
localField: 'user',
parent: true
}
}
}
});
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'user/2/things/1');
assert.equal(_this.requests[0].method, 'GET');
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ id: 1 }));
}, 30);
return dsHttpAdapter.find(Thing, 1, { params: { userId: 2 } }).then(function (data) {
assert.deepEqual(data, { id: 1 }, 'thing should have been found');
});
});
});