Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-ui-router'; |
| 4 | |
| 5 | import {IXosConfigHelpersService} from './config.helpers'; |
| 6 | import {xosCore} from '../../index'; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 7 | import {IModeldef} from '../../../datasources/rest/modeldefs.rest'; |
| 8 | import {IXosTableCfg} from '../../table/table'; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 9 | import {IXosFormInput, IXosFormConfig} from '../../form/form'; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 10 | |
| 11 | let service: IXosConfigHelpersService; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 12 | |
| 13 | const 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 Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 24 | validators: { |
| 25 | required: true |
| 26 | } |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 27 | }, |
| 28 | { |
| 29 | type: 'string', |
| 30 | name: 'something', |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 31 | validators: { |
| 32 | maxlength: 30 |
| 33 | } |
| 34 | }, |
| 35 | { |
| 36 | type: 'number', |
| 37 | name: 'else', |
| 38 | validators: { |
| 39 | min: 20, |
| 40 | max: 40 |
| 41 | } |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 42 | }, |
| 43 | { |
| 44 | type: 'date', |
| 45 | name: 'updated', |
| 46 | validators: {} |
| 47 | }, |
| 48 | ] |
| 49 | }; |
| 50 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 51 | describe('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 Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 67 | expect(service.pluralize('Slice', 1)).toEqual('Slice'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 68 | }); |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 96 | expect(service.toLabels(strings)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 97 | }); |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 102 | expect(service.toLabels(strings, true)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 103 | }); |
| 104 | }); |
| 105 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 106 | 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 Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 121 | 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 Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 126 | }); |
| 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 Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 138 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 139 | 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 Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 177 | 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 Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 181 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 182 | |