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