blob: f06fc4fd41e5936fc6227a08699af50f2d4d455c [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolod58d5042016-12-16 16:59:21 -080019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-ui-router';
22
Matteo Scandolo07e2f622017-01-09 10:54:13 -080023import {IXosConfigHelpersService, ConfigHelpers, IXosModelDefsField} from './config.helpers';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080024import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest';
Matteo Scandolocb466ed2017-01-04 17:16:24 -080025import {IXosTableCfg} from '../../table/table';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080026import {IXosFormInput, IXosFormCfg} from '../../form/form';
Matteo Scandolo07e2f622017-01-09 10:54:13 -080027import {BehaviorSubject} from 'rxjs';
Matteo Scandolod58d5042016-12-16 16:59:21 -080028
29let service: IXosConfigHelpersService;
Matteo Scandolocb466ed2017-01-04 17:16:24 -080030
Matteo Scandolo1aee1982017-02-17 08:33:23 -080031const model: IXosModeldef = {
Matteo Scandolocb466ed2017-01-04 17:16:24 -080032 name: 'Test',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080033 app: 'test',
Matteo Scandolocb466ed2017-01-04 17:16:24 -080034 fields: [
35 {
36 type: 'number',
37 name: 'id',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080038 validators: []
Matteo Scandolocb466ed2017-01-04 17:16:24 -080039 },
40 {
41 type: 'string',
42 name: 'name',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080043 validators: [
44 {
45 bool_value: true,
46 name: 'required'
47 }
48 ]
Matteo Scandolocb466ed2017-01-04 17:16:24 -080049 },
50 {
51 type: 'string',
52 name: 'something',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080053 validators: [
54 {
55 int_value: 30,
56 name: 'maxlength'
57 }
58 ]
Matteo Scandolo80c3a652017-01-06 10:48:31 -080059 },
60 {
61 type: 'number',
62 name: 'else',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080063 validators: [
64 {
65 int_value: 20,
66 name: 'min'
67 },
68 {
69 int_value: 40,
70 name: 'max'
71 }
72 ]
Matteo Scandolocb466ed2017-01-04 17:16:24 -080073 },
74 {
75 type: 'date',
76 name: 'updated',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080077 validators: []
Matteo Scandolocb466ed2017-01-04 17:16:24 -080078 },
Matteo Scandolo580033a2017-08-17 11:16:39 -070079 ],
80 description: '',
81 verbose_name: ''
Matteo Scandolocb466ed2017-01-04 17:16:24 -080082};
83
Matteo Scandolod58d5042016-12-16 16:59:21 -080084describe('The ConfigHelpers service', () => {
85
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080086 beforeEach(() => {
87 angular
88 .module('test', ['toastr'])
89 .service('ConfigHelpers', ConfigHelpers)
90 .value('AuthService', {
91 getUser: () => {
92 return {id: 1};
93 }
Matteo Scandolo04964232017-01-07 12:53:46 -080094 })
Matteo Scandolo1aee1982017-02-17 08:33:23 -080095 .value('XosModelStore', {
Matteo Scandolo04964232017-01-07 12:53:46 -080096
Matteo Scandoloa242c872017-01-12 15:13:00 -080097 })
98 .value('$state', {
99 get: () => {
100 return [
101 {
102 name: 'xos.core.tests',
103 data: {model: 'Test'}
104 },
105 {
106 name: 'xos.core.slices',
107 data: {model: 'Slices'}
108 }
109 ];
110 }
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -0800111 });
112 angular.mock.module('test');
113 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800114
115 beforeEach(angular.mock.inject((
116 ConfigHelpers: IXosConfigHelpersService,
117 ) => {
118 service = ConfigHelpers;
119 }));
120
121 describe('The pluralize function', () => {
122 it('should pluralize string', () => {
123 expect(service.pluralize('test')).toEqual('tests');
124 expect(service.pluralize('test', 1)).toEqual('test');
Matteo Scandolo08464e52017-01-17 13:35:27 -0800125 expect(service.pluralize('xos')).toEqual('xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800126 expect(service.pluralize('slice')).toEqual('slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800127 expect(service.pluralize('Slice', 1)).toEqual('Slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800128 });
129
130 it('should preprend count to string', () => {
131 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
132 expect(service.pluralize('test', 1, true)).toEqual('1 test');
133 });
134 });
135
136 describe('the label formatter', () => {
137 it('should format a camel case string', () => {
138 expect(service.toLabel('camelCase')).toEqual('Camel case');
139 });
140
141 it('should format a snake case string', () => {
142 expect(service.toLabel('snake_case')).toEqual('Snake case');
143 });
144
145 it('should format a kebab case string', () => {
146 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
147 });
148
149 it('should set plural', () => {
150 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
151 });
152
153 it('should format an array of strings', () => {
154 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
155 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800156 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800157 });
158
159 it('should set plural on an array of strings', () => {
160 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
161 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800162 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800163 });
164 });
165
Matteo Scandoloa242c872017-01-12 15:13:00 -0800166 describe('the navigation methods', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800167 describe('stateFromCoreModels', () => {
168
169 let state: ng.ui.IStateService;
170
171 beforeEach(angular.mock.inject(($state) => {
172 state = $state;
173 }));
174
175 it('should return the state for a given model', () => {
176 expect(service.stateFromCoreModel('Test')).toBe('xos.core.tests');
177 });
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700178 });
179 describe('stateWithParams', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800180 it('should return the state with params for a given model', () => {
181 expect(service.stateWithParams('Test', {id: 1})).toBe('xos.core.tests({id: 1})');
182 });
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700183 it('should return the state with params for a given relation', () => {
184 expect(service.relatedStateWithParams('Test', '1')).toBe('xos.core.tests({id: 1})');
185 });
186
187 it('should return the state with params for usage in js', () => {
188 expect(service.stateWithParamsForJs('Test', {id: 1})).toEqual({ name: 'xos.core.tests', params: Object({ id: 1 }) });
189 });
Matteo Scandoloa242c872017-01-12 15:13:00 -0800190 });
191 });
192
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800193 describe('the modelFieldsToColumnsCfg method', () => {
194 it('should return an array of columns', () => {
Matteo Scandolo580033a2017-08-17 11:16:39 -0700195 const cols = service.modelFieldsToColumnsCfg({fields: model.fields, name: 'testUrl', app: 'test', description: '', verbose_name: ''});
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800196 expect(cols[0].label).toBe('Id');
197 expect(cols[0].prop).toBe('id');
198 expect(cols[0].link).toBeDefined();
199
200 expect(cols[1].label).toBe('Name');
201 expect(cols[1].prop).toBe('name');
202 expect(cols[1].link).toBeDefined();
203
204 expect(cols[2].label).toBe('Something');
205 expect(cols[2].prop).toBe('something');
206 expect(cols[2].link).not.toBeDefined();
207
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800208 expect(cols[3].label).toBe('Else');
209 expect(cols[3].prop).toBe('else');
210 expect(cols[3].link).not.toBeDefined();
211
212 expect(cols[4]).not.toBeDefined();
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800213 });
214 });
215
216 describe('the modelToTableCfg method', () => {
217 it('should return a table config', () => {
218 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
219 expect(cfg.columns).toBeDefined();
220 expect(cfg.filter).toBe('fulltext');
221 expect(cfg.order).toEqual({field: 'id', reverse: false});
Matteo Scandolocc4bce82017-08-07 13:11:47 -0700222 expect(cfg.actions.length).toBe(2);
223 expect(cfg.actions[0].label).toEqual('details');
224 expect(cfg.actions[1].label).toEqual('delete');
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800225 });
226 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800227
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800228 describe('the modelFieldToInputConfig', () => {
229 it('should return an array of inputs', () => {
230 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800231
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700232 expect(inputs[0].name).toBe('name');
233 expect(inputs[0].type).toBe('string');
234 expect(inputs[0].label).toBe('Name');
235 expect(inputs[0].validators.required).toBe(true);
236
237 expect(inputs[1].name).toBe('something');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800238 expect(inputs[1].type).toBe('string');
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700239 expect(inputs[1].label).toBe('Something');
240 expect(inputs[1].validators.maxlength).toBe(30);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800241
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700242 expect(inputs[2].name).toBe('else');
243 expect(inputs[2].type).toBe('number');
244 expect(inputs[2].label).toBe('Else');
245 expect(inputs[2].validators.min).toBe(20);
246 expect(inputs[2].validators.max).toBe(40);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800247 });
248 });
249
250 describe('the modelToFormCfg method', () => {
251 it('should return a form config', () => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800252 const config: IXosFormCfg = service.modelToFormCfg(model);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800253 expect(config.formName).toBe('TestForm');
254 expect(config.actions.length).toBe(1);
255 expect(config.actions[0].label).toBe('Save');
256 expect(config.actions[0].class).toBe('success');
257 expect(config.actions[0].icon).toBe('ok');
258 expect(config.actions[0].cb).toBeDefined();
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700259 // NOTE 'id' and 'updated' are hidden fields
260 expect(config.inputs.length).toBe(3);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800261 });
262 });
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800263
264 describe('the private methods', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800265 let modelStoreMock, toastr, auth, stateMock;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800266
267 beforeEach(angular.mock.inject((_toastr_, AuthService) => {
268 modelStoreMock = {
269 query: () => {
270 const subject = new BehaviorSubject([
271 {id: 1, humanReadableName: 'test'},
272 {id: 2, humanReadableName: 'second'}
273 ]);
274 return subject.asObservable();
275 }
276 };
277 toastr = _toastr_;
278 auth = AuthService;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800279 stateMock = {
280 get: ''
281 };
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800282 }));
283
284 const field: IXosModelDefsField = {
285 name: 'test',
286 type: 'number',
287 relation: {
288 model: 'Test',
289 type: 'many_to_one'
290 }
291 };
292
293 describe('the populateRelated method', () => {
294 const item = {
295 test: 2
296 };
297 it('should add the formatted data to the column definition', () => {
Matteo Scandolo02229382017-04-18 11:52:23 -0700298 service = new ConfigHelpers(stateMock, toastr, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800299 service['populateRelated'](item, item.test, field);
300 expect(item['test-formatted']).toBe('second');
301 });
302 });
303
304 describe('the populateSelectField', () => {
305
306 const input: IXosFormInput = {
307 name: 'test',
308 label: 'Test',
309 type: 'select',
310 validators: {}
311 };
312
313 it('should add the available choice to the select', () => {
Matteo Scandolo02229382017-04-18 11:52:23 -0700314 service = new ConfigHelpers(stateMock, toastr, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800315 service['populateSelectField'](field, input);
316 expect(input.options).toEqual([
317 {id: 1, label: 'test'},
318 {id: 2, label: 'second'}
319 ]);
320 });
321 });
322 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800323});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800324