Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 18 | import * as angular from 'angular'; |
| 19 | import 'angular-mocks'; |
| 20 | import 'angular-ui-router'; |
| 21 | import {XosModelDiscovererService, IXosModelDiscovererService} from './model-discoverer.service'; |
| 22 | import {IXosModeldef} from '../rest/modeldefs.rest'; |
| 23 | import {BehaviorSubject} from 'rxjs'; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 24 | import {XosModeldefsCache} from './modeldefs.service'; |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 25 | |
| 26 | const stubModels: IXosModeldef[] = [ |
| 27 | { |
| 28 | fields: [ |
| 29 | {name: 'id', type: 'number'}, |
| 30 | {name: 'foo', type: 'string'} |
| 31 | ], |
| 32 | relations: [], |
| 33 | name: 'Node', |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 34 | app: 'core', |
| 35 | description: '', |
| 36 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 37 | }, |
| 38 | { |
| 39 | fields: [ |
| 40 | {name: 'id', type: 'number'}, |
| 41 | {name: 'bar', type: 'string'} |
| 42 | ], |
| 43 | relations: [], |
| 44 | name: 'VSGTenant', |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 45 | app: 'service.vsg', |
| 46 | description: '', |
| 47 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 48 | } |
| 49 | ]; |
| 50 | |
| 51 | let service: IXosModelDiscovererService; |
| 52 | let scope: ng.IScope; |
| 53 | const MockXosModelDefs = { |
| 54 | get: null |
| 55 | }; |
| 56 | let MockXosRuntimeStates = { |
| 57 | addState: jasmine.createSpy('runtimeState.addState') |
| 58 | .and.callFake(() => true) |
| 59 | }; |
| 60 | let MockConfigHelpers = { |
| 61 | pluralize: jasmine.createSpy('config.pluralize') |
| 62 | .and.callFake((string: string) => `${string}s`), |
| 63 | toLabel: jasmine.createSpy('config.toLabel') |
| 64 | .and.callFake((string: string) => string.toLowerCase()), |
| 65 | modelToTableCfg: jasmine.createSpy('config.modelToTableCfg') |
| 66 | .and.callFake(() => true), |
| 67 | modelToFormCfg: jasmine.createSpy('config.modelToFormCfg') |
| 68 | .and.callFake(() => true), |
| 69 | }; |
| 70 | let MockXosNavigationService = { |
| 71 | add: jasmine.createSpy('navigationService.add') |
| 72 | .and.callFake(() => true) |
| 73 | }; |
| 74 | const MockXosModelStore = { |
| 75 | query: jasmine.createSpy('modelStore.query') |
| 76 | .and.callFake(() => { |
| 77 | const list = new BehaviorSubject([]); |
| 78 | list.next([]); |
| 79 | return list.asObservable(); |
| 80 | }) |
| 81 | }; |
| 82 | const MockProgressBar = { |
| 83 | setColor: jasmine.createSpy('progressBar.setColor'), |
| 84 | start: jasmine.createSpy('progressBar.start'), |
| 85 | complete: jasmine.createSpy('progressBar.complete') |
| 86 | }; |
| 87 | const MockngProgressFactory = { |
| 88 | createInstance: jasmine.createSpy('ngProgress.createInstance') |
| 89 | .and.callFake(() => MockProgressBar) |
| 90 | }; |
| 91 | |
| 92 | describe('The ModelDicoverer service', () => { |
| 93 | |
| 94 | beforeEach(() => { |
| 95 | angular |
| 96 | .module('test', []) |
| 97 | .service('XosModelDiscoverer', XosModelDiscovererService) |
| 98 | .value('ConfigHelpers', MockConfigHelpers) |
| 99 | .value('XosModelDefs', MockXosModelDefs) |
| 100 | .value('XosRuntimeStates', MockXosRuntimeStates) |
| 101 | .value('XosModelStore', MockXosModelStore) |
| 102 | .value('ngProgressFactory', MockngProgressFactory) |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 103 | .value('XosNavigationService', MockXosNavigationService) |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 104 | .value('AuthService', {}) |
| 105 | .service('XosModeldefsCache', XosModeldefsCache); |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 106 | |
| 107 | angular.mock.module('test'); |
| 108 | }); |
| 109 | |
| 110 | beforeEach(angular.mock.inject(( |
| 111 | XosModelDiscoverer: IXosModelDiscovererService, |
| 112 | $rootScope: ng.IScope, |
| 113 | $q: ng.IQService |
| 114 | ) => { |
| 115 | service = XosModelDiscoverer; |
| 116 | scope = $rootScope; |
| 117 | MockXosModelDefs.get = jasmine.createSpy('modelDefs.get') |
| 118 | .and.callFake(() => { |
| 119 | const d = $q.defer(); |
| 120 | d.resolve(stubModels); |
| 121 | return d.promise; |
| 122 | }); |
| 123 | })); |
| 124 | |
| 125 | it('should setup the progress bar', () => { |
| 126 | expect(MockngProgressFactory.createInstance).toHaveBeenCalled(); |
| 127 | expect(MockProgressBar.setColor).toHaveBeenCalled(); |
| 128 | }); |
| 129 | |
| 130 | it('should not have loaded models', () => { |
| 131 | expect(service.areModelsLoaded()).toBeFalsy(); |
| 132 | }); |
| 133 | |
| 134 | it('should get the url from a core model', () => { |
| 135 | const model = { |
| 136 | name: 'Node', |
| 137 | app: 'core', |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 138 | fields: [], |
| 139 | description: '', |
| 140 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 141 | }; |
| 142 | expect(service.getApiUrlFromModel(model)).toBe('/core/nodes'); |
| 143 | }); |
| 144 | |
| 145 | it('should get the url from a service model', () => { |
| 146 | const model = { |
| 147 | name: 'Tenant', |
| 148 | app: 'services.test', |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 149 | fields: [], |
| 150 | description: '', |
| 151 | verbose_name: '' |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 152 | }; |
| 153 | expect(service.getApiUrlFromModel(model)).toBe('/test/tenants'); |
| 154 | }); |
| 155 | |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 156 | it('should get the state name from the model', () => { |
| 157 | expect(service['stateNameFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg.tenant'); |
| 158 | }); |
| 159 | |
| 160 | it('should get the parent state name from a core model', () => { |
| 161 | expect(service['getParentStateFromModel']({name: 'Nodes', app: 'core'})).toBe('xos.core'); |
| 162 | }); |
| 163 | |
| 164 | it('should get the parent state name from a service model', () => { |
| 165 | expect(service['getParentStateFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg'); |
| 166 | }); |
| 167 | |
| 168 | it('should add a new service entry in the system', () => { |
| 169 | service['addService']({name: 'Tenant', app: 'services.vsg'}); |
| 170 | expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg', { |
| 171 | url: 'vsg', |
| 172 | parent: 'xos', |
| 173 | abstract: true, |
| 174 | template: '<div ui-view></div>' |
| 175 | }); |
| 176 | expect(MockXosNavigationService.add).toHaveBeenCalledWith({ |
| 177 | label: 'vsg', |
| 178 | state: 'xos.vsg' |
| 179 | }); |
| 180 | expect(service['xosServices'][0]).toEqual('vsg'); |
| 181 | expect(service['xosServices'].length).toBe(1); |
| 182 | }); |
| 183 | |
| 184 | it('should add a state in the system', (done) => { |
| 185 | MockXosRuntimeStates.addState.calls.reset(); |
| 186 | service['addState']({name: 'Tenant', app: 'services.vsg'}) |
| 187 | .then((model) => { |
| 188 | expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', { |
| 189 | parent: 'xos.vsg', |
| 190 | url: '/tenants/:id?', |
| 191 | params: { |
| 192 | id: null |
| 193 | }, |
| 194 | data: { |
| 195 | model: 'Tenant' |
| 196 | }, |
| 197 | component: 'xosCrud', |
| 198 | }); |
| 199 | expect(model.clientUrl).toBe('vsg/tenants/:id?'); |
| 200 | done(); |
| 201 | }); |
| 202 | scope.$apply(); |
| 203 | }); |
| 204 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 205 | it('should add a state with relations in the system', (done) => { |
| 206 | MockXosRuntimeStates.addState.calls.reset(); |
| 207 | service['addState']({name: 'Tenant', app: 'services.vsg', relations: [{model: 'Something', type: 'manytoone'}]}) |
| 208 | .then((model) => { |
| 209 | expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', { |
| 210 | parent: 'xos.vsg', |
| 211 | url: '/tenants/:id?', |
| 212 | params: { |
| 213 | id: null |
| 214 | }, |
| 215 | data: { |
| 216 | model: 'Tenant', |
| 217 | relations: [ |
| 218 | {model: 'Something', type: 'manytoone'} |
| 219 | ] |
| 220 | }, |
| 221 | component: 'xosCrud', |
| 222 | }); |
| 223 | expect(model.clientUrl).toBe('vsg/tenants/:id?'); |
| 224 | done(); |
| 225 | }); |
| 226 | scope.$apply(); |
| 227 | }); |
| 228 | |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 229 | it('should add an item to navigation', () => { |
| 230 | service['addNavItem']({name: 'Tenant', app: 'services.vsg'}); |
| 231 | expect(MockXosNavigationService.add).toHaveBeenCalledWith({ |
| 232 | label: 'Tenants', |
| 233 | state: 'xos.vsg.tenant', |
| 234 | parent: 'xos.vsg' |
| 235 | }); |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 236 | |
| 237 | service['addNavItem']({name: 'Tenant', verbose_name: 'Verbose', app: 'services.vsg'}); |
| 238 | expect(MockXosNavigationService.add).toHaveBeenCalledWith({ |
| 239 | label: 'Verboses', |
| 240 | state: 'xos.vsg.tenant', |
| 241 | parent: 'xos.vsg' |
| 242 | }); |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 243 | }); |
| 244 | |
| 245 | it('should cache a model', () => { |
| 246 | service['cacheModelEntries']({name: 'Tenant', app: 'services.vsg'}); |
| 247 | expect(MockXosModelStore.query).toHaveBeenCalledWith('Tenant', '/vsg/tenants'); |
| 248 | }); |
| 249 | |
| 250 | it('should get the table config', () => { |
| 251 | service['getTableCfg']({name: 'Tenant', app: 'services.vsg'}); |
| 252 | expect(MockConfigHelpers.modelToTableCfg).toHaveBeenCalledWith( |
| 253 | {name: 'Tenant', app: 'services.vsg', tableCfg: true}, |
| 254 | 'xos.vsg.tenant' |
| 255 | ); |
| 256 | }); |
| 257 | |
| 258 | it('should get the form config', () => { |
| 259 | service['getFormCfg']({name: 'Tenant', app: 'services.vsg'}); |
| 260 | expect(MockConfigHelpers.modelToFormCfg).toHaveBeenCalledWith( |
| 261 | {name: 'Tenant', app: 'services.vsg', formCfg: true} |
| 262 | ); |
| 263 | }); |
| 264 | |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 265 | describe('when discovering models', () => { |
| 266 | beforeEach(() => { |
| 267 | spyOn(service, 'cacheModelEntries').and.callThrough(); |
| 268 | spyOn(service, 'addState').and.callThrough(); |
| 269 | spyOn(service, 'addNavItem').and.callThrough(); |
| 270 | spyOn(service, 'getTableCfg').and.callThrough(); |
| 271 | spyOn(service, 'getFormCfg').and.callThrough(); |
| 272 | spyOn(service, 'storeModel').and.callThrough(); |
| 273 | }); |
| 274 | |
| 275 | it('should call all the function chain', (done) => { |
| 276 | service.discover() |
| 277 | .then((res) => { |
| 278 | expect(MockProgressBar.start).toHaveBeenCalled(); |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame^] | 279 | expect(MockXosModelDefs.get).toHaveBeenCalled(); // FIXME replace correct spy |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 280 | expect(service['cacheModelEntries'].calls.count()).toBe(2); |
| 281 | expect(service['addState'].calls.count()).toBe(2); |
| 282 | expect(service['addNavItem'].calls.count()).toBe(2); |
| 283 | expect(service['getTableCfg'].calls.count()).toBe(2); |
| 284 | expect(service['getFormCfg'].calls.count()).toBe(2); |
| 285 | expect(service['storeModel'].calls.count()).toBe(2); |
| 286 | expect(res).toBeTruthy(); |
| 287 | done(); |
| 288 | }); |
| 289 | scope.$apply(); |
| 290 | }); |
| 291 | }); |
| 292 | }); |