blob: 432bd7e0b5d14397b8524f829e996ee59bbb095b [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 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',
23 validators: {}
24 },
25 {
26 type: 'string',
27 name: 'something',
28 validators: {}
29 },
30 {
31 type: 'date',
32 name: 'updated',
33 validators: {}
34 },
35 ]
36};
37
Matteo Scandolod58d5042016-12-16 16:59:21 -080038describe('The ConfigHelpers service', () => {
39
40 beforeEach(angular.mock.module(xosCore));
41
42 beforeEach(angular.mock.inject((
43 ConfigHelpers: IXosConfigHelpersService,
44 ) => {
45 service = ConfigHelpers;
46 }));
47
48 describe('The pluralize function', () => {
49 it('should pluralize string', () => {
50 expect(service.pluralize('test')).toEqual('tests');
51 expect(service.pluralize('test', 1)).toEqual('test');
52 expect(service.pluralize('xos')).toEqual('xosses');
53 expect(service.pluralize('slice')).toEqual('slices');
54 });
55
56 it('should preprend count to string', () => {
57 expect(service.pluralize('test', 6, true)).toEqual('6 tests');
58 expect(service.pluralize('test', 1, true)).toEqual('1 test');
59 });
60 });
61
62 describe('the label formatter', () => {
63 it('should format a camel case string', () => {
64 expect(service.toLabel('camelCase')).toEqual('Camel case');
65 });
66
67 it('should format a snake case string', () => {
68 expect(service.toLabel('snake_case')).toEqual('Snake case');
69 });
70
71 it('should format a kebab case string', () => {
72 expect(service.toLabel('kebab-case')).toEqual('Kebab case');
73 });
74
75 it('should set plural', () => {
76 expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases');
77 });
78
79 it('should format an array of strings', () => {
80 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
81 let labels = ['Camel case', 'Snake case', 'Kebab case'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080082 expect(service.toLabels(strings)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -080083 });
84
85 it('should set plural on an array of strings', () => {
86 let strings: string[] = ['camelCase', 'snake_case', 'kebab-case'];
87 let labels = ['Camel cases', 'Snake cases', 'Kebab cases'];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080088 expect(service.toLabels(strings, true)).toEqual(labels);
Matteo Scandolod58d5042016-12-16 16:59:21 -080089 });
90 });
91
Matteo Scandolocb466ed2017-01-04 17:16:24 -080092 describe('the modelFieldsToColumnsCfg method', () => {
93 it('should return an array of columns', () => {
94 const cols = service.modelFieldsToColumnsCfg(model.fields, 'testUrl/:id?');
95 expect(cols[0].label).toBe('Id');
96 expect(cols[0].prop).toBe('id');
97 expect(cols[0].link).toBeDefined();
98
99 expect(cols[1].label).toBe('Name');
100 expect(cols[1].prop).toBe('name');
101 expect(cols[1].link).toBeDefined();
102
103 expect(cols[2].label).toBe('Something');
104 expect(cols[2].prop).toBe('something');
105 expect(cols[2].link).not.toBeDefined();
106
107 expect(cols[3]).not.toBeDefined();
108 });
109 });
110
111 describe('the modelToTableCfg method', () => {
112 it('should return a table config', () => {
113 const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?');
114 expect(cfg.columns).toBeDefined();
115 expect(cfg.filter).toBe('fulltext');
116 expect(cfg.order).toEqual({field: 'id', reverse: false});
117 expect(cfg.actions.length).toBe(1);
118 });
119 });
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800120
121 it('should convert a core model name in an URL', () => {
122 expect(service.urlFromCoreModel('Slice')).toBe('/core/slices');
123 expect(service.urlFromCoreModel('Xos')).toBe('/core/xosses');
124 });
Matteo Scandolod58d5042016-12-16 16:59:21 -0800125});