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