blob: 4e94495a2c6efac30807054b0b4464f2ff3ce857 [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 Scandoloa4a47112016-12-16 10:06:13 -080030
Matteo Scandolo47860fe2017-02-02 12:05:55 -080031let service: IXosModelStoreService;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080032let httpBackend: ng.IHttpBackendService;
33let $scope;
34let WebSocket;
35
36class MockWs {
37 private _list;
38 constructor() {
39 this._list = new Subject<IWSEvent>();
40 }
41 list() {
42 return this._list.asObservable();
43 }
44
45 next(event: IWSEvent) {
46 this._list.next(event);
47 }
48}
49
50const queryData = [
51 {id: 1, name: 'foo'},
Matteo Scandolo5d962a32017-08-01 18:16:14 -070052 {id: 2, name: 'bar'}
Matteo Scandoloa4a47112016-12-16 10:06:13 -080053];
54
Matteo Scandolo828d1e82017-01-17 14:49:38 -080055const MockAppCfg = {
56 apiEndpoint: 'http://xos-test:3000/api',
57 websocketClient: 'http://xos-test:3000'
58};
59
Matteo Scandoloa4a47112016-12-16 10:06:13 -080060describe('The ModelStore service', () => {
61
62 beforeEach(() => {
63 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080064 .module('ModelStore', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandoloa4a47112016-12-16 10:06:13 -080065 .service('WebSocket', MockWs)
66 .service('StoreHelpers', StoreHelpers) // TODO mock
67 .service('ModelRest', ModelRest) // TODO mock
Matteo Scandolo1aee1982017-02-17 08:33:23 -080068 .service('XosModelStore', XosModelStore)
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080069 .service('ConfigHelpers', ConfigHelpers) // TODO mock
Matteo Scandolo828d1e82017-01-17 14:49:38 -080070 .service('AuthService', AuthService)
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080071 .constant('AppConfig', MockAppCfg)
72 .service('XosDebouncer', XosDebouncer);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080073
74 angular.mock.module('ModelStore');
75 });
76
77 beforeEach(angular.mock.inject((
Matteo Scandolo1aee1982017-02-17 08:33:23 -080078 XosModelStore: IXosModelStoreService,
Matteo Scandoloa4a47112016-12-16 10:06:13 -080079 $httpBackend: ng.IHttpBackendService,
80 _$rootScope_: ng.IRootScopeService,
81 _WebSocket_: any
82 ) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080083 service = XosModelStore;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080084 httpBackend = $httpBackend;
85 $scope = _$rootScope_;
86 WebSocket = _WebSocket_;
87
88 // ModelRest will call the backend
Matteo Scandolo828d1e82017-01-17 14:49:38 -080089 httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080090 .respond(queryData);
91 }));
92
Matteo Scandolo5d962a32017-08-01 18:16:14 -070093 describe('the QUERY method', () => {
94 it('should return an Observable', () => {
95 expect(typeof service.query('test').subscribe).toBe('function');
96 });
97
98 it('the first event should be the resource response', (done) => {
99 let event = 0;
100 service.query('sample')
101 .subscribe(collection => {
102 event++;
103 if (event === 2) {
104 expect(collection[0].id).toEqual(queryData[0].id);
105 expect(collection[1].id).toEqual(queryData[1].id);
106 done();
107 }
108 });
109 $scope.$apply();
110 httpBackend.flush();
111 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800112 });
113
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700114 describe('the GET method', () => {
115 it('should return an observable containing a single model', (done) => {
116 let event = 0;
117 service.get('sample', queryData[1].id)
118 .subscribe((model) => {
119 event++;
120 if (event === 2) {
121 expect(model.id).toEqual(queryData[1].id);
122 expect(model.name).toEqual(queryData[1].name);
123 done();
124 }
125 });
126 httpBackend.flush();
127 $scope.$apply();
128 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800129 });
130
131 describe('when a web-socket event is received for that model', () => {
132 it('should update the collection', (done) => {
133 let event = 0;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800134 service.query('sample')
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800135 .subscribe(
136 collection => {
137 event++;
138 if (event === 3) {
139 expect(collection[0].id).toEqual(queryData[0].id);
140 expect(collection[1].id).toEqual(queryData[1].id);
141 expect(collection[2].id).toEqual(3);
142 expect(collection[2].name).toEqual('baz');
143 done();
144 }
145 },
146 err => {
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800147 done(err);
148 }
149 );
150 window.setTimeout(() => {
151 WebSocket.next({
Matteo Scandolod58d5042016-12-16 16:59:21 -0800152 model: 'sample',
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800153 msg: {
154 changed_fields: ['id'],
155 object: {id: 3, name: 'baz'},
156 pk: 3
157 }
158 });
159 }, 1);
160 $scope.$apply();
161 httpBackend.flush();
162 });
163 });
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800164
165 describe('when multiple stores are requested', () => {
166
167 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800168 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/firsts`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800169 .respond([
170 {first: 'foo'}
171 ]);
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800172 httpBackend.expectGET(`${MockAppCfg.apiEndpoint}/core/seconds`)
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800173 .respond([
174 {second: 'foo'}
175 ]);
176 });
177 it('should create different Subject', (done) => {
178 let fevent = 0;
179 let sevent = 0;
180 service.query('first')
181 .subscribe(first => {
182 fevent++;
183 if (fevent >= 2) {
184 service.query('second')
185 .subscribe(second => {
186 sevent++;
187 if (sevent === 2) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800188 expect(first).not.toEqual(second);
189 done();
190 }
191 });
192 }
193 });
194 $scope.$apply();
195 httpBackend.flush();
196 });
197 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800198});