blob: 837491ad607af7b24f2f919e909e11d277677431 [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 Scandolocb466ed2017-01-04 17:16:24 -08006import {IModeldef} from '../../../datasources/rest/modeldefs.rest';
7import {IXosTableCfg} from '../../table/table';
Matteo Scandolo80c3a652017-01-06 10:48:31 -08008import {IXosFormInput, IXosFormConfig} 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
13const model: IModeldef = {
14 name: 'Test',
15 fields: [
16 {
17 type: 'number',
18 name: 'id',
19 validators: {}
20 },
21 {
22 type: 'string',
23 name: 'name',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080024 validators: {
25 required: true
26 }
Matteo Scandolocb466ed2017-01-04 17:16:24 -080027 },
28 {
29 type: 'string',
30 name: 'something',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080031 validators: {
32 maxlength: 30
33 }
34 },
35 {
36 type: 'number',
37 name: 'else',
38 validators: {
39 min: 20,
40 max: 40
41 }
Matteo Scandolocb466ed2017-01-04 17:16:24 -080042 },
43 {
44 type: 'date',
45 name: 'updated',
46 validators: {}
47 },
48 ]
49};
50
Matteo Scandolod58d5042016-12-16 16:59:21 -080051describe('The ConfigHelpers service', () => {
52
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080053 beforeEach(() => {
54 angular
55 .module('test', ['toastr'])
56 .service('ConfigHelpers', ConfigHelpers)
57 .value('AuthService', {
58 getUser: () => {
59 return {id: 1};
60 }
Matteo Scandolo04964232017-01-07 12:53:46 -080061 })
62 .value('ModelStore', {
63
Matteo Scandoloa242c872017-01-12 15:13:00 -080064 })
65 .value('$state', {
66 get: () => {
67 return [
68 {
69 name: 'xos.core.tests',
70 data: {model: 'Test'}
71 },
72 {
73 name: 'xos.core.slices',
74 data: {model: 'Slices'}
75 }
76 ];
77 }
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080078 });
79 angular.mock.module('test');
80 });
Matteo Scandolod58d5042016-12-16 16:59:21 -080081
82 beforeEach(angular.mock.inject((
83 ConfigHelpers: IXosConfigHelpersService,
84 ) => {
85 service = ConfigHelpers;
86 }));
87
88 describe('The pluralize function', () => {
89 it('should pluralize string', () => {
90 expect(service.pluralize('test')).toEqual('tests');
91 expect(service.pluralize('test', 1)).toEqual('test');
92 expect(service.pluralize('xos')).toEqual('xosses');
93 expect(service.pluralize('slice')).toEqual('slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -080094 expect(service.pluralize('Slice', 1)).toEqual('Slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -080095 });
96
97 it('should preprend count to string', () => {
98 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
99 expect(service.pluralize('test', 1, true)).toEqual('1 test');
100 });
101 });
102
103 describe('the label formatter', () => {
104 it('should format a camel case string', () => {
105 expect(service.toLabel('camelCase')).toEqual('Camel case');
106 });
107
108 it('should format a snake case string', () => {
109 expect(service.toLabel('snake_case')).toEqual('Snake case');
110 });
111
112 it('should format a kebab case string', () => {
113 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
114 });
115
116 it('should set plural', () => {
117 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
118 });
119
120 it('should format an array of strings', () => {
121 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
122 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800123 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800124 });
125
126 it('should set plural on an array of strings', () => {
127 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
128 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800129 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800130 });
131 });
132
Matteo Scandoloa242c872017-01-12 15:13:00 -0800133 describe('the navigation methods', () => {
134 describe('urlFromCoreModels', () => {
135 it('should return the URL for a given model', () => {
136 expect(service.urlFromCoreModel('Test')).toBe('/core/tests');
137 });
138 });
139 describe('stateFromCoreModels', () => {
140
141 let state: ng.ui.IStateService;
142
143 beforeEach(angular.mock.inject(($state) => {
144 state = $state;
145 }));
146
147 it('should return the state for a given model', () => {
148 expect(service.stateFromCoreModel('Test')).toBe('xos.core.tests');
149 });
150
151 it('should return the state with params for a given model', () => {
152 expect(service.stateWithParams('Test', {id: 1})).toBe('xos.core.tests({id: 1})');
153 });
154 });
155 });
156
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800157 describe('the modelFieldsToColumnsCfg method', () => {
158 it('should return an array of columns', () => {
159 const cols = service.modelFieldsToColumnsCfg(model.fields, 'testUrl/:id?');
160 expect(cols[0].label).toBe('Id');
161 expect(cols[0].prop).toBe('id');
162 expect(cols[0].link).toBeDefined();
163
164 expect(cols[1].label).toBe('Name');
165 expect(cols[1].prop).toBe('name');
166 expect(cols[1].link).toBeDefined();
167
168 expect(cols[2].label).toBe('Something');
169 expect(cols[2].prop).toBe('something');
170 expect(cols[2].link).not.toBeDefined();
171
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800172 expect(cols[3].label).toBe('Else');
173 expect(cols[3].prop).toBe('else');
174 expect(cols[3].link).not.toBeDefined();
175
176 expect(cols[4]).not.toBeDefined();
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800177 });
178 });
179
180 describe('the modelToTableCfg method', () => {
181 it('should return a table config', () => {
182 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
183 expect(cfg.columns).toBeDefined();
184 expect(cfg.filter).toBe('fulltext');
185 expect(cfg.order).toEqual({field: 'id', reverse: false});
186 expect(cfg.actions.length).toBe(1);
187 });
188 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800189
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800190 describe('the modelFieldToInputConfig', () => {
191 it('should return an array of inputs', () => {
192 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
193 expect(inputs[0].name).toBe('id');
194 expect(inputs[0].type).toBe('number');
195 expect(inputs[0].label).toBe('Id');
196
197 expect(inputs[1].name).toBe('name');
198 expect(inputs[1].type).toBe('string');
199 expect(inputs[1].label).toBe('Name');
200 expect(inputs[1].validators.required).toBe(true);
201
202 expect(inputs[2].name).toBe('something');
203 expect(inputs[2].type).toBe('string');
204 expect(inputs[2].label).toBe('Something');
205 expect(inputs[2].validators.maxlength).toBe(30);
206
207 expect(inputs[3].name).toBe('else');
208 expect(inputs[3].type).toBe('number');
209 expect(inputs[3].label).toBe('Else');
210 expect(inputs[3].validators.min).toBe(20);
211 expect(inputs[3].validators.max).toBe(40);
212 });
213 });
214
215 describe('the modelToFormCfg method', () => {
216 it('should return a form config', () => {
217 const config: IXosFormConfig = service.modelToFormCfg(model);
218 expect(config.formName).toBe('TestForm');
219 expect(config.actions.length).toBe(1);
220 expect(config.actions[0].label).toBe('Save');
221 expect(config.actions[0].class).toBe('success');
222 expect(config.actions[0].icon).toBe('ok');
223 expect(config.actions[0].cb).toBeDefined();
224 expect(config.inputs.length).toBe(4);
225 });
226 });
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800227
228 describe('the private methods', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800229 let modelStoreMock, toastr, auth, stateMock;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800230
231 beforeEach(angular.mock.inject((_toastr_, AuthService) => {
232 modelStoreMock = {
233 query: () => {
234 const subject = new BehaviorSubject([
235 {id: 1, humanReadableName: 'test'},
236 {id: 2, humanReadableName: 'second'}
237 ]);
238 return subject.asObservable();
239 }
240 };
241 toastr = _toastr_;
242 auth = AuthService;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800243 stateMock = {
244 get: ''
245 };
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800246 }));
247
248 const field: IXosModelDefsField = {
249 name: 'test',
250 type: 'number',
251 relation: {
252 model: 'Test',
253 type: 'many_to_one'
254 }
255 };
256
257 describe('the populateRelated method', () => {
258 const item = {
259 test: 2
260 };
261 it('should add the formatted data to the column definition', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800262 service = new ConfigHelpers(stateMock, toastr, auth, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800263 service['populateRelated'](item, item.test, field);
264 expect(item['test-formatted']).toBe('second');
265 });
266 });
267
268 describe('the populateSelectField', () => {
269
270 const input: IXosFormInput = {
271 name: 'test',
272 label: 'Test',
273 type: 'select',
274 validators: {}
275 };
276
277 it('should add the available choice to the select', () => {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800278 service = new ConfigHelpers(stateMock, toastr, auth, modelStoreMock);
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800279 service['populateSelectField'](field, input);
280 expect(input.options).toEqual([
281 {id: 1, label: 'test'},
282 {id: 2, label: 'second'}
283 ]);
284 });
285 });
286 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800287});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800288