forked from staugaard/ember-resource
/
destroySpec.js
103 lines (82 loc) · 2.85 KB
/
destroySpec.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
describe('Destroying resources', function() {
var Model, model, server;
beforeEach(function() {
Model = Ember.Resource.define({
schema: {
id: Number,
name: String,
subject: String
},
url: '/people'
});
server = sinon.fakeServer.create();
});
afterEach(function() {
server.restore();
Ember.Resource.errorHandler = null;
});
describe('#destroy', function() {
beforeEach(function() {
model = Model.create({id: 1});
expect(Model.identityMap.get(1)).to.equal(model);
});
it('should remove the object from the identity map', function() {
model.destroy();
expect(Model.identityMap.get(1)).to.be.undefined;
});
it('should not remove the object from the identity map when the instance if different', function() {
var otherModel = Model.create({id: 1, skipIdentityMap: true});
expect(otherModel).to.not.equal(model);
otherModel.destroy();
expect(Model.identityMap.get(1)).to.equal(model);
model.destroy();
expect(Model.identityMap.get(1)).to.be.undefined;
});
});
describe('#destroy without an identityMap on the Model', function() {
beforeEach(function() {
model = Model.create();
model.set('id', 1);
});
it('should not throw an exception', function() {
expect(model.destroy.bind(model)).to.not.throw(Error);
});
});
describe('destroyResource', function() {
var resource;
describe('instance destruction', function() {
beforeEach(function() {
server.respondWith('DELETE', '/people/1', [200, {}, '[["foo", "bar"]]']);
resource = Model.create({ id: 1, name: 'f0o' });
resource.destroyResource();
server.respond();
});
it('should defer destroying the Em.Resource instance till the next run loop', function(done) {
expect(resource.get('isDestroyed')).to.not.be.ok;
// The destroy happens in the "next" run loop.
Em.run.next(function() {
// In the loop *after* that one, we check the resource state.
Em.run.next(function() {
expect(resource.get('isDestroyed')).to.be.ok;
done();
});
});
});
});
describe('handling errors', function() {
beforeEach(function() {
server.respondWith('DELETE', '/people/1', [422, {}, '[["foo", "bar"]]']);
});
it('should pass a reference to the resource to the error handling function', function() {
var spy = sinon.spy();
Ember.Resource.errorHandler = function(a, b, c, fourthArgument) {
spy(fourthArgument.resource, fourthArgument.operation);
};
resource = Model.create({ id: 1, name: 'f0o' });
resource.destroyResource();
server.respond();
expect(spy.calledWith(resource, "destroy")).to.be.ok;
});
});
});
});