blob: 957998c1f6dc0aa0a414e6daa48256d1aa01b2be [file] [log] [blame]
Matteo Scandolod58d5042016-12-16 16:59:21 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-ui-router';
4
Matteo Scandolo07e2f622017-01-09 10:54:13 -08005import {IXosConfigHelpersService, ConfigHelpers, IXosModelDefsField} from './config.helpers';
Matteo Scandolo1aee1982017-02-17 08:33:23 -08006import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest';
Matteo Scandolocb466ed2017-01-04 17:16:24 -08007import {IXosTableCfg} from '../../table/table';
Matteo Scandolo1aee1982017-02-17 08:33:23 -08008import {IXosFormInput, IXosFormCfg} from '../../form/form';
Matteo Scandolo07e2f622017-01-09 10:54:13 -08009import {BehaviorSubject} from 'rxjs';
Matteo Scandolod58d5042016-12-16 16:59:21 -080010
11let service: IXosConfigHelpersService;
Matteo Scandolocb466ed2017-01-04 17:16:24 -080012
Matteo Scandolo1aee1982017-02-17 08:33:23 -080013const model: IXosModeldef = {
Matteo Scandolocb466ed2017-01-04 17:16:24 -080014 name: 'Test',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080015 app: 'test',
Matteo Scandolocb466ed2017-01-04 17:16:24 -080016 fields: [
17 {
18 type: 'number',
19 name: 'id',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080020 validators: []
Matteo Scandolocb466ed2017-01-04 17:16:24 -080021 },
22 {
23 type: 'string',
24 name: 'name',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080025 validators: [
26 {
27 bool_value: true,
28 name: 'required'
29 }
30 ]
Matteo Scandolocb466ed2017-01-04 17:16:24 -080031 },
32 {
33 type: 'string',
34 name: 'something',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080035 validators: [
36 {
37 int_value: 30,
38 name: 'maxlength'
39 }
40 ]
Matteo Scandolo80c3a652017-01-06 10:48:31 -080041 },
42 {
43 type: 'number',
44 name: 'else',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080045 validators: [
46 {
47 int_value: 20,
48 name: 'min'
49 },
50 {
51 int_value: 40,
52 name: 'max'
53 }
54 ]
Matteo Scandolocb466ed2017-01-04 17:16:24 -080055 },
56 {
57 type: 'date',
58 name: 'updated',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080059 validators: []
Matteo Scandolocb466ed2017-01-04 17:16:24 -080060 },
61 ]
62};
63
Matteo Scandolod58d5042016-12-16 16:59:21 -080064describe('The ConfigHelpers service', () => {
65
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080066 beforeEach(() => {
67 angular
68 .module('test', ['toastr'])
69 .service('ConfigHelpers', ConfigHelpers)
70 .value('AuthService', {
71 getUser: () => {
72 return {id: 1};
73 }
Matteo Scandolo04964232017-01-07 12:53:46 -080074 })
Matteo Scandolo1aee1982017-02-17 08:33:23 -080075 .value('XosModelStore', {
Matteo Scandolo04964232017-01-07 12:53:46 -080076
Matteo Scandoloa242c872017-01-12 15:13:00 -080077 })
78 .value('$state', {
79 get: () => {
80 return [
81 {
82 name: 'xos.core.tests',
83 data: {model: 'Test'}
84 },
85 {
86 name: 'xos.core.slices',
87 data: {model: 'Slices'}
88 }
89 ];
90 }
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080091 });
92 angular.mock.module('test');
93 });
Matteo Scandolod58d5042016-12-16 16:59:21 -080094
95 beforeEach(angular.mock.inject((
96 ConfigHelpers: IXosConfigHelpersService,
97 ) => {
98 service = ConfigHelpers;
99 }));
100
101 describe('The pluralize function', () => {
102 it('should pluralize string', () => {
103 expect(service.pluralize('test')).toEqual('tests');
104 expect(service.pluralize('test', 1)).toEqual('test');
Matteo Scandolo08464e52017-01-17 13:35:27 -0800105 expect(service.pluralize('xos')).toEqual('xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800106 expect(service.pluralize('slice')).toEqual('slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800107 expect(service.pluralize('Slice', 1)).toEqual('Slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800108 });
109
110 it('should preprend count to string', () => {
111 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
112 expect(service.pluralize('test', 1, true)).toEqual('1 test');
113 });
114 });
115
116 describe('the label formatter', () => {
117 it('should format a camel case string', () => {
118 expect(service.toLabel('camelCase')).toEqual('Camel case');
119 });
120
121 it('should format a snake case string', () => {
122 expect(service.toLabel('snake_case')).toEqual('Snake case');
123 });
124
125 it('should format a kebab case string', () => {
126 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
127 });
128
129 it('should set plural', () => {
130 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
131 });
132
133 it('should format an array of strings', () => {
134 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
135 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800136 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800137 });
138
139 it('should set plural on an array of strings', () => {
140 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
141 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800142 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800143 });
144 });
145
Matteo Scandoloa242c872017-01-12 15:13:00 -0800146 describe('the navigation methods', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800147 describe('stateFromCoreModels', () => {
148
149 let state: ng.ui.IStateService;
150
151 beforeEach(angular.mock.inject(($state) => {
152 state = $state;
153 }));
154
155 it('should return the state for a given model', () => {
156 expect(service.stateFromCoreModel('Test')).toBe('xos.core.tests');
157 });
158
159 it('should return the state with params for a given model', () => {
160 expect(service.stateWithParams('Test', {id: 1})).toBe('xos.core.tests({id: 1})');
161 });
162 });
163 });
164
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800165 describe('the modelFieldsToColumnsCfg method', () => {
166 it('should return an array of columns', () => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800167 const cols = service.modelFieldsToColumnsCfg({fields: model.fields, name: 'testUrl', app: 'test'});
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800168 expect(cols[0].label).toBe('Id');
169 expect(cols[0].prop).toBe('id');
170 expect(cols[0].link).toBeDefined();
171
172 expect(cols[1].label).toBe('Name');
173 expect(cols[1].prop).toBe('name');
174 expect(cols[1].link).toBeDefined();
175
176 expect(cols[2].label).toBe('Something');
177 expect(cols[2].prop).toBe('something');
178 expect(cols[2].link).not.toBeDefined();
179
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800180 expect(cols[3].label).toBe('Else');
181 expect(cols[3].prop).toBe('else');
182 expect(cols[3].link).not.toBeDefined();
183
184 expect(cols[4]).not.toBeDefined();
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800185 });
186 });
187
188 describe('the modelToTableCfg method', () => {
189 it('should return a table config', () => {
190 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
191 expect(cfg.columns).toBeDefined();
192 expect(cfg.filter).toBe('fulltext');
193 expect(cfg.order).toEqual({field: 'id', reverse: false});
Matteo Scandolo2c61b882017-08-07 13:11:47 -0700194 expect(cfg.actions.length).toBe(2);
195 expect(cfg.actions[0].label).toEqual('details');
196 expect(cfg.actions[1].label).toEqual('delete');
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800197 });
198 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800199
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800200 describe('the modelFieldToInputConfig', () => {
201 it('should return an array of inputs', () => {
202 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
203 expect(inputs[0].name).toBe('id');
204 expect(inputs[0].type).toBe('number');
205 expect(inputs[0].label).toBe('Id');
206
207 expect(inputs[1].name).toBe('name');
208 expect(inputs[1].type).toBe('string');
209 expect(inputs[1].label).toBe('Name');
210 expect(inputs[1].validators.required).toBe(true);
211
212 expect(inputs[2].name).toBe('something');
213 expect(inputs[2].type).toBe('string');
214 expect(inputs[2].label).toBe('Something');
215 expect(inputs[2].validators.maxlength).toBe(30);
216
217 expect(inputs[3].name).toBe('else');
218 expect(inputs[3].type).toBe('number');
219 expect(inputs[3].label).toBe('Else');
220 expect(inputs[3].validators.min).toBe(20);
221 expect(inputs[3].validators.max).toBe(40);
222 });
223 });
224
225 describe('the modelToFormCfg method', () => {
226 it('should return a form config', () => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800227 const config: IXosFormCfg = service.modelToFormCfg(model);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800228 expect(config.formName).toBe('TestForm');
229 expect(config.actions.length).toBe(1);
230 expect(config.actions[0].label).toBe('Save');
231 expect(config.actions[0].class).toBe('success');
232 expect(config.actions[0].icon).toBe('ok');
233 expect(config.actions[0].cb).toBeDefined();
234 expect(config.inputs.length).toBe(4);
235 });
236 });
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800237
238 describe('the private methods', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800239 let modelStoreMock, toastr, auth, stateMock;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800240
241 beforeEach(angular.mock.inject((_toastr_, AuthService) => {
242 modelStoreMock = {
243 query: () => {
244 const subject = new BehaviorSubject([
245 {id: 1, humanReadableName: 'test'},
246 {id: 2, humanReadableName: 'second'}
247 ]);
248 return subject.asObservable();
249 }
250 };
251 toastr = _toastr_;
252 auth = AuthService;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800253 stateMock = {
254 get: ''
255 };
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800256 }));
257
258 const field: IXosModelDefsField = {
259 name: 'test',
260 type: 'number',
261 relation: {
262 model: 'Test',
263 type: 'many_to_one'
264 }
265 };
266
267 describe('the populateRelated method', () => {
268 const item = {
269 test: 2
270 };
271 it('should add the formatted data to the column definition', () => {
Matteo Scandolo02229382017-04-18 11:52:23 -0700272 service = new ConfigHelpers(stateMock, toastr, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800273 service['populateRelated'](item, item.test, field);
274 expect(item['test-formatted']).toBe('second');
275 });
276 });
277
278 describe('the populateSelectField', () => {
279
280 const input: IXosFormInput = {
281 name: 'test',
282 label: 'Test',
283 type: 'select',
284 validators: {}
285 };
286
287 it('should add the available choice to the select', () => {
Matteo Scandolo02229382017-04-18 11:52:23 -0700288 service = new ConfigHelpers(stateMock, toastr, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800289 service['populateSelectField'](field, input);
290 expect(input.options).toEqual([
291 {id: 1, label: 'test'},
292 {id: 2, label: 'second'}
293 ]);
294 });
295 });
296 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800297});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800298