blob: eca43d90b08022f6a4bde119cd4509a6f409c7ef [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 Scandolo0a8b02e2017-01-06 14:43:36 -080064 });
65 angular.mock.module('test');
66 });
Matteo Scandolod58d5042016-12-16 16:59:21 -080067
68 beforeEach(angular.mock.inject((
69 ConfigHelpers: IXosConfigHelpersService,
70 ) => {
71 service = ConfigHelpers;
72 }));
73
74 describe('The pluralize function', () => {
75 it('should pluralize string', () => {
76 expect(service.pluralize('test')).toEqual('tests');
77 expect(service.pluralize('test', 1)).toEqual('test');
78 expect(service.pluralize('xos')).toEqual('xosses');
79 expect(service.pluralize('slice')).toEqual('slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -080080 expect(service.pluralize('Slice', 1)).toEqual('Slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -080081 });
82
83 it('should preprend count to string', () => {
84 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
85 expect(service.pluralize('test', 1, true)).toEqual('1 test');
86 });
87 });
88
89 describe('the label formatter', () => {
90 it('should format a camel case string', () => {
91 expect(service.toLabel('camelCase')).toEqual('Camel case');
92 });
93
94 it('should format a snake case string', () => {
95 expect(service.toLabel('snake_case')).toEqual('Snake case');
96 });
97
98 it('should format a kebab case string', () => {
99 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
100 });
101
102 it('should set plural', () => {
103 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
104 });
105
106 it('should format an array of strings', () => {
107 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
108 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800109 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800110 });
111
112 it('should set plural on an array of strings', () => {
113 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
114 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800115 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800116 });
117 });
118
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800119 describe('the modelFieldsToColumnsCfg method', () => {
120 it('should return an array of columns', () => {
121 const cols = service.modelFieldsToColumnsCfg(model.fields, 'testUrl/:id?');
122 expect(cols[0].label).toBe('Id');
123 expect(cols[0].prop).toBe('id');
124 expect(cols[0].link).toBeDefined();
125
126 expect(cols[1].label).toBe('Name');
127 expect(cols[1].prop).toBe('name');
128 expect(cols[1].link).toBeDefined();
129
130 expect(cols[2].label).toBe('Something');
131 expect(cols[2].prop).toBe('something');
132 expect(cols[2].link).not.toBeDefined();
133
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800134 expect(cols[3].label).toBe('Else');
135 expect(cols[3].prop).toBe('else');
136 expect(cols[3].link).not.toBeDefined();
137
138 expect(cols[4]).not.toBeDefined();
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800139 });
140 });
141
142 describe('the modelToTableCfg method', () => {
143 it('should return a table config', () => {
144 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
145 expect(cfg.columns).toBeDefined();
146 expect(cfg.filter).toBe('fulltext');
147 expect(cfg.order).toEqual({field: 'id', reverse: false});
148 expect(cfg.actions.length).toBe(1);
149 });
150 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800151
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800152 describe('the modelFieldToInputConfig', () => {
153 it('should return an array of inputs', () => {
154 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
155 expect(inputs[0].name).toBe('id');
156 expect(inputs[0].type).toBe('number');
157 expect(inputs[0].label).toBe('Id');
158
159 expect(inputs[1].name).toBe('name');
160 expect(inputs[1].type).toBe('string');
161 expect(inputs[1].label).toBe('Name');
162 expect(inputs[1].validators.required).toBe(true);
163
164 expect(inputs[2].name).toBe('something');
165 expect(inputs[2].type).toBe('string');
166 expect(inputs[2].label).toBe('Something');
167 expect(inputs[2].validators.maxlength).toBe(30);
168
169 expect(inputs[3].name).toBe('else');
170 expect(inputs[3].type).toBe('number');
171 expect(inputs[3].label).toBe('Else');
172 expect(inputs[3].validators.min).toBe(20);
173 expect(inputs[3].validators.max).toBe(40);
174 });
175 });
176
177 describe('the modelToFormCfg method', () => {
178 it('should return a form config', () => {
179 const config: IXosFormConfig = service.modelToFormCfg(model);
180 expect(config.formName).toBe('TestForm');
181 expect(config.actions.length).toBe(1);
182 expect(config.actions[0].label).toBe('Save');
183 expect(config.actions[0].class).toBe('success');
184 expect(config.actions[0].icon).toBe('ok');
185 expect(config.actions[0].cb).toBeDefined();
186 expect(config.inputs.length).toBe(4);
187 });
188 });
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800189
190 describe('the private methods', () => {
191 let modelStoreMock, toastr, auth;
192
193 beforeEach(angular.mock.inject((_toastr_, AuthService) => {
194 modelStoreMock = {
195 query: () => {
196 const subject = new BehaviorSubject([
197 {id: 1, humanReadableName: 'test'},
198 {id: 2, humanReadableName: 'second'}
199 ]);
200 return subject.asObservable();
201 }
202 };
203 toastr = _toastr_;
204 auth = AuthService;
205 }));
206
207 const field: IXosModelDefsField = {
208 name: 'test',
209 type: 'number',
210 relation: {
211 model: 'Test',
212 type: 'many_to_one'
213 }
214 };
215
216 describe('the populateRelated method', () => {
217 const item = {
218 test: 2
219 };
220 it('should add the formatted data to the column definition', () => {
221 service = new ConfigHelpers(toastr, auth, modelStoreMock);
222 service['populateRelated'](item, item.test, field);
223 expect(item['test-formatted']).toBe('second');
224 });
225 });
226
227 describe('the populateSelectField', () => {
228
229 const input: IXosFormInput = {
230 name: 'test',
231 label: 'Test',
232 type: 'select',
233 validators: {}
234 };
235
236 it('should add the available choice to the select', () => {
237 service = new ConfigHelpers(toastr, auth, modelStoreMock);
238 service['populateSelectField'](field, input);
239 expect(input.options).toEqual([
240 {id: 1, label: 'test'},
241 {id: 2, label: 'second'}
242 ]);
243 });
244 });
245 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800246});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800247