Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-ui-router'; |
| 22 | |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 23 | import {IXosConfigHelpersService, ConfigHelpers, IXosModelDefsField} from './config.helpers'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 24 | import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest'; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 25 | import {IXosTableCfg} from '../../table/table'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 26 | import {IXosFormInput, IXosFormCfg} from '../../form/form'; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 27 | import {BehaviorSubject} from 'rxjs'; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 28 | import {XosFormHelpers} from '../../form/form-helpers'; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 29 | |
| 30 | let service: IXosConfigHelpersService; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 31 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 32 | const model: IXosModeldef = { |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 33 | name: 'Test', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 34 | app: 'test', |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 35 | fields: [ |
| 36 | { |
| 37 | type: 'number', |
| 38 | name: 'id', |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 39 | validators: [], |
| 40 | read_only: false |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 41 | }, |
| 42 | { |
| 43 | type: 'string', |
| 44 | name: 'name', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 45 | validators: [ |
| 46 | { |
| 47 | bool_value: true, |
| 48 | name: 'required' |
| 49 | } |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 50 | ], |
| 51 | read_only: false |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 52 | }, |
| 53 | { |
| 54 | type: 'string', |
| 55 | name: 'something', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 56 | validators: [ |
| 57 | { |
| 58 | int_value: 30, |
| 59 | name: 'maxlength' |
| 60 | } |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 61 | ], |
| 62 | read_only: false |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 63 | }, |
| 64 | { |
| 65 | type: 'number', |
| 66 | name: 'else', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 67 | validators: [ |
| 68 | { |
| 69 | int_value: 20, |
| 70 | name: 'min' |
| 71 | }, |
| 72 | { |
| 73 | int_value: 40, |
| 74 | name: 'max' |
| 75 | } |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 76 | ], |
| 77 | read_only: false |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 78 | }, |
| 79 | { |
| 80 | type: 'date', |
| 81 | name: 'updated', |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 82 | validators: [], |
| 83 | read_only: false |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 84 | }, |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 85 | ], |
| 86 | description: '', |
| 87 | verbose_name: '' |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 90 | describe('The ConfigHelpers service', () => { |
| 91 | |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 92 | beforeEach(() => { |
| 93 | angular |
| 94 | .module('test', ['toastr']) |
| 95 | .service('ConfigHelpers', ConfigHelpers) |
| 96 | .value('AuthService', { |
| 97 | getUser: () => { |
| 98 | return {id: 1}; |
| 99 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 100 | }) |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 101 | .value('XosModelStore', { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 102 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 103 | }) |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 104 | .service('XosFormHelpers', XosFormHelpers) |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 105 | .value('$state', { |
| 106 | get: () => { |
| 107 | return [ |
| 108 | { |
| 109 | name: 'xos.core.tests', |
| 110 | data: {model: 'Test'} |
| 111 | }, |
| 112 | { |
| 113 | name: 'xos.core.slices', |
| 114 | data: {model: 'Slices'} |
| 115 | } |
| 116 | ]; |
| 117 | } |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 118 | }); |
| 119 | angular.mock.module('test'); |
| 120 | }); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 121 | |
| 122 | beforeEach(angular.mock.inject(( |
| 123 | ConfigHelpers: IXosConfigHelpersService, |
| 124 | ) => { |
| 125 | service = ConfigHelpers; |
| 126 | })); |
| 127 | |
| 128 | describe('The pluralize function', () => { |
| 129 | it('should pluralize string', () => { |
| 130 | expect(service.pluralize('test')).toEqual('tests'); |
| 131 | expect(service.pluralize('test', 1)).toEqual('test'); |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 132 | expect(service.pluralize('xos')).toEqual('xoses'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 133 | expect(service.pluralize('slice')).toEqual('slices'); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 134 | expect(service.pluralize('Slice', 1)).toEqual('Slice'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 135 | }); |
| 136 | |
| 137 | it('should preprend count to string', () => { |
| 138 | expect(service.pluralize('test', 6, true)).toEqual('6 tests'); |
| 139 | expect(service.pluralize('test', 1, true)).toEqual('1 test'); |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | describe('the label formatter', () => { |
| 144 | it('should format a camel case string', () => { |
| 145 | expect(service.toLabel('camelCase')).toEqual('Camel case'); |
| 146 | }); |
| 147 | |
| 148 | it('should format a snake case string', () => { |
| 149 | expect(service.toLabel('snake_case')).toEqual('Snake case'); |
| 150 | }); |
| 151 | |
| 152 | it('should format a kebab case string', () => { |
| 153 | expect(service.toLabel('kebab-case')).toEqual('Kebab case'); |
| 154 | }); |
| 155 | |
| 156 | it('should set plural', () => { |
| 157 | expect(service.toLabel('kebab-case', true)).toEqual('Kebab cases'); |
| 158 | }); |
| 159 | |
| 160 | it('should format an array of strings', () => { |
| 161 | let strings: string[] = ['camelCase', 'snake_case', 'kebab-case']; |
| 162 | let labels = ['Camel case', 'Snake case', 'Kebab case']; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 163 | expect(service.toLabels(strings)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 164 | }); |
| 165 | |
| 166 | it('should set plural on an array of strings', () => { |
| 167 | let strings: string[] = ['camelCase', 'snake_case', 'kebab-case']; |
| 168 | let labels = ['Camel cases', 'Snake cases', 'Kebab cases']; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 169 | expect(service.toLabels(strings, true)).toEqual(labels); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 170 | }); |
| 171 | }); |
| 172 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 173 | describe('the navigation methods', () => { |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 174 | describe('stateFromCoreModels', () => { |
| 175 | |
| 176 | let state: ng.ui.IStateService; |
| 177 | |
| 178 | beforeEach(angular.mock.inject(($state) => { |
| 179 | state = $state; |
| 180 | })); |
| 181 | |
| 182 | it('should return the state for a given model', () => { |
| 183 | expect(service.stateFromCoreModel('Test')).toBe('xos.core.tests'); |
| 184 | }); |
Matteo Scandolo | 8248bca | 2017-08-09 13:46:04 -0700 | [diff] [blame] | 185 | }); |
| 186 | describe('stateWithParams', () => { |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 187 | it('should return the state with params for a given model', () => { |
| 188 | expect(service.stateWithParams('Test', {id: 1})).toBe('xos.core.tests({id: 1})'); |
| 189 | }); |
Matteo Scandolo | 8248bca | 2017-08-09 13:46:04 -0700 | [diff] [blame] | 190 | it('should return the state with params for a given relation', () => { |
| 191 | expect(service.relatedStateWithParams('Test', '1')).toBe('xos.core.tests({id: 1})'); |
| 192 | }); |
| 193 | |
| 194 | it('should return the state with params for usage in js', () => { |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 195 | expect(service.stateWithParamsForJs('Test', 1)).toEqual({ name: 'xos.core.tests', params: Object({ id: 1 }) }); |
Matteo Scandolo | 8248bca | 2017-08-09 13:46:04 -0700 | [diff] [blame] | 196 | }); |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 197 | }); |
| 198 | }); |
| 199 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 200 | describe('the modelFieldsToColumnsCfg method', () => { |
| 201 | it('should return an array of columns', () => { |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 202 | const cols = service.modelFieldsToColumnsCfg({fields: model.fields, name: 'testUrl', app: 'test', description: '', verbose_name: ''}); |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 203 | expect(cols[0].label).toBe('Id'); |
| 204 | expect(cols[0].prop).toBe('id'); |
| 205 | expect(cols[0].link).toBeDefined(); |
| 206 | |
| 207 | expect(cols[1].label).toBe('Name'); |
| 208 | expect(cols[1].prop).toBe('name'); |
| 209 | expect(cols[1].link).toBeDefined(); |
| 210 | |
| 211 | expect(cols[2].label).toBe('Something'); |
| 212 | expect(cols[2].prop).toBe('something'); |
| 213 | expect(cols[2].link).not.toBeDefined(); |
| 214 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 215 | expect(cols[3].label).toBe('Else'); |
| 216 | expect(cols[3].prop).toBe('else'); |
| 217 | expect(cols[3].link).not.toBeDefined(); |
| 218 | |
| 219 | expect(cols[4]).not.toBeDefined(); |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 220 | }); |
Matteo Scandolo | f1e68cd | 2017-09-05 17:30:34 -0700 | [diff] [blame] | 221 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 222 | }); |
| 223 | |
| 224 | describe('the modelToTableCfg method', () => { |
| 225 | it('should return a table config', () => { |
| 226 | const cfg: IXosTableCfg = service.modelToTableCfg(model, 'testUrl/:id?'); |
| 227 | expect(cfg.columns).toBeDefined(); |
| 228 | expect(cfg.filter).toBe('fulltext'); |
| 229 | expect(cfg.order).toEqual({field: 'id', reverse: false}); |
Matteo Scandolo | cc4bce8 | 2017-08-07 13:11:47 -0700 | [diff] [blame] | 230 | expect(cfg.actions.length).toBe(2); |
| 231 | expect(cfg.actions[0].label).toEqual('details'); |
| 232 | expect(cfg.actions[1].label).toEqual('delete'); |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 233 | }); |
| 234 | }); |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 235 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 236 | describe('the modelFieldToInputConfig', () => { |
| 237 | it('should return an array of inputs', () => { |
| 238 | const inputs: IXosFormInput[] = service.modelFieldToInputCfg(model.fields); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 239 | |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 240 | expect(inputs[0].name).toBe('name'); |
| 241 | expect(inputs[0].type).toBe('string'); |
| 242 | expect(inputs[0].label).toBe('Name'); |
| 243 | expect(inputs[0].validators.required).toBe(true); |
| 244 | |
| 245 | expect(inputs[1].name).toBe('something'); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 246 | expect(inputs[1].type).toBe('string'); |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 247 | expect(inputs[1].label).toBe('Something'); |
| 248 | expect(inputs[1].validators.maxlength).toBe(30); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 249 | |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 250 | expect(inputs[2].name).toBe('else'); |
| 251 | expect(inputs[2].type).toBe('number'); |
| 252 | expect(inputs[2].label).toBe('Else'); |
| 253 | expect(inputs[2].validators.min).toBe(20); |
| 254 | expect(inputs[2].validators.max).toBe(40); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 255 | }); |
Matteo Scandolo | f1e68cd | 2017-09-05 17:30:34 -0700 | [diff] [blame] | 256 | |
| 257 | it('should convert boolean defaults to real booleans', () => { |
| 258 | const fields: IXosModelDefsField[] = [ |
| 259 | { |
| 260 | type: 'boolean', |
| 261 | name: 'active', |
| 262 | default: '"True"', |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 263 | validators: [], |
| 264 | read_only: false |
Matteo Scandolo | f1e68cd | 2017-09-05 17:30:34 -0700 | [diff] [blame] | 265 | }, |
| 266 | { |
| 267 | type: 'boolean', |
| 268 | name: 'disabled', |
| 269 | default: '"False"', |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 270 | validators: [], |
| 271 | read_only: false |
Matteo Scandolo | f1e68cd | 2017-09-05 17:30:34 -0700 | [diff] [blame] | 272 | }, |
| 273 | ]; |
| 274 | const form_fields = service.modelFieldToInputCfg(fields); |
| 275 | expect(form_fields[0].default).toBe(true); |
| 276 | expect(form_fields[1].default).toBe(false); |
| 277 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 278 | }); |
| 279 | |
| 280 | describe('the modelToFormCfg method', () => { |
| 281 | it('should return a form config', () => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 282 | const config: IXosFormCfg = service.modelToFormCfg(model); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 283 | expect(config.formName).toBe('TestForm'); |
| 284 | expect(config.actions.length).toBe(1); |
| 285 | expect(config.actions[0].label).toBe('Save'); |
| 286 | expect(config.actions[0].class).toBe('success'); |
| 287 | expect(config.actions[0].icon).toBe('ok'); |
| 288 | expect(config.actions[0].cb).toBeDefined(); |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 289 | // NOTE 'id' and 'updated' are hidden fields |
| 290 | expect(config.inputs.length).toBe(3); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 291 | }); |
| 292 | }); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 293 | |
| 294 | describe('the private methods', () => { |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 295 | let modelStoreMock, q, toastr, auth, stateMock, XosFormHelpersMock; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 296 | |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 297 | beforeEach(angular.mock.inject(($q, _toastr_, AuthService, XosFormHelpers) => { |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 298 | modelStoreMock = { |
| 299 | query: () => { |
| 300 | const subject = new BehaviorSubject([ |
| 301 | {id: 1, humanReadableName: 'test'}, |
| 302 | {id: 2, humanReadableName: 'second'} |
| 303 | ]); |
| 304 | return subject.asObservable(); |
| 305 | } |
| 306 | }; |
| 307 | toastr = _toastr_; |
| 308 | auth = AuthService; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 309 | XosFormHelpersMock = XosFormHelpers; |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 310 | stateMock = { |
| 311 | get: '' |
| 312 | }; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 313 | q = $q; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 314 | })); |
| 315 | |
| 316 | const field: IXosModelDefsField = { |
| 317 | name: 'test', |
| 318 | type: 'number', |
| 319 | relation: { |
| 320 | model: 'Test', |
| 321 | type: 'many_to_one' |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 322 | }, |
| 323 | read_only: false |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | describe('the populateRelated method', () => { |
| 327 | const item = { |
| 328 | test: 2 |
| 329 | }; |
| 330 | it('should add the formatted data to the column definition', () => { |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 331 | service = new ConfigHelpers(q, stateMock, toastr, modelStoreMock, XosFormHelpersMock); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 332 | service['populateRelated'](item, item.test, field); |
| 333 | expect(item['test-formatted']).toBe('second'); |
| 334 | }); |
| 335 | }); |
| 336 | |
| 337 | describe('the populateSelectField', () => { |
| 338 | |
| 339 | const input: IXosFormInput = { |
| 340 | name: 'test', |
| 341 | label: 'Test', |
| 342 | type: 'select', |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 343 | validators: {}, |
| 344 | read_only: false |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | it('should add the available choice to the select', () => { |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 348 | service = new ConfigHelpers(q, stateMock, toastr, modelStoreMock, XosFormHelpersMock); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 349 | service['populateSelectField'](field, input); |
| 350 | expect(input.options).toEqual([ |
| 351 | {id: 1, label: 'test'}, |
| 352 | {id: 2, label: 'second'} |
| 353 | ]); |
| 354 | }); |
| 355 | }); |
Matteo Scandolo | 620cb49 | 2017-11-27 16:56:36 -0800 | [diff] [blame] | 356 | |
| 357 | describe('the removeExtraFields method', () => { |
| 358 | beforeEach(() => { |
| 359 | service = new ConfigHelpers(q, stateMock, toastr, modelStoreMock, XosFormHelpersMock); |
| 360 | }); |
| 361 | |
| 362 | it('should remove properties not defined in xProto', () => { |
| 363 | const model: IXosModeldef = { |
| 364 | name: 'Test', |
| 365 | app: 'test', |
| 366 | fields: [ |
| 367 | { |
| 368 | type: 'number', |
| 369 | name: 'foo', |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 370 | validators: [], |
| 371 | read_only: false |
Matteo Scandolo | 620cb49 | 2017-11-27 16:56:36 -0800 | [diff] [blame] | 372 | }, |
| 373 | { |
| 374 | type: 'string', |
| 375 | name: 'bar', |
| 376 | validators: [ |
| 377 | { |
| 378 | bool_value: true, |
| 379 | name: 'required' |
| 380 | } |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 381 | ], |
| 382 | read_only: false |
Matteo Scandolo | 620cb49 | 2017-11-27 16:56:36 -0800 | [diff] [blame] | 383 | } |
| 384 | ], |
| 385 | description: '', |
| 386 | verbose_name: '' |
| 387 | }; |
| 388 | |
| 389 | const item: any = { |
| 390 | foo: 1, |
| 391 | bar: 'existing', |
| 392 | baz: 'remove me' |
| 393 | }; |
| 394 | |
| 395 | const res = service['removeExtraFields'](item, model); |
| 396 | |
| 397 | expect(res).not.toHaveProp('baz'); |
| 398 | }); |
| 399 | }); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 400 | }); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 401 | }); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 402 | |