Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-resource'; |
| 4 | import 'angular-cookies'; |
| 5 | import {IXosResourceService} from './model.rest'; |
| 6 | import {xosDataSources} from '../index'; |
| 7 | import {AppConfig} from '../../config/app.config'; |
| 8 | |
| 9 | let service: IXosResourceService; |
| 10 | let resource: ng.resource.IResourceClass<any>; |
| 11 | let httpBackend: ng.IHttpBackendService; |
| 12 | let $resource; |
| 13 | let $scope; |
| 14 | |
| 15 | describe('The ModelRest service', () => { |
| 16 | |
| 17 | beforeEach(angular.mock.module(xosDataSources)); |
| 18 | |
| 19 | beforeEach(() => { |
| 20 | angular.mock.module(xosDataSources); |
| 21 | }); |
| 22 | |
| 23 | |
| 24 | beforeEach(angular.mock.inject(( |
| 25 | ModelRest: IXosResourceService, |
| 26 | $httpBackend: ng.IHttpBackendService, |
| 27 | _$resource_: ng.resource.IResourceService, |
| 28 | _$rootScope_: ng.IRootScopeService |
| 29 | ) => { |
| 30 | service = ModelRest; |
| 31 | httpBackend = $httpBackend; |
| 32 | $resource = _$resource_; |
| 33 | $scope = _$rootScope_; |
| 34 | })); |
| 35 | |
| 36 | it('should return a resource based on the URL', () => { |
| 37 | resource = service.getResource('/core/test'); |
| 38 | expect(resource.constructor).toEqual($resource.constructor); |
| 39 | }); |
| 40 | |
| 41 | it('should have a query method', (done) => { |
| 42 | httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/test`) |
| 43 | .respond([ |
| 44 | {status: 'ok'} |
| 45 | ]); |
| 46 | resource = service.getResource('/core/test'); |
| 47 | resource.query().$promise |
| 48 | .then((res) => { |
| 49 | expect(res[0].status).toEqual('ok'); |
| 50 | done(); |
| 51 | }) |
| 52 | .catch(e => { |
| 53 | done(e); |
| 54 | }); |
| 55 | $scope.$apply(); |
| 56 | httpBackend.flush(); |
| 57 | }); |
| 58 | |
| 59 | it('should have a get method', (done) => { |
| 60 | httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/test/1`) |
| 61 | .respond([ |
| 62 | {status: 'ok'} |
| 63 | ]); |
| 64 | resource = service.getResource('/core/test'); |
| 65 | resource.get({id: 1}).$promise |
| 66 | .then((res) => { |
| 67 | expect(res[0].status).toEqual('ok'); |
| 68 | done(); |
| 69 | }) |
| 70 | .catch(e => { |
| 71 | done(e); |
| 72 | }); |
| 73 | $scope.$apply(); |
| 74 | httpBackend.flush(); |
| 75 | }); |
| 76 | }); |