blob: b39f444edaad16c90d0d4d738f709b30eedca3f5 [file] [log] [blame]
Matteo Scandolod58d5042016-12-16 16:59:21 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-ui-router';
4
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -08005import {IXosConfigHelpersService, ConfigHelpers} 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 Scandolod58d5042016-12-16 16:59:21 -08009
10let service: IXosConfigHelpersService;
Matteo Scandolocb466ed2017-01-04 17:16:24 -080011
12const model: IModeldef = {
13 name: 'Test',
14 fields: [
15 {
16 type: 'number',
17 name: 'id',
18 validators: {}
19 },
20 {
21 type: 'string',
22 name: 'name',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080023 validators: {
24 required: true
25 }
Matteo Scandolocb466ed2017-01-04 17:16:24 -080026 },
27 {
28 type: 'string',
29 name: 'something',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080030 validators: {
31 maxlength: 30
32 }
33 },
34 {
35 type: 'number',
36 name: 'else',
37 validators: {
38 min: 20,
39 max: 40
40 }
Matteo Scandolocb466ed2017-01-04 17:16:24 -080041 },
42 {
43 type: 'date',
44 name: 'updated',
45 validators: {}
46 },
47 ]
48};
49
Matteo Scandolod58d5042016-12-16 16:59:21 -080050describe('The ConfigHelpers service', () => {
51
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080052 beforeEach(() => {
53 angular
54 .module('test', ['toastr'])
55 .service('ConfigHelpers', ConfigHelpers)
56 .value('AuthService', {
57 getUser: () => {
58 return {id: 1};
59 }
60 });
61 angular.mock.module('test');
62 });
Matteo Scandolod58d5042016-12-16 16:59:21 -080063
64 beforeEach(angular.mock.inject((
65 ConfigHelpers: IXosConfigHelpersService,
66 ) => {
67 service = ConfigHelpers;
68 }));
69
70 describe('The pluralize function', () => {
71 it('should pluralize string', () => {
72 expect(service.pluralize('test')).toEqual('tests');
73 expect(service.pluralize('test', 1)).toEqual('test');
74 expect(service.pluralize('xos')).toEqual('xosses');
75 expect(service.pluralize('slice')).toEqual('slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -080076 expect(service.pluralize('Slice', 1)).toEqual('Slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -080077 });
78
79 it('should preprend count to string', () => {
80 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
81 expect(service.pluralize('test', 1, true)).toEqual('1 test');
82 });
83 });
84
85 describe('the label formatter', () => {
86 it('should format a camel case string', () => {
87 expect(service.toLabel('camelCase')).toEqual('Camel case');
88 });
89
90 it('should format a snake case string', () => {
91 expect(service.toLabel('snake_case')).toEqual('Snake case');
92 });
93
94 it('should format a kebab case string', () => {
95 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
96 });
97
98 it('should set plural', () => {
99 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
100 });
101
102 it('should format an array of strings', () => {
103 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
104 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800105 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800106 });
107
108 it('should set plural on an array of strings', () => {
109 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
110 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800111 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800112 });
113 });
114
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800115 describe('the modelFieldsToColumnsCfg method', () => {
116 it('should return an array of columns', () => {
117 const cols = service.modelFieldsToColumnsCfg(model.fields, 'testUrl/:id?');
118 expect(cols[0].label).toBe('Id');
119 expect(cols[0].prop).toBe('id');
120 expect(cols[0].link).toBeDefined();
121
122 expect(cols[1].label).toBe('Name');
123 expect(cols[1].prop).toBe('name');
124 expect(cols[1].link).toBeDefined();
125
126 expect(cols[2].label).toBe('Something');
127 expect(cols[2].prop).toBe('something');
128 expect(cols[2].link).not.toBeDefined();
129
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800130 expect(cols[3].label).toBe('Else');
131 expect(cols[3].prop).toBe('else');
132 expect(cols[3].link).not.toBeDefined();
133
134 expect(cols[4]).not.toBeDefined();
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800135 });
136 });
137
138 describe('the modelToTableCfg method', () => {
139 it('should return a table config', () => {
140 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
141 expect(cfg.columns).toBeDefined();
142 expect(cfg.filter).toBe('fulltext');
143 expect(cfg.order).toEqual({field: 'id', reverse: false});
144 expect(cfg.actions.length).toBe(1);
145 });
146 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800147
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800148 describe('the modelFieldToInputConfig', () => {
149 it('should return an array of inputs', () => {
150 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
151 expect(inputs[0].name).toBe('id');
152 expect(inputs[0].type).toBe('number');
153 expect(inputs[0].label).toBe('Id');
154
155 expect(inputs[1].name).toBe('name');
156 expect(inputs[1].type).toBe('string');
157 expect(inputs[1].label).toBe('Name');
158 expect(inputs[1].validators.required).toBe(true);
159
160 expect(inputs[2].name).toBe('something');
161 expect(inputs[2].type).toBe('string');
162 expect(inputs[2].label).toBe('Something');
163 expect(inputs[2].validators.maxlength).toBe(30);
164
165 expect(inputs[3].name).toBe('else');
166 expect(inputs[3].type).toBe('number');
167 expect(inputs[3].label).toBe('Else');
168 expect(inputs[3].validators.min).toBe(20);
169 expect(inputs[3].validators.max).toBe(40);
170 });
171 });
172
173 describe('the modelToFormCfg method', () => {
174 it('should return a form config', () => {
175 const config: IXosFormConfig = service.modelToFormCfg(model);
176 expect(config.formName).toBe('TestForm');
177 expect(config.actions.length).toBe(1);
178 expect(config.actions[0].label).toBe('Save');
179 expect(config.actions[0].class).toBe('success');
180 expect(config.actions[0].icon).toBe('ok');
181 expect(config.actions[0].cb).toBeDefined();
182 expect(config.inputs.length).toBe(4);
183 });
184 });
185
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800186 it('should convert a core model name in an URL', () => {
187 expect(service.urlFromCoreModel('Slice')).toBe('/core/slices');
188 expect(service.urlFromCoreModel('Xos')).toBe('/core/xosses');
189 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800190});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800191