blob: f3b23215d40f769dab9914a887aa11f07083903d [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 Scandoloaa024ff2017-01-04 15:04:46 -080029
30let service: IStoreHelpersService;
31let subject: BehaviorSubject<any>;
32let resource: ng.resource.IResourceClass<any>;
33let $resource: ng.resource.IResourceService;
Matteo Scandolo63498472017-09-26 17:21:41 -070034let xosModelCache: IXosModeldefsCache;
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080035
36describe('The StoreHelpers service', () => {
37
38 beforeEach(() => {
39 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080040 .module('test', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080041 .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080042 .service('ModelRest', ModelRest) // NOTE evaluate mock
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080043 .service('StoreHelpers', StoreHelpers)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080044 .service('AuthService', AuthService)
Matteo Scandolo63498472017-09-26 17:21:41 -070045 .value('XosModeldefsCache', {
46 get: jasmine.createSpy('XosModeldefsCache.get'),
47 getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel')
48 })
Matteo Scandolo828d1e82017-01-17 14:49:38 -080049 .value('AppConfig', {});
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080050
51 angular.mock.module('test');
52 });
53
54 beforeEach(angular.mock.inject((
55 StoreHelpers: IStoreHelpersService,
Matteo Scandolo63498472017-09-26 17:21:41 -070056 XosModeldefsCache: IXosModeldefsCache,
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080057 _$resource_: ng.resource.IResourceService
58 ) => {
59 $resource = _$resource_;
60 resource = $resource('/test');
Matteo Scandolo63498472017-09-26 17:21:41 -070061 xosModelCache = XosModeldefsCache;
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080062 service = StoreHelpers;
63 }));
64
65 it('should have an update collection method', () => {
66 expect(service.updateCollection).toBeDefined();
67 });
68
Matteo Scandolo63498472017-09-26 17:21:41 -070069 describe('the updateCollection method', () => {
Matteo Scandolo04964232017-01-07 12:53:46 -080070
Matteo Scandolo63498472017-09-26 17:21:41 -070071 beforeEach(() => {
72 subject = new BehaviorSubject([
73 new resource({id: 1, name: 'test'})
74 ]);
75
76 });
77
78 it('should get the correct url for a core model', () => {
79 const mockModelDef = {
80 name: 'Node',
81 app: 'core'
82 };
83
84 xosModelCache.get['and'].returnValue(mockModelDef);
85
86 const event: IWSEvent = {
87 model: 'TestModel',
88 msg: {
89 object: {
90 id: 1,
91 name: 'test'
92 },
93 changed_fields: ['deleted']
94 }
95 };
96
97 service.updateCollection(event, subject);
98 expect(xosModelCache.get).toHaveBeenCalledWith('TestModel');
99 expect(xosModelCache.getApiUrlFromModel).toHaveBeenCalledWith(mockModelDef);
100 });
Matteo Scandolo04964232017-01-07 12:53:46 -0800101 });
102
Matteo Scandoloaa024ff2017-01-04 15:04:46 -0800103 describe('when updating a collection', () => {
104
105 beforeEach(() => {
106 subject = new BehaviorSubject([
107 new resource({id: 1, name: 'test'})
108 ]);
109 });
110
111 it('should remove a model if it has been deleted', () => {
112 const event: IWSEvent = {
113 model: 'Test',
114 msg: {
115 object: {
116 id: 1,
117 name: 'test'
118 },
119 changed_fields: ['deleted']
120 }
121 };
122 service.updateCollection(event, subject);
123 expect(subject.value.length).toBe(0);
124 });
125
126 it('should update a model if it has been updated', () => {
127 const event: IWSEvent = {
128 model: 'Test',
129 msg: {
130 object: {
131 id: 1,
132 name: 'test-updated'
133 },
134 changed_fields: ['name']
135 }
136 };
137 service.updateCollection(event, subject);
138 expect(subject.value.length).toBe(1);
139 expect(subject.value[0].name).toBe('test-updated');
140 });
141
142 it('should add a model if it has been created', () => {
143 const event: IWSEvent = {
144 model: 'Test',
145 msg: {
146 object: {
147 id: 2,
148 name: 'another-test'
149 },
150 changed_fields: ['created']
151 }
152 };
153 service.updateCollection(event, subject);
154 expect(subject.value.length).toBe(2);
155 expect(subject.value[0].name).toBe('test');
156 expect(subject.value[1].name).toBe('another-test');
157 });
158
159 describe('when adding a model', () => {
160
161 beforeEach(() => {
162 const event: IWSEvent = {
163 model: 'Test',
164 msg: {
165 object: {
166 id: 2,
167 name: 'another-test'
168 },
169 changed_fields: ['created']
170 }
171 };
172 service.updateCollection(event, subject);
173 });
174
175 it('should create a resource', () => {
176 expect(subject.value[1].$save).toBeDefined();
177 expect(subject.value[1].$delete).toBeDefined();
178 });
179
180 xit('should automatically create the appropriate resource', () => {
181 // TODO test that the url of the resource is the correct one,
182 // use httpbackend and mock a call?? any faster way??
183 });
184 });
185 });
186
187});