blob: d5a3232d97fe117515a70320d49ab404242c8618 [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
193 it('should add an item to navigation', () => {
194 service['addNavItem']({name: 'Tenant', app: 'services.vsg'});
195 expect(MockXosNavigationService.add).toHaveBeenCalledWith({
196 label: 'Tenants',
197 state: 'xos.vsg.tenant',
198 parent: 'xos.vsg'
199 });
200 });
201
202 it('should cache a model', () => {
203 service['cacheModelEntries']({name: 'Tenant', app: 'services.vsg'});
204 expect(MockXosModelStore.query).toHaveBeenCalledWith('Tenant', '/vsg/tenants');
205 });
206
207 it('should get the table config', () => {
208 service['getTableCfg']({name: 'Tenant', app: 'services.vsg'});
209 expect(MockConfigHelpers.modelToTableCfg).toHaveBeenCalledWith(
210 {name: 'Tenant', app: 'services.vsg', tableCfg: true},
211 'xos.vsg.tenant'
212 );
213 });
214
215 it('should get the form config', () => {
216 service['getFormCfg']({name: 'Tenant', app: 'services.vsg'});
217 expect(MockConfigHelpers.modelToFormCfg).toHaveBeenCalledWith(
218 {name: 'Tenant', app: 'services.vsg', formCfg: true}
219 );
220 });
221
222 it('should store the model in memory', () => {
223 service['storeModel']({name: 'Tenant'});
224 expect(service['xosModels'][0]).toEqual({name: 'Tenant'});
225 expect(service['xosModels'].length).toEqual(1);
226 });
227
228 describe('when discovering models', () => {
229 beforeEach(() => {
230 spyOn(service, 'cacheModelEntries').and.callThrough();
231 spyOn(service, 'addState').and.callThrough();
232 spyOn(service, 'addNavItem').and.callThrough();
233 spyOn(service, 'getTableCfg').and.callThrough();
234 spyOn(service, 'getFormCfg').and.callThrough();
235 spyOn(service, 'storeModel').and.callThrough();
236 });
237
238 it('should call all the function chain', (done) => {
239 service.discover()
240 .then((res) => {
241 expect(MockProgressBar.start).toHaveBeenCalled();
242 expect(MockXosModelDefs.get).toHaveBeenCalled();
243 expect(service['cacheModelEntries'].calls.count()).toBe(2);
244 expect(service['addState'].calls.count()).toBe(2);
245 expect(service['addNavItem'].calls.count()).toBe(2);
246 expect(service['getTableCfg'].calls.count()).toBe(2);
247 expect(service['getFormCfg'].calls.count()).toBe(2);
248 expect(service['storeModel'].calls.count()).toBe(2);
249 expect(res).toBeTruthy();
250 done();
251 });
252 scope.$apply();
253 });
254 });
255});