blob: d545ac204ea81152a80d21bcc363666a2f7d35ea [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa4a47112016-12-16 10:06:13 -080019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-resource';
22import 'angular-cookies';
23import {IXosResourceService} from './model.rest';
24import {xosDataSources} from '../index';
Matteo Scandoloa4a47112016-12-16 10:06:13 -080025
26let service: IXosResourceService;
27let resource: ng.resource.IResourceClass<any>;
28let httpBackend: ng.IHttpBackendService;
29let $resource;
30let $scope;
31
Matteo Scandolo828d1e82017-01-17 14:49:38 -080032const MockAppCfg = {
33 apiEndpoint: 'http://xos-test:3000/api',
34 websocketClient: 'http://xos-test:3000'
35};
36
Matteo Scandoloa4a47112016-12-16 10:06:13 -080037describe('The ModelRest service', () => {
38
39 beforeEach(angular.mock.module(xosDataSources));
40
41 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080042
43 angular.module(xosDataSources)
44 .constant('AppConfig', MockAppCfg);
45
Matteo Scandoloa4a47112016-12-16 10:06:13 -080046 angular.mock.module(xosDataSources);
47 });
48
49
50 beforeEach(angular.mock.inject((
51 ModelRest: IXosResourceService,
52 $httpBackend: ng.IHttpBackendService,
53 _$resource_: ng.resource.IResourceService,
54 _$rootScope_: ng.IRootScopeService
55 ) => {
56 service = ModelRest;
57 httpBackend = $httpBackend;
58 $resource = _$resource_;
59 $scope = _$rootScope_;
60 }));
61
62 it('should return a resource based on the URL', () => {
63 resource = service.getResource('/core/test');
64 expect(resource.constructor).toEqual($resource.constructor);
65 });
66
67 it('should have a query method', (done) => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080068 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080069 .respond([
70 {status: 'ok'}
71 ]);
72 resource = service.getResource('/core/test');
73 resource.query().$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
85 it('should have a get method', (done) => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080086 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test/1`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080087 .respond([
88 {status: 'ok'}
89 ]);
90 resource = service.getResource('/core/test');
91 resource.get({id: 1}).$promise
92 .then((res) => {
93 expect(res[0].status).toEqual('ok');
94 done();
95 })
96 .catch(e => {
97 done(e);
98 });
99 $scope.$apply();
100 httpBackend.flush();
101 });
102});