forked from 418sec/js-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestroy.test.js
102 lines (89 loc) · 3.24 KB
/
destroy.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
describe('DS#destroy', function () {
it('should delete an item from the data store', function () {
var _this = this;
Post.inject(p1);
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts/5');
assert.equal(_this.requests[0].method, 'DELETE');
_this.requests[0].respond(200, { 'Content-Type': 'text/plain' }, '5');
}, 100);
return Post.destroy(5).then(function (id) {
assert.equal(id, '5', 'post 5 should have been deleted');
assert.equal(lifecycle.beforeDestroy.callCount, 1, 'beforeDestroy should have been called');
assert.equal(lifecycle.afterDestroy.callCount, 1, 'afterDestroy should have been called');
assert.isUndefined(Post.get(5));
assert.equal(Post.lastModified(5), 0);
assert.equal(Post.lastSaved(5), 0);
});
});
it('should handle nested resources', function () {
var _this = this;
var testComment = {
id: 5,
content: 'test'
};
var testComment2 = {
id: 6,
content: 'test',
approvedBy: 4
};
Comment.inject(testComment);
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'http://test.js-data.io/user/4/comment/5');
assert.equal(_this.requests[0].method, 'DELETE');
_this.requests[0].respond(204);
}, 100);
return Comment.destroy(5, {
params: {
approvedBy: 4
}
}).then(function () {
Comment.inject(testComment2);
setTimeout(function () {
assert.equal(2, _this.requests.length);
assert.equal(_this.requests[1].url, 'http://test.js-data.io/user/4/comment/6');
assert.equal(_this.requests[1].method, 'DELETE');
_this.requests[1].respond(204);
}, 100);
return Comment.destroy(6, {
bypassCache: true
});
}).then(function () {
Comment.inject(testComment2);
setTimeout(function () {
assert.equal(3, _this.requests.length);
assert.equal(_this.requests[2].url, 'http://test.js-data.io/comment/6');
assert.equal(_this.requests[2].method, 'DELETE');
_this.requests[2].respond(204);
}, 100);
return Comment.destroy(6, {
params: {
approvedBy: false
}
});
});
});
it('should eager eject', function () {
var _this = this;
Post.inject(p1);
setTimeout(function () {
assert.isUndefined(Post.get(5));
setTimeout(function () {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'http://test.js-data.io/posts/5');
assert.equal(_this.requests[0].method, 'DELETE');
_this.requests[0].respond(200, { 'Content-Type': 'text/plain' }, '5');
}, 100);
}, 100);
return Post.destroy(5, { eagerEject: true }).then(function (id) {
assert.equal(id, '5', 'post 5 should have been deleted');
assert.equal(lifecycle.beforeDestroy.callCount, 1, 'beforeDestroy should have been called');
assert.equal(lifecycle.afterDestroy.callCount, 1, 'afterDestroy should have been called');
assert.isUndefined(Post.get(5));
assert.equal(Post.lastModified(5), 0);
assert.equal(Post.lastSaved(5), 0);
});
});
});