blob: 80e7dc14287165bcdb37154a65a33b24c6349318 [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 Scandolo63498472017-09-26 17:21:41 -070030import {IXosModeldefsCache} from '../helpers/modeldefs.service';
Matteo Scandoloa1654572017-11-02 12:45:37 +010031import {XosFormHelpers} from '../../core/form/form-helpers';
Matteo Scandoloa4a47112016-12-16 10:06:13 -080032
Matteo Scandolo47860fe2017-02-02 12:05:55 -080033let service: IXosModelStoreService;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080034let httpBackend: ng.IHttpBackendService;
35let $scope;
36let WebSocket;
Matteo Scandolo63498472017-09-26 17:21:41 -070037let XosModeldefsCache;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080038
39class MockWs {
40 private _list;
41 constructor() {
42 this._list = new Subject<IWSEvent>();
43 }
44 list() {
45 return this._list.asObservable();
46 }
47
48 next(event: IWSEvent) {
49 this._list.next(event);
50 }
51}
52
53const queryData = [
54 {id: 1, name: 'foo'},
Matteo Scandolo5d962a32017-08-01 18:16:14 -070055 {id: 2, name: 'bar'}
Matteo Scandoloa4a47112016-12-16 10:06:13 -080056];
57
Matteo Scandolo828d1e82017-01-17 14:49:38 -080058const MockAppCfg = {
59 apiEndpoint: 'http://xos-test:3000/api',
60 websocketClient: 'http://xos-test:3000'
61};
62
Matteo Scandoloa4a47112016-12-16 10:06:13 -080063describe('The ModelStore service', () => {
64
65 beforeEach(() => {
66 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080067 .module('ModelStore', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandoloa4a47112016-12-16 10:06:13 -080068 .service('WebSocket', MockWs)
69 .service('StoreHelpers', StoreHelpers) // TODO mock
70 .service('ModelRest', ModelRest) // TODO mock
Matteo Scandolo1aee1982017-02-17 08:33:23 -080071 .service('XosModelStore', XosModelStore)
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080072 .service('ConfigHelpers', ConfigHelpers) // TODO mock
Matteo Scandolo828d1e82017-01-17 14:49:38 -080073 .service('AuthService', AuthService)
Matteo Scandoloa1654572017-11-02 12:45:37 +010074 .service('XosFormHelpers', XosFormHelpers)
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080075 .constant('AppConfig', MockAppCfg)
Matteo Scandolo63498472017-09-26 17:21:41 -070076 .value('XosModeldefsCache', {
77 get: jasmine.createSpy('XosModeldefsCache.get').and.returnValue({}),
78 getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel')
79 })
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080080 .service('XosDebouncer', XosDebouncer);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080081
82 angular.mock.module('ModelStore');
83 });
84
85 beforeEach(angular.mock.inject((
Matteo Scandolo1aee1982017-02-17 08:33:23 -080086 XosModelStore: IXosModelStoreService,
Matteo Scandoloa4a47112016-12-16 10:06:13 -080087 $httpBackend: ng.IHttpBackendService,
88 _$rootScope_: ng.IRootScopeService,
Matteo Scandolo63498472017-09-26 17:21:41 -070089 _WebSocket_: any,
90 _XosModeldefsCache_: IXosModeldefsCache
Matteo Scandoloa4a47112016-12-16 10:06:13 -080091 ) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080092 service = XosModelStore;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080093 httpBackend = $httpBackend;
94 $scope = _$rootScope_;
95 WebSocket = _WebSocket_;
Matteo Scandolo63498472017-09-26 17:21:41 -070096 XosModeldefsCache = _XosModeldefsCache_;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080097
98 // ModelRest will call the backend
Matteo Scandolo828d1e82017-01-17 14:49:38 -080099 httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800100 .respond(queryData);
101 }));
102
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700103 describe('the QUERY method', () => {
104 it('should return an Observable', () => {
105 expect(typeof service.query('test').subscribe).toBe('function');
106 });
107
108 it('the first event should be the resource response', (done) => {
Matteo Scandolo63498472017-09-26 17:21:41 -0700109 XosModeldefsCache.getApiUrlFromModel.and.returnValue(`/core/samples`);
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700110 let event = 0;
111 service.query('sample')
112 .subscribe(collection => {
113 event++;
114 if (event === 2) {
115 expect(collection[0].id).toEqual(queryData[0].id);
116 expect(collection[1].id).toEqual(queryData[1].id);
117 done();
118 }
119 });
120 $scope.$apply();
121 httpBackend.flush();
122 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800123 });
124
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700125 describe('the GET method', () => {
126 it('should return an observable containing a single model', (done) => {
127 let event = 0;
Matteo Scandolo63498472017-09-26 17:21:41 -0700128 XosModeldefsCache.getApiUrlFromModel.and.returnValue(`/core/samples`);
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700129 service.get('sample', queryData[1].id)
130 .subscribe((model) => {
131 event++;
132 if (event === 2) {
133 expect(model.id).toEqual(queryData[1].id);
134 expect(model.name).toEqual(queryData[1].name);
135 done();
136 }
137 });
138 httpBackend.flush();
139 $scope.$apply();
140 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800141 });
142
143 describe('when a web-socket event is received for that model', () => {
144 it('should update the collection', (done) => {
145 let event = 0;
Matteo Scandolob310f3c2019-06-20 13:50:31 -0700146 service.query('sample', '/core/samples')
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800147 .subscribe(
148 collection => {
149 event++;
150 if (event === 3) {
151 expect(collection[0].id).toEqual(queryData[0].id);
152 expect(collection[1].id).toEqual(queryData[1].id);
153 expect(collection[2].id).toEqual(3);
154 expect(collection[2].name).toEqual('baz');
155 done();
156 }
157 },
158 err => {
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800159 done(err);
160 }
161 );
162 window.setTimeout(() => {
163 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800164 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800165 msg: {
166 changed_fields: ['id'],
167 object: {id: 3, name: 'baz'},
168 pk: 3
169 }
170 });
171 }, 1);
172 $scope.$apply();
173 httpBackend.flush();
174 });
175 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800176
177 describe('when multiple stores are requested', () => {
178
179 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800180 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800181 .respond([
182 {first: 'foo'}
183 ]);
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800184 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800185 .respond([
186 {second: 'foo'}
187 ]);
188 });
189 it('should create different Subject', (done) => {
190 let fevent = 0;
191 let sevent = 0;
Matteo Scandolo63498472017-09-26 17:21:41 -0700192 XosModeldefsCache.get.and.callFake(v => v);
193 XosModeldefsCache.getApiUrlFromModel.and.callFake(v => `/core/${v}s`);
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800194 service.query('first')
195 .subscribe(first => {
196 fevent++;
197 if (fevent >= 2) {
198 service.query('second')
199 .subscribe(second => {
200 sevent++;
201 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800202 expect(first).not.toEqual(second);
203 done();
204 }
205 });
206 }
207 });
208 $scope.$apply();
209 httpBackend.flush();
210 });
211 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800212});