blob: ed4122851d49498b4d316120fe821e600be03809 [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});
194 expect(cfg.actions.length).toBe(1);
195 });
196 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800197
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800198 describe('the modelFieldToInputConfig', () => {
199 it('should return an array of inputs', () => {
200 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
201 expect(inputs[0].name).toBe('id');
202 expect(inputs[0].type).toBe('number');
203 expect(inputs[0].label).toBe('Id');
204
205 expect(inputs[1].name).toBe('name');
206 expect(inputs[1].type).toBe('string');
207 expect(inputs[1].label).toBe('Name');
208 expect(inputs[1].validators.required).toBe(true);
209
210 expect(inputs[2].name).toBe('something');
211 expect(inputs[2].type).toBe('string');
212 expect(inputs[2].label).toBe('Something');
213 expect(inputs[2].validators.maxlength).toBe(30);
214
215 expect(inputs[3].name).toBe('else');
216 expect(inputs[3].type).toBe('number');
217 expect(inputs[3].label).toBe('Else');
218 expect(inputs[3].validators.min).toBe(20);
219 expect(inputs[3].validators.max).toBe(40);
220 });
221 });
222
223 describe('the modelToFormCfg method', () => {
224 it('should return a form config', () => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800225 const config: IXosFormCfg = service.modelToFormCfg(model);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800226 expect(config.formName).toBe('TestForm');
227 expect(config.actions.length).toBe(1);
228 expect(config.actions[0].label).toBe('Save');
229 expect(config.actions[0].class).toBe('success');
230 expect(config.actions[0].icon).toBe('ok');
231 expect(config.actions[0].cb).toBeDefined();
232 expect(config.inputs.length).toBe(4);
233 });
234 });
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800235
236 describe('the private methods', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800237 let modelStoreMock, toastr, auth, stateMock;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800238
239 beforeEach(angular.mock.inject((_toastr_, AuthService) => {
240 modelStoreMock = {
241 query: () => {
242 const subject = new BehaviorSubject([
243 {id: 1, humanReadableName: 'test'},
244 {id: 2, humanReadableName: 'second'}
245 ]);
246 return subject.asObservable();
247 }
248 };
249 toastr = _toastr_;
250 auth = AuthService;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800251 stateMock = {
252 get: ''
253 };
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800254 }));
255
256 const field: IXosModelDefsField = {
257 name: 'test',
258 type: 'number',
259 relation: {
260 model: 'Test',
261 type: 'many_to_one'
262 }
263 };
264
265 describe('the populateRelated method', () => {
266 const item = {
267 test: 2
268 };
269 it('should add the formatted data to the column definition', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800270 service = new ConfigHelpers(stateMock, toastr, auth, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800271 service['populateRelated'](item, item.test, field);
272 expect(item['test-formatted']).toBe('second');
273 });
274 });
275
276 describe('the populateSelectField', () => {
277
278 const input: IXosFormInput = {
279 name: 'test',
280 label: 'Test',
281 type: 'select',
282 validators: {}
283 };
284
285 it('should add the available choice to the select', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800286 service = new ConfigHelpers(stateMock, toastr, auth, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800287 service['populateSelectField'](field, input);
288 expect(input.options).toEqual([
289 {id: 1, label: 'test'},
290 {id: 2, label: 'second'}
291 ]);
292 });
293 });
294 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800295});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800296