blob: 09dc3f9b8e0b07999dae987d12b38fbb189b2282 [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';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080022import {IXosModelStoreService, XosModelStore} from './model.store';
Matteo Scandoloa4a47112016-12-16 10:06:13 -080023import {Subject} from 'rxjs';
24import {IWSEvent} from '../websocket/global';
25import {StoreHelpers} from '../helpers/store.helpers';
26import {ModelRest} from '../rest/model.rest';
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080027import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080028import {AuthService} from '../rest/auth.rest';
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080029import {XosDebouncer} from '../../core/services/helpers/debounce.helper';
Matteo Scandolo0e171f32017-09-26 17:21:41 -070030import {IXosModeldefsCache} from '../helpers/modeldefs.service';
Matteo Scandoloa4a47112016-12-16 10:06:13 -080031
Matteo Scandolo47860fe2017-02-02 12:05:55 -080032let service: IXosModelStoreService;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080033let httpBackend: ng.IHttpBackendService;
34let $scope;
35let WebSocket;
Matteo Scandolo0e171f32017-09-26 17:21:41 -070036let XosModeldefsCache;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080037
38class MockWs {
39 private _list;
40 constructor() {
41 this._list = new Subject<IWSEvent>();
42 }
43 list() {
44 return this._list.asObservable();
45 }
46
47 next(event: IWSEvent) {
48 this._list.next(event);
49 }
50}
51
52const queryData = [
53 {id: 1, name: 'foo'},
Matteo Scandolo5d962a32017-08-01 18:16:14 -070054 {id: 2, name: 'bar'}
Matteo Scandoloa4a47112016-12-16 10:06:13 -080055];
56
Matteo Scandolo828d1e82017-01-17 14:49:38 -080057const MockAppCfg = {
58 apiEndpoint: 'http://xos-test:3000/api',
59 websocketClient: 'http://xos-test:3000'
60};
61
Matteo Scandoloa4a47112016-12-16 10:06:13 -080062describe('The ModelStore service', () => {
63
64 beforeEach(() => {
65 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080066 .module('ModelStore', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandoloa4a47112016-12-16 10:06:13 -080067 .service('WebSocket', MockWs)
68 .service('StoreHelpers', StoreHelpers) // TODO mock
69 .service('ModelRest', ModelRest) // TODO mock
Matteo Scandolo1aee1982017-02-17 08:33:23 -080070 .service('XosModelStore', XosModelStore)
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080071 .service('ConfigHelpers', ConfigHelpers) // TODO mock
Matteo Scandolo828d1e82017-01-17 14:49:38 -080072 .service('AuthService', AuthService)
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080073 .constant('AppConfig', MockAppCfg)
Matteo Scandolo0e171f32017-09-26 17:21:41 -070074 .value('XosModeldefsCache', {
75 get: jasmine.createSpy('XosModeldefsCache.get').and.returnValue({}),
76 getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel')
77 })
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080078 .service('XosDebouncer', XosDebouncer);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080079
80 angular.mock.module('ModelStore');
81 });
82
83 beforeEach(angular.mock.inject((
Matteo Scandolo1aee1982017-02-17 08:33:23 -080084 XosModelStore: IXosModelStoreService,
Matteo Scandoloa4a47112016-12-16 10:06:13 -080085 $httpBackend: ng.IHttpBackendService,
86 _$rootScope_: ng.IRootScopeService,
Matteo Scandolo0e171f32017-09-26 17:21:41 -070087 _WebSocket_: any,
88 _XosModeldefsCache_: IXosModeldefsCache
Matteo Scandoloa4a47112016-12-16 10:06:13 -080089 ) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080090 service = XosModelStore;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080091 httpBackend = $httpBackend;
92 $scope = _$rootScope_;
93 WebSocket = _WebSocket_;
Matteo Scandolo0e171f32017-09-26 17:21:41 -070094 XosModeldefsCache = _XosModeldefsCache_;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080095
96 // ModelRest will call the backend
Matteo Scandolo828d1e82017-01-17 14:49:38 -080097 httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080098 .respond(queryData);
99 }));
100
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700101 describe('the QUERY method', () => {
102 it('should return an Observable', () => {
103 expect(typeof service.query('test').subscribe).toBe('function');
104 });
105
106 it('the first event should be the resource response', (done) => {
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700107 XosModeldefsCache.getApiUrlFromModel.and.returnValue(`/core/samples`);
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700108 let event = 0;
109 service.query('sample')
110 .subscribe(collection => {
111 event++;
112 if (event === 2) {
113 expect(collection[0].id).toEqual(queryData[0].id);
114 expect(collection[1].id).toEqual(queryData[1].id);
115 done();
116 }
117 });
118 $scope.$apply();
119 httpBackend.flush();
120 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800121 });
122
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700123 describe('the GET method', () => {
124 it('should return an observable containing a single model', (done) => {
125 let event = 0;
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700126 XosModeldefsCache.getApiUrlFromModel.and.returnValue(`/core/samples`);
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700127 service.get('sample', queryData[1].id)
128 .subscribe((model) => {
129 event++;
130 if (event === 2) {
131 expect(model.id).toEqual(queryData[1].id);
132 expect(model.name).toEqual(queryData[1].name);
133 done();
134 }
135 });
136 httpBackend.flush();
137 $scope.$apply();
138 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800139 });
140
141 describe('when a web-socket event is received for that model', () => {
142 it('should update the collection', (done) => {
143 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800144 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800145 .subscribe(
146 collection => {
147 event++;
148 if (event === 3) {
149 expect(collection[0].id).toEqual(queryData[0].id);
150 expect(collection[1].id).toEqual(queryData[1].id);
151 expect(collection[2].id).toEqual(3);
152 expect(collection[2].name).toEqual('baz');
153 done();
154 }
155 },
156 err => {
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800157 done(err);
158 }
159 );
160 window.setTimeout(() => {
161 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800162 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800163 msg: {
164 changed_fields: ['id'],
165 object: {id: 3, name: 'baz'},
166 pk: 3
167 }
168 });
169 }, 1);
170 $scope.$apply();
171 httpBackend.flush();
172 });
173 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800174
175 describe('when multiple stores are requested', () => {
176
177 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800178 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800179 .respond([
180 {first: 'foo'}
181 ]);
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800182 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800183 .respond([
184 {second: 'foo'}
185 ]);
186 });
187 it('should create different Subject', (done) => {
188 let fevent = 0;
189 let sevent = 0;
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700190 XosModeldefsCache.get.and.callFake(v => v);
191 XosModeldefsCache.getApiUrlFromModel.and.callFake(v => `/core/${v}s`);
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800192 service.query('first')
193 .subscribe(first => {
194 fevent++;
195 if (fevent >= 2) {
196 service.query('second')
197 .subscribe(second => {
198 sevent++;
199 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800200 expect(first).not.toEqual(second);
201 done();
202 }
203 });
204 }
205 });
206 $scope.$apply();
207 httpBackend.flush();
208 });
209 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800210});