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 | 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', |
| 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 Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 38 | describe('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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 82 | expect(service.toLabels(strings)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 83 | }); |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 88 | expect(service.toLabels(strings, true)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 89 | }); |
| 90 | }); |
| 91 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame^] | 92 | 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 Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 120 | }); |