blob: b72b311b1a84b7cc6dd445ee1dcf71cb176667fc [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)
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070081 .value('XosNavigationService', MockXosNavigationService)
82 .value('AuthService', {});
Matteo Scandolo02229382017-04-18 11:52:23 -070083
84 angular.mock.module('test');
85 });
86
87 beforeEach(angular.mock.inject((
88 XosModelDiscoverer: IXosModelDiscovererService,
89 $rootScope: ng.IScope,
90 $q: ng.IQService
91 ) => {
92 service = XosModelDiscoverer;
93 scope = $rootScope;
94 MockXosModelDefs.get = jasmine.createSpy('modelDefs.get')
95 .and.callFake(() => {
96 const d = $q.defer();
97 d.resolve(stubModels);
98 return d.promise;
99 });
100 }));
101
102 it('should setup the progress bar', () => {
103 expect(MockngProgressFactory.createInstance).toHaveBeenCalled();
104 expect(MockProgressBar.setColor).toHaveBeenCalled();
105 });
106
107 it('should not have loaded models', () => {
108 expect(service.areModelsLoaded()).toBeFalsy();
109 });
110
111 it('should get the url from a core model', () => {
112 const model = {
113 name: 'Node',
114 app: 'core',
115 fields: []
116 };
117 expect(service.getApiUrlFromModel(model)).toBe('/core/nodes');
118 });
119
120 it('should get the url from a service model', () => {
121 const model = {
122 name: 'Tenant',
123 app: 'services.test',
124 fields: []
125 };
126 expect(service.getApiUrlFromModel(model)).toBe('/test/tenants');
127 });
128
129 it('should retrieve a model definition from local cache', () => {
130 const model = {
131 name: 'Node',
132 app: 'core'
133 };
134 service['xosModels'] = [
135 model
136 ];
137 expect(service.get('Node')).toEqual(model);
138 });
139
140 it('should get the service name from the app name', () => {
141 expect(service['serviceNameFromAppName']('services.vsg')).toBe('vsg');
142 });
143
144 it('should get the state name from the model', () => {
145 expect(service['stateNameFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg.tenant');
146 });
147
148 it('should get the parent state name from a core model', () => {
149 expect(service['getParentStateFromModel']({name: 'Nodes', app: 'core'})).toBe('xos.core');
150 });
151
152 it('should get the parent state name from a service model', () => {
153 expect(service['getParentStateFromModel']({name: 'Tenant', app: 'services.vsg'})).toBe('xos.vsg');
154 });
155
156 it('should add a new service entry in the system', () => {
157 service['addService']({name: 'Tenant', app: 'services.vsg'});
158 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg', {
159 url: 'vsg',
160 parent: 'xos',
161 abstract: true,
162 template: '<div ui-view></div>'
163 });
164 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
165 label: 'vsg',
166 state: 'xos.vsg'
167 });
168 expect(service['xosServices'][0]).toEqual('vsg');
169 expect(service['xosServices'].length).toBe(1);
170 });
171
172 it('should add a state in the system', (done) => {
173 MockXosRuntimeStates.addState.calls.reset();
174 service['addState']({name: 'Tenant', app: 'services.vsg'})
175 .then((model) => {
176 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
177 parent: 'xos.vsg',
178 url: '/tenants/:id?',
179 params: {
180 id: null
181 },
182 data: {
183 model: 'Tenant'
184 },
185 component: 'xosCrud',
186 });
187 expect(model.clientUrl).toBe('vsg/tenants/:id?');
188 done();
189 });
190 scope.$apply();
191 });
192
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700193 it('should add a state with relations in the system', (done) => {
194 MockXosRuntimeStates.addState.calls.reset();
195 service['addState']({name: 'Tenant', app: 'services.vsg', relations: [{model: 'Something', type: 'manytoone'}]})
196 .then((model) => {
197 expect(MockXosRuntimeStates.addState).toHaveBeenCalledWith('xos.vsg.tenant', {
198 parent: 'xos.vsg',
199 url: '/tenants/:id?',
200 params: {
201 id: null
202 },
203 data: {
204 model: 'Tenant',
205 relations: [
206 {model: 'Something', type: 'manytoone'}
207 ]
208 },
209 component: 'xosCrud',
210 });
211 expect(model.clientUrl).toBe('vsg/tenants/:id?');
212 done();
213 });
214 scope.$apply();
215 });
216
Matteo Scandolo02229382017-04-18 11:52:23 -0700217 it('should add an item to navigation', () => {
218 service['addNavItem']({name: 'Tenant', app: 'services.vsg'});
219 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
220 label: 'Tenants',
221 state: 'xos.vsg.tenant',
222 parent: 'xos.vsg'
223 });
224 });
225
226 it('should cache a model', () => {
227 service['cacheModelEntries']({name: 'Tenant', app: 'services.vsg'});
228 expect(MockXosModelStore.query).toHaveBeenCalledWith('Tenant', '/vsg/tenants');
229 });
230
231 it('should get the table config', () => {
232 service['getTableCfg']({name: 'Tenant', app: 'services.vsg'});
233 expect(MockConfigHelpers.modelToTableCfg).toHaveBeenCalledWith(
234 {name: 'Tenant', app: 'services.vsg', tableCfg: true},
235 'xos.vsg.tenant'
236 );
237 });
238
239 it('should get the form config', () => {
240 service['getFormCfg']({name: 'Tenant', app: 'services.vsg'});
241 expect(MockConfigHelpers.modelToFormCfg).toHaveBeenCalledWith(
242 {name: 'Tenant', app: 'services.vsg', formCfg: true}
243 );
244 });
245
246 it('should store the model in memory', () => {
247 service['storeModel']({name: 'Tenant'});
248 expect(service['xosModels'][0]).toEqual({name: 'Tenant'});
249 expect(service['xosModels'].length).toEqual(1);
250 });
251
252 describe('when discovering models', () => {
253 beforeEach(() => {
254 spyOn(service, 'cacheModelEntries').and.callThrough();
255 spyOn(service, 'addState').and.callThrough();
256 spyOn(service, 'addNavItem').and.callThrough();
257 spyOn(service, 'getTableCfg').and.callThrough();
258 spyOn(service, 'getFormCfg').and.callThrough();
259 spyOn(service, 'storeModel').and.callThrough();
260 });
261
262 it('should call all the function chain', (done) => {
263 service.discover()
264 .then((res) => {
265 expect(MockProgressBar.start).toHaveBeenCalled();
266 expect(MockXosModelDefs.get).toHaveBeenCalled();
267 expect(service['cacheModelEntries'].calls.count()).toBe(2);
268 expect(service['addState'].calls.count()).toBe(2);
269 expect(service['addNavItem'].calls.count()).toBe(2);
270 expect(service['getTableCfg'].calls.count()).toBe(2);
271 expect(service['getFormCfg'].calls.count()).toBe(2);
272 expect(service['storeModel'].calls.count()).toBe(2);
273 expect(res).toBeTruthy();
274 done();
275 });
276 scope.$apply();
277 });
278 });
279});