Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 4/18/16. |
| 5 | */ |
| 6 | |
| 7 | (function () { |
| 8 | 'use strict'; |
| 9 | |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 10 | let element, scope, isolatedScope, rootScope, compile; |
| 11 | |
| 12 | const compileElement = () => { |
| 13 | |
| 14 | if(!scope){ |
| 15 | scope = rootScope.$new(); |
| 16 | } |
| 17 | |
| 18 | element = angular.element(`<xos-form config="config" ng-model="model"></xos-form>`); |
| 19 | compile(element)(scope); |
| 20 | scope.$digest(); |
| 21 | isolatedScope = element.isolateScope().vm; |
| 22 | } |
| 23 | |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 24 | describe('The xos.helper module', function(){ |
| 25 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 26 | describe('The xos-form component', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 27 | |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 28 | |
| 29 | beforeEach(module('xos.helpers')); |
| 30 | |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 31 | beforeEach(inject(($compile, $rootScope) => { |
| 32 | rootScope = $rootScope; |
| 33 | compile = $compile; |
| 34 | })); |
| 35 | |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 36 | it('should throw an error if no config is specified', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 37 | function errorFunctionWrapper(){ |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 38 | compileElement(); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 39 | } |
| 40 | expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide a configuration via the "config" attribute')); |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 41 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 42 | |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 43 | it('should throw an error if no actions is specified', () => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 44 | function errorFunctionWrapper(){ |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 45 | scope = rootScope.$new(); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 46 | scope.config = 'green'; |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 47 | compileElement(); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 48 | } |
| 49 | expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide an action list in the configuration')); |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 50 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 51 | |
| 52 | describe('when correctly configured', () => { |
| 53 | |
| 54 | let cb = jasmine.createSpy('callback'); |
| 55 | |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 56 | beforeEach(inject(($rootScope) => { |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 57 | |
| 58 | scope = $rootScope.$new(); |
| 59 | |
| 60 | scope.config = { |
| 61 | exclude: ['excludedField'], |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 62 | formName: 'testForm', |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 63 | actions: [ |
| 64 | { |
| 65 | label: 'Save', |
| 66 | icon: 'ok', // refers to bootstraps glyphicon |
| 67 | cb: cb, |
| 68 | class: 'success' |
| 69 | } |
| 70 | ], |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 71 | fields: { |
| 72 | first_name: { |
| 73 | label: 'Custom Label' |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 74 | } |
Matteo Scandolo | 824a7cb | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 75 | } |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | scope.model = { |
| 79 | id: 1, |
| 80 | first_name: 'Jhon', |
| 81 | last_name: 'Snow', |
| 82 | age: 25, |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 83 | email: 'test@onlab.us', |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 84 | birthDate: '2016-04-18T23:44:16.883181Z', |
| 85 | enabled: true, |
| 86 | role: 'user', //select |
| 87 | a_permissions: [ |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 88 | ], |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 89 | object_field: { |
| 90 | string: 'bar', |
| 91 | number: 1, |
| 92 | email: 'teo@onlab.us' |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 93 | } |
| 94 | }; |
| 95 | |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 96 | compileElement(); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 97 | })); |
| 98 | |
| 99 | it('should add excluded properties to the list', () => { |
| 100 | let expected = ['id', 'validators', 'created', 'updated', 'deleted', 'backend_status', 'excludedField']; |
| 101 | expect(isolatedScope.excludedField).toEqual(expected); |
| 102 | }); |
| 103 | |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 104 | it('should render 10 input field', () => { |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 105 | // boolean are in the form model, but are not input |
| 106 | expect(Object.keys(isolatedScope.formField).length).toEqual(9); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 107 | var field = element[0].getElementsByTagName('input'); |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 108 | expect(field.length).toEqual(10); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 109 | }); |
| 110 | |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 111 | it('should render 1 boolean field', () => { |
| 112 | expect($(element).find('.boolean-field > button').length).toEqual(2) |
| 113 | }); |
| 114 | |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 115 | it('when clicking on action should invoke callback', () => { |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 116 | var link = $(element).find('[role="button"]'); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 117 | link.click(); |
| 118 | expect(cb).toHaveBeenCalledWith(scope.model); |
| 119 | }); |
| 120 | |
| 121 | it('should set a custom label', () => { |
| 122 | let nameField = element[0].getElementsByClassName('form-group')[0]; |
| 123 | let label = angular.element(nameField.getElementsByTagName('label')[0]).text() |
| 124 | expect(label).toEqual('Custom Label:'); |
| 125 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 126 | |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 127 | it('should use the correct input type', () => { |
| 128 | expect($(element).find('[name="age"]')).toHaveAttr('type', 'number'); |
| 129 | expect($(element).find('[name="birthDate"]')).toHaveAttr('type', 'date'); |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 130 | expect($(element).find('[name="email"]')).toHaveAttr('type', 'email'); |
| 131 | }); |
| 132 | |
| 133 | describe('the boolean field', () => { |
| 134 | |
| 135 | let setFalse, setTrue; |
| 136 | |
| 137 | beforeEach(() => { |
| 138 | setFalse= $(element).find('.boolean-field > button:first-child'); |
| 139 | setTrue = $(element).find('.boolean-field > button:last-child'); |
| 140 | }); |
| 141 | |
| 142 | it('should change value to false', () => { |
| 143 | expect(isolatedScope.ngModel.enabled).toEqual(true); |
| 144 | setFalse.click() |
| 145 | expect(isolatedScope.ngModel.enabled).toEqual(false); |
| 146 | }); |
| 147 | |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 148 | it('should change value to true', () => { |
Matteo Scandolo | fb667b0 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 149 | isolatedScope.ngModel.enabled = false; |
| 150 | scope.$apply(); |
| 151 | expect(isolatedScope.ngModel.enabled).toEqual(false); |
| 152 | setTrue.click() |
| 153 | expect(isolatedScope.ngModel.enabled).toEqual(true); |
| 154 | }); |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 155 | }); |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 156 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 157 | // NOTE not sure why this tests are failing |
| 158 | xdescribe('the custom validation options', () => { |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 159 | beforeEach(() => { |
| 160 | scope.config.fields.first_name.validators = { |
| 161 | minlength: 10, |
| 162 | maxlength: 15, |
| 163 | required: true |
| 164 | }; |
| 165 | |
Matteo Scandolo | 953ddad | 2016-04-25 08:15:24 -0700 | [diff] [blame] | 166 | scope.config.fields.age = { |
| 167 | validators: { |
| 168 | min: 10, |
| 169 | max: 20 |
| 170 | } |
| 171 | }; |
| 172 | |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 173 | scope.$digest(); |
| 174 | }); |
| 175 | |
| 176 | it('should validate required', () => { |
| 177 | scope.model.first_name = null; |
| 178 | scope.$digest(); |
| 179 | |
| 180 | expect(isolatedScope.testForm.first_name.$valid).toBeFalsy(); |
| 181 | expect(isolatedScope.testForm.first_name.$error.required).toBeTruthy(); |
| 182 | }); |
| 183 | |
| 184 | it('should validate minlength', () => { |
| 185 | scope.model.first_name = 'short'; |
| 186 | scope.$digest(); |
| 187 | |
| 188 | expect(isolatedScope.testForm.first_name.$valid).toBeFalsy(); |
| 189 | expect(isolatedScope.testForm.first_name.$error.minlength).toBeTruthy(); |
| 190 | }); |
| 191 | |
| 192 | it('should validate maxlength', () => { |
| 193 | scope.model.first_name = 'this is way too long!'; |
| 194 | scope.$digest(); |
| 195 | |
| 196 | expect(isolatedScope.testForm.first_name.$valid).toBeFalsy(); |
| 197 | expect(isolatedScope.testForm.first_name.$error.maxlength).toBeTruthy(); |
| 198 | }); |
Matteo Scandolo | 953ddad | 2016-04-25 08:15:24 -0700 | [diff] [blame] | 199 | |
Matteo Scandolo | 2e804cb | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 200 | it('should validate min', () => { |
Matteo Scandolo | 953ddad | 2016-04-25 08:15:24 -0700 | [diff] [blame] | 201 | // not validating min and max for now |
| 202 | scope.model.age = 8; |
| 203 | scope.$digest(); |
| 204 | |
| 205 | expect(isolatedScope.testForm.age.$valid).toBeFalsy(); |
| 206 | expect(isolatedScope.testForm.age.$error.min).toBeTruthy(); |
| 207 | }); |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 208 | }); |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 209 | |
| 210 | describe('when a deep model is passed', () => { |
| 211 | |
| 212 | beforeEach(inject(($rootScope) => { |
| 213 | |
| 214 | scope = $rootScope.$new(); |
| 215 | |
| 216 | scope.config = { |
| 217 | exclude: ['excludedField'], |
| 218 | formName: 'testForm', |
| 219 | actions: [ |
| 220 | { |
| 221 | label: 'Save', |
| 222 | icon: 'ok', // refers to bootstraps glyphicon |
| 223 | cb: cb, |
| 224 | class: 'success' |
| 225 | } |
| 226 | ], |
| 227 | fields: { |
| 228 | object_field: { |
| 229 | field_one: { |
| 230 | label: 'Custom Label' |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | scope.model = { |
| 237 | object_field: { |
| 238 | field_one: 'bar', |
| 239 | number: 1, |
| 240 | email: 'teo@onlab.us' |
| 241 | } |
| 242 | }; |
| 243 | |
| 244 | compileElement(); |
| 245 | })); |
| 246 | |
| 247 | it('should print nested field', () => { |
| 248 | expect($(element).find('input').length).toBe(3); |
| 249 | }); |
| 250 | |
| 251 | xit('should configure nested fields', () => { |
| 252 | let custom_label = $(element).find('input[name=field_one]').parent().find('label'); |
| 253 | expect(custom_label.text()).toBe('Custom Label'); |
| 254 | }); |
| 255 | }); |
Matteo Scandolo | b389f7a | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 256 | }); |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 257 | }); |
| 258 | }); |
| 259 | })(); |