|
| 1 | +describe('DSCacheFactory integration', function () { |
| 2 | + |
| 3 | + beforeEach(function () { |
| 4 | + module('angular-data.DSCacheFactory'); |
| 5 | + }); |
| 6 | + |
| 7 | + beforeEach(startInjector); |
| 8 | + |
| 9 | + it('should get an item from the server and delete when using DSCacheFactory in passive mode', function (done) { |
| 10 | + DS.defineResource({ |
| 11 | + name: 'comment', |
| 12 | + endpoint: '/comments', |
| 13 | + deleteOnExpire: 'passive', |
| 14 | + maxAge: 20 |
| 15 | + }); |
| 16 | + |
| 17 | + $httpBackend.expectGET('http://test.angular-cache.com/comments/5').respond(200, { |
| 18 | + id: 5, |
| 19 | + text: 'test' |
| 20 | + }); |
| 21 | + |
| 22 | + DS.find('comment', 5).then(function (comment) { |
| 23 | + assert.deepEqual(comment, { |
| 24 | + id: 5, |
| 25 | + text: 'test' |
| 26 | + }); |
| 27 | + }, function (err) { |
| 28 | + console.error(err.stack); |
| 29 | + fail('Should not have rejected!'); |
| 30 | + }); |
| 31 | + |
| 32 | + $httpBackend.flush(); |
| 33 | + |
| 34 | + assert.deepEqual(DS.get('comment', 5), { |
| 35 | + id: 5, |
| 36 | + text: 'test' |
| 37 | + }, 'The comment is now in the store'); |
| 38 | + assert.isNumber(DS.lastModified('comment', 5)); |
| 39 | + assert.isNumber(DS.lastSaved('comment', 5)); |
| 40 | + |
| 41 | + setTimeout(function () { |
| 42 | + assert.isUndefined(DS.get('comment', 5)); |
| 43 | + |
| 44 | + assert.equal(lifecycle.beforeInject.callCount, 1, 'beforeInject should have been called'); |
| 45 | + assert.equal(lifecycle.afterInject.callCount, 1, 'afterInject should have been called'); |
| 46 | + assert.equal(lifecycle.serialize.callCount, 0, 'serialize should have been called'); |
| 47 | + assert.equal(lifecycle.deserialize.callCount, 1, 'deserialize should have been called'); |
| 48 | + |
| 49 | + done(); |
| 50 | + }, 100); |
| 51 | + }); |
| 52 | + it('should get an item from the server and delete when using DSCacheFactory in aggressive mode', function (done) { |
| 53 | + DS.defineResource({ |
| 54 | + name: 'comment', |
| 55 | + endpoint: '/comments', |
| 56 | + deleteOnExpire: 'aggressive', |
| 57 | + recycleFreq: 10, |
| 58 | + maxAge: 20 |
| 59 | + }); |
| 60 | + |
| 61 | + $httpBackend.expectGET('http://test.angular-cache.com/comments/5').respond(200, { |
| 62 | + id: 5, |
| 63 | + text: 'test' |
| 64 | + }); |
| 65 | + |
| 66 | + DS.find('comment', 5).then(function (comment) { |
| 67 | + assert.deepEqual(comment, { |
| 68 | + id: 5, |
| 69 | + text: 'test' |
| 70 | + }); |
| 71 | + }, function (err) { |
| 72 | + console.error(err.stack); |
| 73 | + fail('Should not have rejected!'); |
| 74 | + }); |
| 75 | + |
| 76 | + $httpBackend.flush(); |
| 77 | + |
| 78 | + assert.deepEqual(DS.get('comment', 5), { |
| 79 | + id: 5, |
| 80 | + text: 'test' |
| 81 | + }, 'The comment is now in the store'); |
| 82 | + assert.isNumber(DS.lastModified('comment', 5)); |
| 83 | + assert.isNumber(DS.lastSaved('comment', 5)); |
| 84 | + |
| 85 | + setTimeout(function () { |
| 86 | + assert.isUndefined(DS.get('comment', 5)); |
| 87 | + |
| 88 | + assert.equal(lifecycle.beforeInject.callCount, 1, 'beforeInject should have been called'); |
| 89 | + assert.equal(lifecycle.afterInject.callCount, 1, 'afterInject should have been called'); |
| 90 | + assert.equal(lifecycle.serialize.callCount, 0, 'serialize should have been called'); |
| 91 | + assert.equal(lifecycle.deserialize.callCount, 1, 'deserialize should have been called'); |
| 92 | + |
| 93 | + done(); |
| 94 | + }, 100); |
| 95 | + }); |
| 96 | +}); |
0 commit comments