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 | b2225a0 | 2017-04-11 15:23:04 -0700 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import * as $ from 'jquery'; |
| 21 | import 'angular-mocks'; |
| 22 | import {xosForm, IXosFormCfg, IXosFormInput} from './form'; |
| 23 | import {XosFormHelpers} from './form-helpers'; |
| 24 | import {xosField} from '../field/field'; |
| 25 | import {xosAlert} from '../alert/alert'; |
| 26 | |
| 27 | describe('The Xos Form component', () => { |
| 28 | |
| 29 | let element, scope, isolatedScope, rootScope, compile; |
| 30 | |
| 31 | const compileElement = () => { |
| 32 | |
| 33 | if (!scope) { |
| 34 | scope = rootScope.$new(); |
| 35 | } |
| 36 | |
| 37 | element = angular.element(`<xos-form config="config" ng-model="model"></xos-form>`); |
| 38 | compile(element)(scope); |
| 39 | scope.$digest(); |
| 40 | isolatedScope = element.isolateScope().vm; |
| 41 | }; |
| 42 | |
| 43 | const MockConfigHelpers = { |
| 44 | toLabel: string => string |
| 45 | }; |
| 46 | |
| 47 | beforeEach(() => { |
| 48 | |
| 49 | angular |
| 50 | .module('form', []) |
| 51 | .component('xosForm', xosForm) |
| 52 | .service('XosFormHelpers', XosFormHelpers) |
| 53 | .component('xosField', xosField) // NOTE does it make sense to mock it? |
| 54 | .component('xosAlert', xosAlert) |
| 55 | .value('ConfigHelpers', MockConfigHelpers); |
| 56 | |
| 57 | angular.mock.module('form'); |
| 58 | |
| 59 | inject(($compile: ng.ICompileService, $rootScope: ng.IScope) => { |
| 60 | rootScope = $rootScope; |
| 61 | compile = $compile; |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | it('should throw an error if no config is specified', () => { |
| 66 | function errorFunctionWrapper() { |
| 67 | compileElement(); |
| 68 | } |
| 69 | expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide a configuration via the "config" attribute')); |
| 70 | }); |
| 71 | |
| 72 | it('should throw an error if no actions are specified in config', () => { |
| 73 | function errorFunctionWrapper() { |
| 74 | scope = rootScope.$new(); |
| 75 | scope.config = 'green'; |
| 76 | compileElement(); |
| 77 | } |
| 78 | expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide an action list in the configuration')); |
| 79 | }); |
| 80 | |
| 81 | it('should throw an error if no formName is specified in config', () => { |
| 82 | function errorFunctionWrapper() { |
| 83 | scope = rootScope.$new(); |
| 84 | scope.config = { |
| 85 | actions: [] |
| 86 | }; |
| 87 | compileElement(); |
| 88 | } |
| 89 | expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide a formName property in the config')); |
| 90 | }); |
| 91 | |
| 92 | describe('when correctly configured', () => { |
| 93 | |
| 94 | let cb = jasmine.createSpy('callback'); |
| 95 | |
| 96 | beforeEach(inject(($rootScope) => { |
| 97 | |
| 98 | scope = $rootScope.$new(); |
| 99 | |
| 100 | let inputs: IXosFormInput[] = [ |
| 101 | { |
| 102 | name: 'id', |
| 103 | label: 'Id:', |
| 104 | type: 'number', |
| 105 | validators: {} |
| 106 | }, |
| 107 | { |
| 108 | name: 'first_name', |
| 109 | label: 'Name:', |
| 110 | type: 'text', |
| 111 | validators: {} |
| 112 | }, |
| 113 | { |
| 114 | name: 'email', |
| 115 | label: 'Mail:', |
| 116 | type: 'email', |
Matteo Scandolo | 8005623 | 2017-07-10 16:24:41 -0700 | [diff] [blame] | 117 | validators: { |
| 118 | required: true |
| 119 | } |
Matteo Scandolo | b2225a0 | 2017-04-11 15:23:04 -0700 | [diff] [blame] | 120 | }, |
| 121 | { |
| 122 | name: 'birthDate', |
| 123 | label: 'DOB:', |
| 124 | type: 'date', |
| 125 | validators: {} |
| 126 | }, |
| 127 | { |
| 128 | name: 'enabled', |
| 129 | label: 'Status:', |
| 130 | type: 'boolean', |
| 131 | validators: {} |
| 132 | }, |
| 133 | { |
| 134 | name: 'role', |
| 135 | label: 'Role:', |
| 136 | type: 'select', |
| 137 | options: [ |
| 138 | {id: 1, label: 'user'}, |
| 139 | {id: 2, label: 'admin'} |
| 140 | ], |
| 141 | validators: {} |
| 142 | } |
| 143 | ]; |
| 144 | |
| 145 | let config: IXosFormCfg = { |
| 146 | formName: 'testForm', |
| 147 | actions: [ |
| 148 | { |
| 149 | label: 'Save', |
| 150 | icon: 'ok', |
| 151 | cb: cb, |
| 152 | class: 'success' |
| 153 | } |
| 154 | ], |
| 155 | inputs: inputs |
| 156 | }; |
| 157 | |
| 158 | scope.config = config; |
| 159 | |
| 160 | scope.model = { |
| 161 | id: 1, |
| 162 | first_name: 'Jhon', |
| 163 | email: 'test@onlab.us', |
| 164 | birthDate: new Date('2016-04-18T23:44:16.883181Z'), |
| 165 | enabled: true, |
| 166 | role: 1, // select |
| 167 | }; |
| 168 | |
| 169 | compileElement(); |
| 170 | })); |
| 171 | |
| 172 | it('should render 4 input field', () => { |
| 173 | // boolean and select are in the form model, but are not input |
| 174 | expect(Object.keys(isolatedScope.config.inputs).length).toEqual(6); |
| 175 | const htmlInputs = $('input', element); |
| 176 | expect(htmlInputs.length).toEqual(4); |
| 177 | }); |
| 178 | |
| 179 | it('should render 1 boolean field', () => { |
| 180 | expect($(element).find('.boolean-field > a').length).toEqual(2); |
| 181 | }); |
| 182 | |
| 183 | it('when clicking on action should invoke callback', () => { |
| 184 | const link = $(element).find('[role="button"]'); |
| 185 | link.click(); |
| 186 | // TODO : Check correct parameters |
| 187 | expect(cb).toHaveBeenCalled(); |
| 188 | |
| 189 | }); |
| 190 | |
| 191 | // TODO move in xosField test |
| 192 | it('should set a custom label', () => { |
Matteo Scandolo | 8005623 | 2017-07-10 16:24:41 -0700 | [diff] [blame] | 193 | let nameField = element[0].getElementsByClassName('form-group')[1]; |
Matteo Scandolo | b2225a0 | 2017-04-11 15:23:04 -0700 | [diff] [blame] | 194 | let label = angular.element(nameField.getElementsByTagName('label')[0]).text(); |
Matteo Scandolo | 8005623 | 2017-07-10 16:24:41 -0700 | [diff] [blame] | 195 | expect(label).toContain('Name:'); |
| 196 | expect(label).not.toContain('*'); |
| 197 | }); |
| 198 | |
| 199 | it('should print an * for required fields', () => { |
| 200 | let nameField = element[0].getElementsByClassName('form-group')[2]; |
| 201 | let label = angular.element(nameField.getElementsByTagName('label')[0]).text(); |
| 202 | expect(label).toContain('*'); |
Matteo Scandolo | b2225a0 | 2017-04-11 15:23:04 -0700 | [diff] [blame] | 203 | }); |
| 204 | |
| 205 | // TODO move test in xos-field |
| 206 | xdescribe('the boolean field test', () => { |
| 207 | |
| 208 | let setFalse, setTrue; |
| 209 | |
| 210 | beforeEach(() => { |
| 211 | setFalse = $('.boolean-field > button:first-child', element); |
| 212 | setTrue = $('.boolean-field > button:last-child', element); |
| 213 | }); |
| 214 | |
| 215 | it('should change value to false', () => { |
| 216 | expect(isolatedScope.ngModel.enabled).toEqual(true); |
| 217 | setFalse.click(); |
| 218 | expect(isolatedScope.ngModel.enabled).toEqual(false); |
| 219 | }); |
| 220 | |
| 221 | it('should change value to true', () => { |
| 222 | isolatedScope.ngModel.enabled = false; |
| 223 | scope.$apply(); |
| 224 | expect(isolatedScope.ngModel.enabled).toEqual(false); |
| 225 | setTrue.click(); |
| 226 | expect(isolatedScope.ngModel.enabled).toEqual(true); |
| 227 | }); |
| 228 | }); |
| 229 | }); |
| 230 | describe('when correctly configured for feedback', () => { |
| 231 | // NOTE move this tests in xos-alert?? |
| 232 | let fb = jasmine.createSpy('feedback').and.callFake(function(statusFlag: boolean) { |
| 233 | if (statusFlag) { |
| 234 | scope.config.feedback.show = true; |
| 235 | scope.config.feedback.message = 'Form Submitted'; |
| 236 | scope.config.feedback.type = 'success'; |
| 237 | } |
| 238 | else { |
| 239 | scope.config.feedback.show = true; |
| 240 | scope.config.feedback.message = 'Error'; |
| 241 | scope.config.feedback.type = 'danger'; |
| 242 | } |
| 243 | }); |
| 244 | |
| 245 | beforeEach(() => { |
| 246 | scope = rootScope.$new(); |
| 247 | scope.config = { |
| 248 | formName: 'testForm', |
| 249 | feedback: { |
| 250 | show: false, |
| 251 | message: 'Form submitted successfully !!!', |
| 252 | type: 'success' |
| 253 | }, |
| 254 | actions: [ |
| 255 | { |
| 256 | label: 'Save', |
| 257 | icon: 'ok', // refers to bootstraps glyphicon |
| 258 | cb: () => null, |
| 259 | class: 'success' |
| 260 | } |
| 261 | ] |
| 262 | }; |
| 263 | scope.model = {}; |
| 264 | compileElement(); |
| 265 | }); |
| 266 | |
| 267 | it('should not show feedback when loaded', () => { |
| 268 | expect($(element).find('xos-alert > div')).toHaveClass('alert alert-success ng-hide'); |
| 269 | }); |
| 270 | |
| 271 | it('should show a success feedback', () => { |
| 272 | fb(true); |
| 273 | scope.$digest(); |
| 274 | expect(isolatedScope.config.feedback.type).toEqual('success'); |
| 275 | expect(fb).toHaveBeenCalledWith(true); |
| 276 | expect($(element).find('xos-alert > div')).toHaveClass('alert alert-success'); |
| 277 | }); |
| 278 | |
| 279 | it('should show an error feedback', function() { |
| 280 | fb(false); |
| 281 | scope.$digest(); |
| 282 | expect(isolatedScope.config.feedback.type).toEqual('danger'); |
| 283 | expect(fb).toHaveBeenCalledWith(false); |
| 284 | expect($(element).find('xos-alert > div')).toHaveClass('alert alert-danger'); |
| 285 | }); |
| 286 | }); |
| 287 | }); |