blob: da4f2f4ffe1e4c0afec0541a01f3507ea4ae3ae3 [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 Scandolo3850d422017-11-02 12:45:37 +010025import {IXosFormHelpersService} from '../../core/form/form-helpers';
Matteo Scandoloa4a47112016-12-16 10:06:13 -080026
27let service: IXosResourceService;
28let resource: ng.resource.IResourceClass<any>;
29let httpBackend: ng.IHttpBackendService;
30let $resource;
31let $scope;
32
Matteo Scandolo828d1e82017-01-17 14:49:38 -080033const MockAppCfg = {
34 apiEndpoint: 'http://xos-test:3000/api',
35 websocketClient: 'http://xos-test:3000'
36};
37
Matteo Scandolo3850d422017-11-02 12:45:37 +010038const MockFormHelpers: IXosFormHelpersService = {
39 _getFieldFormat: () => 'date'
40};
41
Matteo Scandoloa4a47112016-12-16 10:06:13 -080042describe('The ModelRest service', () => {
43
44 beforeEach(angular.mock.module(xosDataSources));
45
46 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080047
48 angular.module(xosDataSources)
Matteo Scandolo3850d422017-11-02 12:45:37 +010049 .constant('AppConfig', MockAppCfg)
50 .value('XosFormHelpers', MockFormHelpers);
Matteo Scandolo828d1e82017-01-17 14:49:38 -080051
Matteo Scandoloa4a47112016-12-16 10:06:13 -080052 angular.mock.module(xosDataSources);
53 });
54
55
56 beforeEach(angular.mock.inject((
57 ModelRest: IXosResourceService,
58 $httpBackend: ng.IHttpBackendService,
59 _$resource_: ng.resource.IResourceService,
60 _$rootScope_: ng.IRootScopeService
61 ) => {
62 service = ModelRest;
63 httpBackend = $httpBackend;
64 $resource = _$resource_;
65 $scope = _$rootScope_;
66 }));
67
68 it('should return a resource based on the URL', () => {
69 resource = service.getResource('/core/test');
70 expect(resource.constructor).toEqual($resource.constructor);
71 });
72
73 it('should have a query method', (done) => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080074 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080075 .respond([
76 {status: 'ok'}
77 ]);
78 resource = service.getResource('/core/test');
79 resource.query().$promise
80 .then((res) => {
81 expect(res[0].status).toEqual('ok');
82 done();
83 })
84 .catch(e => {
85 done(e);
86 });
87 $scope.$apply();
88 httpBackend.flush();
89 });
90
91 it('should have a get method', (done) => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080092 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/test/1`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080093 .respond([
94 {status: 'ok'}
95 ]);
96 resource = service.getResource('/core/test');
97 resource.get({id: 1}).$promise
98 .then((res) => {
99 expect(res[0].status).toEqual('ok');
100 done();
101 })
102 .catch(e => {
103 done(e);
104 });
105 $scope.$apply();
106 httpBackend.flush();
107 });
Matteo Scandolo3850d422017-11-02 12:45:37 +0100108
109 describe('when saving a model', () => {
110
111 let item, date;
112 const timestamp = 1509552402000;
113
114 beforeEach(() => {
115 httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/core/test`)
116 .respond((method, url, req) => {
117 return [200, req];
118 });
119 resource = service.getResource('/core/test');
120 date = new Date(timestamp);
121 item = new resource({date: date.toString()});
122 });
123
124 xit('should convert dates to timestamps', (done) => {
125 item.$save()
126 .then(res => {
127 expect(res.date).toEqual(timestamp);
128 done();
129 });
130 $scope.$apply();
131 httpBackend.flush();
132 done();
133 });
134 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800135});