blob: ed1cc3bab5fd65d5e705137c00edc622264df109 [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 },
79 ]
80};
81
Matteo Scandolod58d5042016-12-16 16:59:21 -080082describe('The ConfigHelpers service', () => {
83
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080084 beforeEach(() => {
85 angular
86 .module('test', ['toastr'])
87 .service('ConfigHelpers', ConfigHelpers)
88 .value('AuthService', {
89 getUser: () => {
90 return {id: 1};
91 }
Matteo Scandolo04964232017-01-07 12:53:46 -080092 })
Matteo Scandolo1aee1982017-02-17 08:33:23 -080093 .value('XosModelStore', {
Matteo Scandolo04964232017-01-07 12:53:46 -080094
Matteo Scandoloa242c872017-01-12 15:13:00 -080095 })
96 .value('$state', {
97 get: () => {
98 return [
99 {
100 name: 'xos.core.tests',
101 data: {model: 'Test'}
102 },
103 {
104 name: 'xos.core.slices',
105 data: {model: 'Slices'}
106 }
107 ];
108 }
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -0800109 });
110 angular.mock.module('test');
111 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800112
113 beforeEach(angular.mock.inject((
114 ConfigHelpers: IXosConfigHelpersService,
115 ) => {
116 service = ConfigHelpers;
117 }));
118
119 describe('The pluralize function', () => {
120 it('should pluralize string', () => {
121 expect(service.pluralize('test')).toEqual('tests');
122 expect(service.pluralize('test', 1)).toEqual('test');
Matteo Scandolo08464e52017-01-17 13:35:27 -0800123 expect(service.pluralize('xos')).toEqual('xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800124 expect(service.pluralize('slice')).toEqual('slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800125 expect(service.pluralize('Slice', 1)).toEqual('Slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800126 });
127
128 it('should preprend count to string', () => {
129 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
130 expect(service.pluralize('test', 1, true)).toEqual('1 test');
131 });
132 });
133
134 describe('the label formatter', () => {
135 it('should format a camel case string', () => {
136 expect(service.toLabel('camelCase')).toEqual('Camel case');
137 });
138
139 it('should format a snake case string', () => {
140 expect(service.toLabel('snake_case')).toEqual('Snake case');
141 });
142
143 it('should format a kebab case string', () => {
144 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
145 });
146
147 it('should set plural', () => {
148 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
149 });
150
151 it('should format an array of strings', () => {
152 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
153 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800154 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800155 });
156
157 it('should set plural on an array of strings', () => {
158 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
159 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800160 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800161 });
162 });
163
Matteo Scandoloa242c872017-01-12 15:13:00 -0800164 describe('the navigation methods', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800165 describe('stateFromCoreModels', () => {
166
167 let state: ng.ui.IStateService;
168
169 beforeEach(angular.mock.inject(($state) => {
170 state = $state;
171 }));
172
173 it('should return the state for a given model', () => {
174 expect(service.stateFromCoreModel('Test')).toBe('xos.core.tests');
175 });
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700176 });
177 describe('stateWithParams', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800178 it('should return the state with params for a given model', () => {
179 expect(service.stateWithParams('Test', {id: 1})).toBe('xos.core.tests({id: 1})');
180 });
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700181 it('should return the state with params for a given relation', () => {
182 expect(service.relatedStateWithParams('Test', '1')).toBe('xos.core.tests({id: 1})');
183 });
184
185 it('should return the state with params for usage in js', () => {
186 expect(service.stateWithParamsForJs('Test', {id: 1})).toEqual({ name: 'xos.core.tests', params: Object({ id: 1 }) });
187 });
Matteo Scandoloa242c872017-01-12 15:13:00 -0800188 });
189 });
190
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800191 describe('the modelFieldsToColumnsCfg method', () => {
192 it('should return an array of columns', () => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800193 const cols = service.modelFieldsToColumnsCfg({fields: model.fields, name: 'testUrl', app: 'test'});
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800194 expect(cols[0].label).toBe('Id');
195 expect(cols[0].prop).toBe('id');
196 expect(cols[0].link).toBeDefined();
197
198 expect(cols[1].label).toBe('Name');
199 expect(cols[1].prop).toBe('name');
200 expect(cols[1].link).toBeDefined();
201
202 expect(cols[2].label).toBe('Something');
203 expect(cols[2].prop).toBe('something');
204 expect(cols[2].link).not.toBeDefined();
205
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800206 expect(cols[3].label).toBe('Else');
207 expect(cols[3].prop).toBe('else');
208 expect(cols[3].link).not.toBeDefined();
209
210 expect(cols[4]).not.toBeDefined();
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800211 });
212 });
213
214 describe('the modelToTableCfg method', () => {
215 it('should return a table config', () => {
216 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
217 expect(cfg.columns).toBeDefined();
218 expect(cfg.filter).toBe('fulltext');
219 expect(cfg.order).toEqual({field: 'id', reverse: false});
Matteo Scandolocc4bce82017-08-07 13:11:47 -0700220 expect(cfg.actions.length).toBe(2);
221 expect(cfg.actions[0].label).toEqual('details');
222 expect(cfg.actions[1].label).toEqual('delete');
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800223 });
224 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800225
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800226 describe('the modelFieldToInputConfig', () => {
227 it('should return an array of inputs', () => {
228 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800229
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700230 expect(inputs[0].name).toBe('name');
231 expect(inputs[0].type).toBe('string');
232 expect(inputs[0].label).toBe('Name');
233 expect(inputs[0].validators.required).toBe(true);
234
235 expect(inputs[1].name).toBe('something');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800236 expect(inputs[1].type).toBe('string');
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700237 expect(inputs[1].label).toBe('Something');
238 expect(inputs[1].validators.maxlength).toBe(30);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800239
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700240 expect(inputs[2].name).toBe('else');
241 expect(inputs[2].type).toBe('number');
242 expect(inputs[2].label).toBe('Else');
243 expect(inputs[2].validators.min).toBe(20);
244 expect(inputs[2].validators.max).toBe(40);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800245 });
246 });
247
248 describe('the modelToFormCfg method', () => {
249 it('should return a form config', () => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800250 const config: IXosFormCfg = service.modelToFormCfg(model);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800251 expect(config.formName).toBe('TestForm');
252 expect(config.actions.length).toBe(1);
253 expect(config.actions[0].label).toBe('Save');
254 expect(config.actions[0].class).toBe('success');
255 expect(config.actions[0].icon).toBe('ok');
256 expect(config.actions[0].cb).toBeDefined();
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700257 // NOTE 'id' and 'updated' are hidden fields
258 expect(config.inputs.length).toBe(3);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800259 });
260 });
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800261
262 describe('the private methods', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800263 let modelStoreMock, toastr, auth, stateMock;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800264
265 beforeEach(angular.mock.inject((_toastr_, AuthService) => {
266 modelStoreMock = {
267 query: () => {
268 const subject = new BehaviorSubject([
269 {id: 1, humanReadableName: 'test'},
270 {id: 2, humanReadableName: 'second'}
271 ]);
272 return subject.asObservable();
273 }
274 };
275 toastr = _toastr_;
276 auth = AuthService;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800277 stateMock = {
278 get: ''
279 };
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800280 }));
281
282 const field: IXosModelDefsField = {
283 name: 'test',
284 type: 'number',
285 relation: {
286 model: 'Test',
287 type: 'many_to_one'
288 }
289 };
290
291 describe('the populateRelated method', () => {
292 const item = {
293 test: 2
294 };
295 it('should add the formatted data to the column definition', () => {
Matteo Scandolo02229382017-04-18 11:52:23 -0700296 service = new ConfigHelpers(stateMock, toastr, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800297 service['populateRelated'](item, item.test, field);
298 expect(item['test-formatted']).toBe('second');
299 });
300 });
301
302 describe('the populateSelectField', () => {
303
304 const input: IXosFormInput = {
305 name: 'test',
306 label: 'Test',
307 type: 'select',
308 validators: {}
309 };
310
311 it('should add the available choice to the select', () => {
Matteo Scandolo02229382017-04-18 11:52:23 -0700312 service = new ConfigHelpers(stateMock, toastr, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800313 service['populateSelectField'](field, input);
314 expect(input.options).toEqual([
315 {id: 1, label: 'test'},
316 {id: 2, label: 'second'}
317 ]);
318 });
319 });
320 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800321});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800322