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