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 | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 5 | import {IXosConfigHelpersService, ConfigHelpers, IXosModelDefsField} from './config.helpers'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 6 | import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest'; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 7 | import {IXosTableCfg} from '../../table/table'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 8 | import {IXosFormInput, IXosFormCfg} from '../../form/form'; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 9 | import {BehaviorSubject} from 'rxjs'; |
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 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 13 | const model: IXosModeldef = { |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 14 | name: 'Test', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 15 | app: 'test', |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 16 | fields: [ |
| 17 | { |
| 18 | type: 'number', |
| 19 | name: 'id', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 20 | validators: [] |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 21 | }, |
| 22 | { |
| 23 | type: 'string', |
| 24 | name: 'name', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 25 | validators: [ |
| 26 | { |
| 27 | bool_value: true, |
| 28 | name: 'required' |
| 29 | } |
| 30 | ] |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 31 | }, |
| 32 | { |
| 33 | type: 'string', |
| 34 | name: 'something', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 35 | validators: [ |
| 36 | { |
| 37 | int_value: 30, |
| 38 | name: 'maxlength' |
| 39 | } |
| 40 | ] |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 41 | }, |
| 42 | { |
| 43 | type: 'number', |
| 44 | name: 'else', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 45 | validators: [ |
| 46 | { |
| 47 | int_value: 20, |
| 48 | name: 'min' |
| 49 | }, |
| 50 | { |
| 51 | int_value: 40, |
| 52 | name: 'max' |
| 53 | } |
| 54 | ] |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 55 | }, |
| 56 | { |
| 57 | type: 'date', |
| 58 | name: 'updated', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 59 | validators: [] |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 60 | }, |
| 61 | ] |
| 62 | }; |
| 63 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 64 | describe('The ConfigHelpers service', () => { |
| 65 | |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 66 | beforeEach(() => { |
| 67 | angular |
| 68 | .module('test', ['toastr']) |
| 69 | .service('ConfigHelpers', ConfigHelpers) |
| 70 | .value('AuthService', { |
| 71 | getUser: () => { |
| 72 | return {id: 1}; |
| 73 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 74 | }) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 75 | .value('XosModelStore', { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 76 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 77 | }) |
| 78 | .value('$state', { |
| 79 | get: () => { |
| 80 | return [ |
| 81 | { |
| 82 | name: 'xos.core.tests', |
| 83 | data: {model: 'Test'} |
| 84 | }, |
| 85 | { |
| 86 | name: 'xos.core.slices', |
| 87 | data: {model: 'Slices'} |
| 88 | } |
| 89 | ]; |
| 90 | } |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 91 | }); |
| 92 | angular.mock.module('test'); |
| 93 | }); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 94 | |
| 95 | beforeEach(angular.mock.inject(( |
| 96 | ConfigHelpers: IXosConfigHelpersService, |
| 97 | ) => { |
| 98 | service = ConfigHelpers; |
| 99 | })); |
| 100 | |
| 101 | describe('The pluralize function', () => { |
| 102 | it('should pluralize string', () => { |
| 103 | expect(service.pluralize('test')).toEqual('tests'); |
| 104 | expect(service.pluralize('test', 1)).toEqual('test'); |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 105 | expect(service.pluralize('xos')).toEqual('xoses'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 106 | expect(service.pluralize('slice')).toEqual('slices'); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 107 | expect(service.pluralize('Slice', 1)).toEqual('Slice'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 108 | }); |
| 109 | |
| 110 | it('should preprend count to string', () => { |
| 111 | expect(service.pluralize('test', 6, true)).toEqual('6 tests'); |
| 112 | expect(service.pluralize('test', 1, true)).toEqual('1 test'); |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | describe('the label formatter', () => { |
| 117 | it('should format a camel case string', () => { |
| 118 | expect(service.toLabel('camelCase')).toEqual('Camel case'); |
| 119 | }); |
| 120 | |
| 121 | it('should format a snake case string', () => { |
| 122 | expect(service.toLabel('snake_case')).toEqual('Snake case'); |
| 123 | }); |
| 124 | |
| 125 | it('should format a kebab case string', () => { |
| 126 | expect(service.toLabel('kebab-case')).toEqual('Kebab case'); |
| 127 | }); |
| 128 | |
| 129 | it('should set plural', () => { |
| 130 | expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases'); |
| 131 | }); |
| 132 | |
| 133 | it('should format an array of strings', () => { |
| 134 | let strings: string[] = ['camelCase', 'snake_case', 'kebab-case']; |
| 135 | let labels = ['Camel case', 'Snake case', 'Kebab case']; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 136 | expect(service.toLabels(strings)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 137 | }); |
| 138 | |
| 139 | it('should set plural on an array of strings', () => { |
| 140 | let strings: string[] = ['camelCase', 'snake_case', 'kebab-case']; |
| 141 | let labels = ['Camel cases', 'Snake cases', 'Kebab cases']; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 142 | expect(service.toLabels(strings, true)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 143 | }); |
| 144 | }); |
| 145 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 146 | describe('the navigation methods', () => { |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 147 | describe('stateFromCoreModels', () => { |
| 148 | |
| 149 | let state: ng.ui.IStateService; |
| 150 | |
| 151 | beforeEach(angular.mock.inject(($state) => { |
| 152 | state = $state; |
| 153 | })); |
| 154 | |
| 155 | it('should return the state for a given model', () => { |
| 156 | expect(service.stateFromCoreModel('Test')).toBe('xos.core.tests'); |
| 157 | }); |
| 158 | |
| 159 | it('should return the state with params for a given model', () => { |
| 160 | expect(service.stateWithParams('Test', {id: 1})).toBe('xos.core.tests({id: 1})'); |
| 161 | }); |
| 162 | }); |
| 163 | }); |
| 164 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 165 | describe('the modelFieldsToColumnsCfg method', () => { |
| 166 | it('should return an array of columns', () => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 167 | const cols = service.modelFieldsToColumnsCfg({fields: model.fields, name: 'testUrl', app: 'test'}); |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 168 | expect(cols[0].label).toBe('Id'); |
| 169 | expect(cols[0].prop).toBe('id'); |
| 170 | expect(cols[0].link).toBeDefined(); |
| 171 | |
| 172 | expect(cols[1].label).toBe('Name'); |
| 173 | expect(cols[1].prop).toBe('name'); |
| 174 | expect(cols[1].link).toBeDefined(); |
| 175 | |
| 176 | expect(cols[2].label).toBe('Something'); |
| 177 | expect(cols[2].prop).toBe('something'); |
| 178 | expect(cols[2].link).not.toBeDefined(); |
| 179 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 180 | expect(cols[3].label).toBe('Else'); |
| 181 | expect(cols[3].prop).toBe('else'); |
| 182 | expect(cols[3].link).not.toBeDefined(); |
| 183 | |
| 184 | expect(cols[4]).not.toBeDefined(); |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 185 | }); |
| 186 | }); |
| 187 | |
| 188 | describe('the modelToTableCfg method', () => { |
| 189 | it('should return a table config', () => { |
| 190 | const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?'); |
| 191 | expect(cfg.columns).toBeDefined(); |
| 192 | expect(cfg.filter).toBe('fulltext'); |
| 193 | expect(cfg.order).toEqual({field: 'id', reverse: false}); |
Matteo Scandolo | 2c61b88 | 2017-08-07 13:11:47 -0700 | [diff] [blame^] | 194 | expect(cfg.actions.length).toBe(2); |
| 195 | expect(cfg.actions[0].label).toEqual('details'); |
| 196 | expect(cfg.actions[1].label).toEqual('delete'); |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 197 | }); |
| 198 | }); |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 199 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 200 | describe('the modelFieldToInputConfig', () => { |
| 201 | it('should return an array of inputs', () => { |
| 202 | const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields); |
| 203 | expect(inputs[0].name).toBe('id'); |
| 204 | expect(inputs[0].type).toBe('number'); |
| 205 | expect(inputs[0].label).toBe('Id'); |
| 206 | |
| 207 | expect(inputs[1].name).toBe('name'); |
| 208 | expect(inputs[1].type).toBe('string'); |
| 209 | expect(inputs[1].label).toBe('Name'); |
| 210 | expect(inputs[1].validators.required).toBe(true); |
| 211 | |
| 212 | expect(inputs[2].name).toBe('something'); |
| 213 | expect(inputs[2].type).toBe('string'); |
| 214 | expect(inputs[2].label).toBe('Something'); |
| 215 | expect(inputs[2].validators.maxlength).toBe(30); |
| 216 | |
| 217 | expect(inputs[3].name).toBe('else'); |
| 218 | expect(inputs[3].type).toBe('number'); |
| 219 | expect(inputs[3].label).toBe('Else'); |
| 220 | expect(inputs[3].validators.min).toBe(20); |
| 221 | expect(inputs[3].validators.max).toBe(40); |
| 222 | }); |
| 223 | }); |
| 224 | |
| 225 | describe('the modelToFormCfg method', () => { |
| 226 | it('should return a form config', () => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 227 | const config: IXosFormCfg = service.modelToFormCfg(model); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 228 | expect(config.formName).toBe('TestForm'); |
| 229 | expect(config.actions.length).toBe(1); |
| 230 | expect(config.actions[0].label).toBe('Save'); |
| 231 | expect(config.actions[0].class).toBe('success'); |
| 232 | expect(config.actions[0].icon).toBe('ok'); |
| 233 | expect(config.actions[0].cb).toBeDefined(); |
| 234 | expect(config.inputs.length).toBe(4); |
| 235 | }); |
| 236 | }); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 237 | |
| 238 | describe('the private methods', () => { |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 239 | let modelStoreMock, toastr, auth, stateMock; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 240 | |
| 241 | beforeEach(angular.mock.inject((_toastr_, AuthService) => { |
| 242 | modelStoreMock = { |
| 243 | query: () => { |
| 244 | const subject = new BehaviorSubject([ |
| 245 | {id: 1, humanReadableName: 'test'}, |
| 246 | {id: 2, humanReadableName: 'second'} |
| 247 | ]); |
| 248 | return subject.asObservable(); |
| 249 | } |
| 250 | }; |
| 251 | toastr = _toastr_; |
| 252 | auth = AuthService; |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 253 | stateMock = { |
| 254 | get: '' |
| 255 | }; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 256 | })); |
| 257 | |
| 258 | const field: IXosModelDefsField = { |
| 259 | name: 'test', |
| 260 | type: 'number', |
| 261 | relation: { |
| 262 | model: 'Test', |
| 263 | type: 'many_to_one' |
| 264 | } |
| 265 | }; |
| 266 | |
| 267 | describe('the populateRelated method', () => { |
| 268 | const item = { |
| 269 | test: 2 |
| 270 | }; |
| 271 | it('should add the formatted data to the column definition', () => { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 272 | service = new ConfigHelpers(stateMock, toastr, modelStoreMock); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 273 | service['populateRelated'](item, item.test, field); |
| 274 | expect(item['test-formatted']).toBe('second'); |
| 275 | }); |
| 276 | }); |
| 277 | |
| 278 | describe('the populateSelectField', () => { |
| 279 | |
| 280 | const input: IXosFormInput = { |
| 281 | name: 'test', |
| 282 | label: 'Test', |
| 283 | type: 'select', |
| 284 | validators: {} |
| 285 | }; |
| 286 | |
| 287 | it('should add the available choice to the select', () => { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 288 | service = new ConfigHelpers(stateMock, toastr, modelStoreMock); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 289 | service['populateSelectField'](field, input); |
| 290 | expect(input.options).toEqual([ |
| 291 | {id: 1, label: 'test'}, |
| 292 | {id: 2, label: 'second'} |
| 293 | ]); |
| 294 | }); |
| 295 | }); |
| 296 | }); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 297 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 298 | |