blob: f57c0c8a21b7949cdca939760905a1ed7770a65e [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';
7import {AppConfig} from '../../config/app.config';
8
9let service: IXosResourceService;
10let resource: ng.resource.IResourceClass<any>;
11let httpBackend: ng.IHttpBackendService;
12let $resource;
13let $scope;
14
15describe('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});