blob: beec2e0d82db9abf1d7e11c7e69e55ea6931284c [file] [log] [blame]
Matteo Scandolod58d5042016-12-16 16:59:21 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-ui-router';
4
5import {IXosConfigHelpersService} from './config.helpers';
6import {xosCore} from '../../index';
Matteo Scandolocb466ed2017-01-04 17:16:24 -08007import {IModeldef} from '../../../datasources/rest/modeldefs.rest';
8import {IXosTableCfg} from '../../table/table';
Matteo Scandolo80c3a652017-01-06 10:48:31 -08009import {IXosFormInput, IXosFormConfig} from '../../form/form';
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
53 beforeEach(angular.mock.module(xosCore));
54
55 beforeEach(angular.mock.inject((
56 ConfigHelpers: IXosConfigHelpersService,
57 ) => {
58 service = ConfigHelpers;
59 }));
60
61 describe('The pluralize function', () => {
62 it('should pluralize string', () => {
63 expect(service.pluralize('test')).toEqual('tests');
64 expect(service.pluralize('test', 1)).toEqual('test');
65 expect(service.pluralize('xos')).toEqual('xosses');
66 expect(service.pluralize('slice')).toEqual('slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -080067 expect(service.pluralize('Slice', 1)).toEqual('Slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -080068 });
69
70 it('should preprend count to string', () => {
71 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
72 expect(service.pluralize('test', 1, true)).toEqual('1 test');
73 });
74 });
75
76 describe('the label formatter', () => {
77 it('should format a camel case string', () => {
78 expect(service.toLabel('camelCase')).toEqual('Camel case');
79 });
80
81 it('should format a snake case string', () => {
82 expect(service.toLabel('snake_case')).toEqual('Snake case');
83 });
84
85 it('should format a kebab case string', () => {
86 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
87 });
88
89 it('should set plural', () => {
90 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
91 });
92
93 it('should format an array of strings', () => {
94 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
95 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080096 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -080097 });
98
99 it('should set plural on an array of strings', () => {
100 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
101 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800102 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -0800103 });
104 });
105
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800106 describe('the modelFieldsToColumnsCfg method', () => {
107 it('should return an array of columns', () => {
108 const cols = service.modelFieldsToColumnsCfg(model.fields, 'testUrl/:id?');
109 expect(cols[0].label).toBe('Id');
110 expect(cols[0].prop).toBe('id');
111 expect(cols[0].link).toBeDefined();
112
113 expect(cols[1].label).toBe('Name');
114 expect(cols[1].prop).toBe('name');
115 expect(cols[1].link).toBeDefined();
116
117 expect(cols[2].label).toBe('Something');
118 expect(cols[2].prop).toBe('something');
119 expect(cols[2].link).not.toBeDefined();
120
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800121 expect(cols[3].label).toBe('Else');
122 expect(cols[3].prop).toBe('else');
123 expect(cols[3].link).not.toBeDefined();
124
125 expect(cols[4]).not.toBeDefined();
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800126 });
127 });
128
129 describe('the modelToTableCfg method', () => {
130 it('should return a table config', () => {
131 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
132 expect(cfg.columns).toBeDefined();
133 expect(cfg.filter).toBe('fulltext');
134 expect(cfg.order).toEqual({field: 'id', reverse: false});
135 expect(cfg.actions.length).toBe(1);
136 });
137 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800138
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800139 describe('the modelFieldToInputConfig', () => {
140 it('should return an array of inputs', () => {
141 const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields);
142 expect(inputs[0].name).toBe('id');
143 expect(inputs[0].type).toBe('number');
144 expect(inputs[0].label).toBe('Id');
145
146 expect(inputs[1].name).toBe('name');
147 expect(inputs[1].type).toBe('string');
148 expect(inputs[1].label).toBe('Name');
149 expect(inputs[1].validators.required).toBe(true);
150
151 expect(inputs[2].name).toBe('something');
152 expect(inputs[2].type).toBe('string');
153 expect(inputs[2].label).toBe('Something');
154 expect(inputs[2].validators.maxlength).toBe(30);
155
156 expect(inputs[3].name).toBe('else');
157 expect(inputs[3].type).toBe('number');
158 expect(inputs[3].label).toBe('Else');
159 expect(inputs[3].validators.min).toBe(20);
160 expect(inputs[3].validators.max).toBe(40);
161 });
162 });
163
164 describe('the modelToFormCfg method', () => {
165 it('should return a form config', () => {
166 const config: IXosFormConfig = service.modelToFormCfg(model);
167 expect(config.formName).toBe('TestForm');
168 expect(config.actions.length).toBe(1);
169 expect(config.actions[0].label).toBe('Save');
170 expect(config.actions[0].class).toBe('success');
171 expect(config.actions[0].icon).toBe('ok');
172 expect(config.actions[0].cb).toBeDefined();
173 expect(config.inputs.length).toBe(4);
174 });
175 });
176
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800177 it('should convert a core model name in an URL', () => {
178 expect(service.urlFromCoreModel('Slice')).toBe('/core/slices');
179 expect(service.urlFromCoreModel('Xos')).toBe('/core/xosses');
180 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800181});
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800182