|
| 1 | +describe('DS.findAll(resourceName, params[, options]): ', function () { |
| 2 | + var errorPrefix = 'DS.findAll(resourceName, params[, options]): '; |
| 3 | + |
| 4 | + it('should throw an error when method pre-conditions are not met', function (done) { |
| 5 | + DS.findAll('does not exist', {}).then(function () { |
| 6 | + fail('should have rejected'); |
| 7 | + }, function (err) { |
| 8 | + assert.isTrue(err instanceof DS.errors.RuntimeError); |
| 9 | + assert.equal(err.message, errorPrefix + 'does not exist is not a registered resource!'); |
| 10 | + }); |
| 11 | + |
| 12 | + angular.forEach(TYPES_EXCEPT_OBJECT, function (key) { |
| 13 | + DS.findAll('post', key).then(function () { |
| 14 | + fail('should have rejected'); |
| 15 | + }, function (err) { |
| 16 | + assert.isTrue(err instanceof DS.errors.IllegalArgumentError); |
| 17 | + assert.equal(err.message, errorPrefix + 'params: Must be an object!'); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + angular.forEach(TYPES_EXCEPT_OBJECT, function (key) { |
| 22 | + if (key) { |
| 23 | + DS.findAll('post', {}, key).then(function () { |
| 24 | + fail('should have rejected'); |
| 25 | + }, function (err) { |
| 26 | + assert.isTrue(err instanceof DS.errors.IllegalArgumentError); |
| 27 | + assert.equal(err.message, errorPrefix + 'options: Must be an object!'); |
| 28 | + }); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + done(); |
| 33 | + }); |
| 34 | + it('should query the server for a collection', function (done) { |
| 35 | + $httpBackend.expectGET('http://test.angular-cache.com/posts?').respond(200, [p1, p2, p3, p4]); |
| 36 | + |
| 37 | + DS.findAll('post', {}).then(function (data) { |
| 38 | + assert.deepEqual(data, [p1, p2, p3, p4]); |
| 39 | + }, function (err) { |
| 40 | + console.error(err.stack); |
| 41 | + fail('Should not have rejected!'); |
| 42 | + }); |
| 43 | + |
| 44 | + assert.deepEqual(DS.filter('post', {}), [], 'The posts should not be in the store yet'); |
| 45 | + |
| 46 | + // Should have no effect because there is already a pending query |
| 47 | + DS.findAll('post', {}).then(function (data) { |
| 48 | + assert.deepEqual(data, [p1, p2, p3, p4]); |
| 49 | + }, function (err) { |
| 50 | + console.error(err.stack); |
| 51 | + fail('Should not have rejected!'); |
| 52 | + }); |
| 53 | + |
| 54 | + $httpBackend.flush(); |
| 55 | + |
| 56 | + assert.deepEqual(DS.filter('post', {}), [p1, p2, p3, p4], 'The posts are now in the store'); |
| 57 | + assert.isNumber(DS.lastModified('post', 5)); |
| 58 | + assert.isNumber(DS.lastSaved('post', 5)); |
| 59 | + |
| 60 | + // Should not make a request because the request was already completed |
| 61 | + DS.findAll('post', {}).then(function (data) { |
| 62 | + assert.deepEqual(data, [p1, p2, p3, p4]); |
| 63 | + }, function (err) { |
| 64 | + console.error(err.stack); |
| 65 | + fail('Should not have rejected!'); |
| 66 | + }); |
| 67 | + |
| 68 | + $httpBackend.expectGET('http://test.angular-cache.com/posts?').respond(200, [p1, p2, p3, p4]); |
| 69 | + |
| 70 | + // Should make a request because bypassCache is set to true |
| 71 | + DS.findAll('post', {}, { bypassCache: true }).then(function (data) { |
| 72 | + assert.deepEqual(data, [p1, p2, p3, p4]); |
| 73 | + }, function (err) { |
| 74 | + console.error(err.stack); |
| 75 | + fail('Should not have rejected!'); |
| 76 | + }); |
| 77 | + |
| 78 | + $httpBackend.flush(); |
| 79 | + |
| 80 | + done(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments