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 | |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 5 | import {IXosConfigHelpersService, ConfigHelpers} from './config.helpers'; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 6 | import {IModeldef} from '../../../datasources/rest/modeldefs.rest'; |
| 7 | import {IXosTableCfg} from '../../table/table'; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 8 | import {IXosFormInput, IXosFormConfig} from '../../form/form'; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 9 | |
| 10 | let service: IXosConfigHelpersService; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 11 | |
| 12 | const 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 Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 23 | validators: { |
| 24 | required: true |
| 25 | } |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 26 | }, |
| 27 | { |
| 28 | type: 'string', |
| 29 | name: 'something', |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 30 | validators: { |
| 31 | maxlength: 30 |
| 32 | } |
| 33 | }, |
| 34 | { |
| 35 | type: 'number', |
| 36 | name: 'else', |
| 37 | validators: { |
| 38 | min: 20, |
| 39 | max: 40 |
| 40 | } |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 41 | }, |
| 42 | { |
| 43 | type: 'date', |
| 44 | name: 'updated', |
| 45 | validators: {} |
| 46 | }, |
| 47 | ] |
| 48 | }; |
| 49 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 50 | describe('The ConfigHelpers service', () => { |
| 51 | |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 52 | beforeEach(() => { |
| 53 | angular |
| 54 | .module('test', ['toastr']) |
| 55 | .service('ConfigHelpers', ConfigHelpers) |
| 56 | .value('AuthService', { |
| 57 | getUser: () => { |
| 58 | return {id: 1}; |
| 59 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame^] | 60 | }) |
| 61 | .value('ModelStore', { |
| 62 | |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 63 | }); |
| 64 | angular.mock.module('test'); |
| 65 | }); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 66 | |
| 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 Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 79 | expect(service.pluralize('Slice', 1)).toEqual('Slice'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 80 | }); |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 108 | expect(service.toLabels(strings)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 109 | }); |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 114 | expect(service.toLabels(strings, true)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 115 | }); |
| 116 | }); |
| 117 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 118 | 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 Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 133 | 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 Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 138 | }); |
| 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 Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 150 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 151 | 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 Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 188 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 189 | |