blob: 9d5b4f38ce1688abe6ef267f66345ed61526e941 [file] [log] [blame]
Matteo Scandoloa4a47112016-12-16 10:06:13 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-resource';
4import 'angular-cookies';
5import {IXosResourceService} from './model.rest';
6import {xosDataSources} from '../index';
Matteo Scandoloa4a47112016-12-16 10:06:13 -08007
8let service: IXosResourceService;
9let resource: ng.resource.IResourceClass<any>;
10let httpBackend: ng.IHttpBackendService;
11let $resource;
12let $scope;
13
Matteo Scandolo828d1e82017-01-17 14:49:38 -080014const MockAppCfg = {
15 apiEndpoint: 'http://xos-test:3000/api',
16 websocketClient: 'http://xos-test:3000'
17};
18
Matteo Scandoloa4a47112016-12-16 10:06:13 -080019describe('The ModelRest service', () => {
20
21 beforeEach(angular.mock.module(xosDataSources));
22
23 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080024
25 angular.module(xosDataSources)
26 .constant('AppConfig', MockAppCfg);
27
Matteo Scandoloa4a47112016-12-16 10:06:13 -080028 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 Scandolo828d1e82017-01-17 14:49:38 -080050 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080051 .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 Scandolo828d1e82017-01-17 14:49:38 -080068 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test/1`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080069 .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});