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