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