blob: 9c4cfc6782107eb71aa5d5395d7f1f60bc43669b [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 Scandoloaa024ff2017-01-04 15:04:46 -080019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-ui-router';
22import {StoreHelpers, IStoreHelpersService} from './store.helpers';
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080023import {ModelRest} from '../rest/model.rest';
24import {BehaviorSubject} from 'rxjs';
25import {IWSEvent} from '../websocket/global';
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080026import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080027import {AuthService} from '../rest/auth.rest';
Matteo Scandolo63498472017-09-26 17:21:41 -070028import {IXosModeldefsCache} from './modeldefs.service';
Matteo Scandoloa1654572017-11-02 12:45:37 +010029import {XosFormHelpers} from '../../core/form/form-helpers';
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080030
31let service: IStoreHelpersService;
32let subject: BehaviorSubject<any>;
33let resource: ng.resource.IResourceClass<any>;
34let $resource: ng.resource.IResourceService;
Matteo Scandolo63498472017-09-26 17:21:41 -070035let xosModelCache: IXosModeldefsCache;
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -080036let $log: ng.ILogService;
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080037
38describe('The StoreHelpers service', () => {
39
40 beforeEach(() => {
41 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080042 .module('test', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080043 .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080044 .service('ModelRest', ModelRest) // NOTE evaluate mock
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080045 .service('StoreHelpers', StoreHelpers)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080046 .service('AuthService', AuthService)
Matteo Scandoloa1654572017-11-02 12:45:37 +010047 .service('XosFormHelpers', XosFormHelpers)
Matteo Scandolo63498472017-09-26 17:21:41 -070048 .value('XosModeldefsCache', {
49 get: jasmine.createSpy('XosModeldefsCache.get'),
50 getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel')
51 })
Matteo Scandolo828d1e82017-01-17 14:49:38 -080052 .value('AppConfig', {});
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080053
54 angular.mock.module('test');
55 });
56
57 beforeEach(angular.mock.inject((
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -080058
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080059 StoreHelpers: IStoreHelpersService,
Matteo Scandolo63498472017-09-26 17:21:41 -070060 XosModeldefsCache: IXosModeldefsCache,
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -080061 _$resource_: ng.resource.IResourceService,
62 _$log_: ng.ILogService
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080063 ) => {
64 $resource = _$resource_;
65 resource = $resource('/test');
Matteo Scandolo63498472017-09-26 17:21:41 -070066 xosModelCache = XosModeldefsCache;
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080067 service = StoreHelpers;
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -080068 $log = _$log_;
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080069 }));
70
71 it('should have an update collection method', () => {
72 expect(service.updateCollection).toBeDefined();
73 });
74
Matteo Scandolo63498472017-09-26 17:21:41 -070075 describe('the updateCollection method', () => {
Matteo Scandolo04964232017-01-07 12:53:46 -080076
Matteo Scandolo63498472017-09-26 17:21:41 -070077 beforeEach(() => {
78 subject = new BehaviorSubject([
79 new resource({id: 1, name: 'test'})
80 ]);
81
82 });
83
84 it('should get the correct url for a core model', () => {
85 const mockModelDef = {
86 name: 'Node',
87 app: 'core'
88 };
89
90 xosModelCache.get['and'].returnValue(mockModelDef);
91
92 const event: IWSEvent = {
93 model: 'TestModel',
94 msg: {
95 object: {
96 id: 1,
97 name: 'test'
98 },
99 changed_fields: ['deleted']
100 }
101 };
102
103 service.updateCollection(event, subject);
104 expect(xosModelCache.get).toHaveBeenCalledWith('TestModel');
105 expect(xosModelCache.getApiUrlFromModel).toHaveBeenCalledWith(mockModelDef);
106 });
Matteo Scandolo04964232017-01-07 12:53:46 -0800107 });
108
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800109 describe('when removing an item from a collection', () => {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -0800110 beforeEach(() => {
111 subject = new BehaviorSubject([
112 new resource({id: 1, name: 'test'})
113 ]);
114 });
115
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800116 describe('the updateCollection method', () => {
117 beforeEach(() => {
118 spyOn($log, 'error');
119 });
120
121 it('should log an error if called with a delete event', () => {
122 const event: IWSEvent = {
123 deleted: true,
124 model: 'Deleted',
125 msg: {
126 changed_fields: []
127 }
128 };
129 service.updateCollection(event, subject);
130 expect($log.error).toHaveBeenCalled();
131 });
132 });
133
Matteo Scandoloaa024ff2017-01-04 15:04:46 -0800134 it('should remove a model if it has been deleted', () => {
135 const event: IWSEvent = {
136 model: 'Test',
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800137 deleted: true,
Matteo Scandoloaa024ff2017-01-04 15:04:46 -0800138 msg: {
139 object: {
140 id: 1,
141 name: 'test'
142 },
143 changed_fields: ['deleted']
144 }
145 };
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800146 service.removeItemFromCollection(event, subject);
Matteo Scandoloaa024ff2017-01-04 15:04:46 -0800147 expect(subject.value.length).toBe(0);
148 });
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800149 });
150
151 describe('when updating a collection', () => {
152
153 beforeEach(() => {
154 subject = new BehaviorSubject([
155 new resource({id: 1, name: 'test'})
156 ]);
157 });
158
159 describe('the removeItemFromCollection method', () => {
160 beforeEach(() => {
161 spyOn($log, 'error');
162 });
163
164 it('should log an error if called with an update event', () => {
165 const event: IWSEvent = {
166 model: 'Deleted',
167 msg: {
168 changed_fields: []
169 }
170 };
171 service.removeItemFromCollection(event, subject);
172 expect($log.error).toHaveBeenCalled();
173 });
174 });
Matteo Scandoloaa024ff2017-01-04 15:04:46 -0800175
176 it('should update a model if it has been updated', () => {
177 const event: IWSEvent = {
178 model: 'Test',
179 msg: {
180 object: {
181 id: 1,
182 name: 'test-updated'
183 },
184 changed_fields: ['name']
185 }
186 };
187 service.updateCollection(event, subject);
188 expect(subject.value.length).toBe(1);
189 expect(subject.value[0].name).toBe('test-updated');
190 });
191
192 it('should add a model if it has been created', () => {
193 const event: IWSEvent = {
194 model: 'Test',
195 msg: {
196 object: {
197 id: 2,
198 name: 'another-test'
199 },
200 changed_fields: ['created']
201 }
202 };
203 service.updateCollection(event, subject);
204 expect(subject.value.length).toBe(2);
205 expect(subject.value[0].name).toBe('test');
206 expect(subject.value[1].name).toBe('another-test');
207 });
208
209 describe('when adding a model', () => {
210
211 beforeEach(() => {
212 const event: IWSEvent = {
213 model: 'Test',
214 msg: {
215 object: {
216 id: 2,
217 name: 'another-test'
218 },
219 changed_fields: ['created']
220 }
221 };
222 service.updateCollection(event, subject);
223 });
224
225 it('should create a resource', () => {
226 expect(subject.value[1].$save).toBeDefined();
227 expect(subject.value[1].$delete).toBeDefined();
228 });
229
230 xit('should automatically create the appropriate resource', () => {
231 // TODO test that the url of the resource is the correct one,
232 // use httpbackend and mock a call?? any faster way??
233 });
234 });
235 });
236
237});