blob: 28ba22b7413467b1df02e86f49bd70c33457bea7 [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 }
Matteo Scandolo04964232017-01-07 12:53:46 -080060 })
61 .value('ModelStore', {
62
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080063 });
64 angular.mock.module('test');
65 });
Matteo Scandolod58d5042016-12-16 16:59:21 -080066
67 beforeEach(angular.mock.inject((
68 ConfigHelpers: IXosConfigHelpersService,
69 ) => {
70 service = ConfigHelpers;
71 }));
72
73 describe('The pluralize function', () => {
74 it('should pluralize string', () => {
75 expect(service.pluralize('test')).toEqual('tests');
76 expect(service.pluralize('test', 1)).toEqual('test');
77 expect(service.pluralize('xos')).toEqual('xosses');
78 expect(service.pluralize('slice')).toEqual('slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -080079 expect(service.pluralize('Slice', 1)).toEqual('Slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -080080 });
81
82 it('should preprend count to string', () => {
83 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
84 expect(service.pluralize('test', 1, true)).toEqual('1 test');
85 });
86 });
87
88 describe('the label formatter', () => {
89 it('should format a camel case string', () => {
90 expect(service.toLabel('camelCase')).toEqual('Camel case');
91 });
92
93 it('should format a snake case string', () => {
94 expect(service.toLabel('snake_case')).toEqual('Snake case');
95 });
96
97 it('should format a kebab case string', () => {
98 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
99 });
100
101 it('should set plural', () => {
102 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
103 });
104
105 it('should format an array of strings', () => {
106 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
107 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800108 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800109 });
110
111 it('should set plural on an array of strings', () => {
112 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
113 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800114 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800115 });
116 });
117
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800118 describe('the modelFieldsToColumnsCfg method', () => {
119 it('should return an array of columns', () => {
120 const cols = service.modelFieldsToColumnsCfg(model.fields, 'testUrl/:id?');
121 expect(cols[0].label).toBe('Id');
122 expect(cols[0].prop).toBe('id');
123 expect(cols[0].link).toBeDefined();
124
125 expect(cols[1].label).toBe('Name');
126 expect(cols[1].prop).toBe('name');
127 expect(cols[1].link).toBeDefined();
128
129 expect(cols[2].label).toBe('Something');
130 expect(cols[2].prop).toBe('something');
131 expect(cols[2].link).not.toBeDefined();
132
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800133 expect(cols[3].label).toBe('Else');
134 expect(cols[3].prop).toBe('else');
135 expect(cols[3].link).not.toBeDefined();
136
137 expect(cols[4]).not.toBeDefined();
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800138 });
139 });
140
141 describe('the modelToTableCfg method', () => {
142 it('should return a table config', () => {
143 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
144 expect(cfg.columns).toBeDefined();
145 expect(cfg.filter).toBe('fulltext');
146 expect(cfg.order).toEqual({field: 'id', reverse: false});
147 expect(cfg.actions.length).toBe(1);
148 });
149 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800150
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800151 describe('the modelFieldToInputConfig', () => {
152 it('should return an array of inputs', () => {
153 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
154 expect(inputs[0].name).toBe('id');
155 expect(inputs[0].type).toBe('number');
156 expect(inputs[0].label).toBe('Id');
157
158 expect(inputs[1].name).toBe('name');
159 expect(inputs[1].type).toBe('string');
160 expect(inputs[1].label).toBe('Name');
161 expect(inputs[1].validators.required).toBe(true);
162
163 expect(inputs[2].name).toBe('something');
164 expect(inputs[2].type).toBe('string');
165 expect(inputs[2].label).toBe('Something');
166 expect(inputs[2].validators.maxlength).toBe(30);
167
168 expect(inputs[3].name).toBe('else');
169 expect(inputs[3].type).toBe('number');
170 expect(inputs[3].label).toBe('Else');
171 expect(inputs[3].validators.min).toBe(20);
172 expect(inputs[3].validators.max).toBe(40);
173 });
174 });
175
176 describe('the modelToFormCfg method', () => {
177 it('should return a form config', () => {
178 const config: IXosFormConfig = service.modelToFormCfg(model);
179 expect(config.formName).toBe('TestForm');
180 expect(config.actions.length).toBe(1);
181 expect(config.actions[0].label).toBe('Save');
182 expect(config.actions[0].class).toBe('success');
183 expect(config.actions[0].icon).toBe('ok');
184 expect(config.actions[0].cb).toBeDefined();
185 expect(config.inputs.length).toBe(4);
186 });
187 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800188});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800189