blob: 5d0a5847746dd808e09a1768e303e89f35bb6775 [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 Scandoloaa024ff2017-01-04 15:04:46 -080028
29let service: IStoreHelpersService;
30let subject: BehaviorSubject<any>;
31let resource: ng.resource.IResourceClass<any>;
32let $resource: ng.resource.IResourceService;
33
34describe('The StoreHelpers service', () => {
35
36 beforeEach(() => {
37 angular
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080038 .module('test', ['ngResource', 'toastr', 'ngCookies'])
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080039 .service('ConfigHelpers', ConfigHelpers) // NOTE evaluate mock
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080040 .service('ModelRest', ModelRest) // NOTE evaluate mock
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080041 .service('StoreHelpers', StoreHelpers)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080042 .service('AuthService', AuthService)
43 .value('AppConfig', {});
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080044
45 angular.mock.module('test');
46 });
47
48 beforeEach(angular.mock.inject((
49 StoreHelpers: IStoreHelpersService,
50 _$resource_: ng.resource.IResourceService
51 ) => {
52 $resource = _$resource_;
53 resource = $resource('/test');
54 service = StoreHelpers;
55 }));
56
57 it('should have an update collection method', () => {
58 expect(service.updateCollection).toBeDefined();
59 });
60
Matteo Scandolo04964232017-01-07 12:53:46 -080061
62 it('should convert a core model name in an URL', () => {
63 expect(service.urlFromCoreModel('Slice')).toBe('/core/slices');
Matteo Scandolo08464e52017-01-17 13:35:27 -080064 expect(service.urlFromCoreModel('Xos')).toBe('/core/xoses');
Matteo Scandolo1aee1982017-02-17 08:33:23 -080065 expect(service.urlFromCoreModel('SiteRole')).toBe('/core/siteroles');
66 expect(service.urlFromCoreModel('SliceRole')).toBe('/core/sliceroles');
67 expect(service.urlFromCoreModel('SlicePrivilege')).toBe('/core/sliceprivileges');
Matteo Scandolo04964232017-01-07 12:53:46 -080068 });
69
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080070 describe('when updating a collection', () => {
71
72 beforeEach(() => {
73 subject = new BehaviorSubject([
74 new resource({id: 1, name: 'test'})
75 ]);
76 });
77
78 it('should remove a model if it has been deleted', () => {
79 const event: IWSEvent = {
80 model: 'Test',
81 msg: {
82 object: {
83 id: 1,
84 name: 'test'
85 },
86 changed_fields: ['deleted']
87 }
88 };
89 service.updateCollection(event, subject);
90 expect(subject.value.length).toBe(0);
91 });
92
93 it('should update a model if it has been updated', () => {
94 const event: IWSEvent = {
95 model: 'Test',
96 msg: {
97 object: {
98 id: 1,
99 name: 'test-updated'
100 },
101 changed_fields: ['name']
102 }
103 };
104 service.updateCollection(event, subject);
105 expect(subject.value.length).toBe(1);
106 expect(subject.value[0].name).toBe('test-updated');
107 });
108
109 it('should add a model if it has been created', () => {
110 const event: IWSEvent = {
111 model: 'Test',
112 msg: {
113 object: {
114 id: 2,
115 name: 'another-test'
116 },
117 changed_fields: ['created']
118 }
119 };
120 service.updateCollection(event, subject);
121 expect(subject.value.length).toBe(2);
122 expect(subject.value[0].name).toBe('test');
123 expect(subject.value[1].name).toBe('another-test');
124 });
125
126 describe('when adding a model', () => {
127
128 beforeEach(() => {
129 const event: IWSEvent = {
130 model: 'Test',
131 msg: {
132 object: {
133 id: 2,
134 name: 'another-test'
135 },
136 changed_fields: ['created']
137 }
138 };
139 service.updateCollection(event, subject);
140 });
141
142 it('should create a resource', () => {
143 expect(subject.value[1].$save).toBeDefined();
144 expect(subject.value[1].$delete).toBeDefined();
145 });
146
147 xit('should automatically create the appropriate resource', () => {
148 // TODO test that the url of the resource is the correct one,
149 // use httpbackend and mock a call?? any faster way??
150 });
151 });
152 });
153
154});