-
-
Save nerdfiles/84e7c443b97976a0d8798027e84d6d10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(['services/module', 'underscore'], function (module, _) { | |
| 'use strict'; | |
| function OrderResource($http, $q, link) { | |
| // ... other functions removed: viewState, accessState, flattenResource | |
| // viewState is a deeper resource | |
| // accessState looks at the Accept headers | |
| // flattenResource: flattens all of this for the client-side resource (in-memory model bound to view) | |
| // only GET on collection exposed (rather than get on item) | |
| return { | |
| get: function (links, relationshipType, mediaType) { | |
| return link.get(links || 'HEAD', relationshipType || 'collection', mediaType).then( | |
| function success(response) { | |
| return $q.all(_.map(link.filter(response.data, 'item', mediaType), function (item) { | |
| return $http.get(item.href, { headers: {accept: item.type}}).then( | |
| function success(response) { | |
| return $q.all([viewState(response.data, mediaType), accessState(response.headers())]). | |
| then(flattenResource(response.data), error); | |
| }, error) | |
| })). | |
| then(function (resources) { | |
| return { items: resources, links: response.data.links}; | |
| }); | |
| }, | |
| error); | |
| } | |
| } | |
| } | |
| return module.factory('OrderResource', ['$http', '$q', 'link', OrderResource]); | |
| } | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*global describe beforeEach it expect inject module angular $ */ | |
| define(['chai', 'mocks', 'services/httpLink'], | |
| function (chai) { | |
| 'use strict'; | |
| var should = chai.should(), link, $httpBackend; | |
| describe('services/httpLink', function () { | |
| beforeEach(function () { | |
| $('<link rel="collection" type="text/html" href="http://semanticlink-test">').prependTo('HEAD'); | |
| }); | |
| describe('HTTP', function () { | |
| describe("returns a promise", function () { | |
| var success, failure | |
| beforeEach(function () { | |
| success = jasmine.createSpy("Resource"); | |
| failure = jasmine.createSpy("Fail Resource"); | |
| $httpBackend.expectGET('http://semanticlink-test/').respond(204); | |
| }); | |
| it('should do request based on href (200,204) and return to done', function () { | |
| link.get("HEAD", 'collection', 'text/html'). | |
| then(success, failure); | |
| $httpBackend.flush(); | |
| expect(success).toHaveBeenCalled(); | |
| expect(failure).toHaveBeenCalled(); | |
| }); | |
| }); | |
| beforeEach(inject(function (_$httpBackend_) { | |
| $httpBackend = _$httpBackend_; | |
| })); | |
| }); | |
| afterEach(function () { | |
| $('link[rel="collection"]').remove(); | |
| }); | |
| beforeEach(module('httpLink')); | |
| beforeEach(inject(function (_link_) { | |
| link = _link_; | |
| })); | |
| }); | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment