Matteo Scandolo | 7bc39c4 | 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 | b9fb625 | 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 | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 24 | describe('The xos.helper module', function(){ |
| 25 | |
Matteo Scandolo | 6ba1287 | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 26 | describe('The xos-form component', () => { |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 27 | |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 28 | |
| 29 | beforeEach(module('xos.helpers')); |
| 30 | |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 31 | beforeEach(inject(($compile, $rootScope) => { |
| 32 | rootScope = $rootScope; |
| 33 | compile = $compile; |
| 34 | })); |
| 35 | |
Matteo Scandolo | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 36 | it('should throw an error if no config is specified', () => { |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 37 | function errorFunctionWrapper(){ |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 38 | compileElement(); |
Matteo Scandolo | 7bc39c4 | 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 | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 41 | }); |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 42 | |
Matteo Scandolo | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 43 | it('should throw an error if no actions is specified', () => { |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 44 | function errorFunctionWrapper(){ |
Matteo Scandolo | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 45 | scope = rootScope.$new(); |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 46 | scope.config = 'green'; |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 47 | compileElement(); |
Matteo Scandolo | 7bc39c4 | 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 | b6ca301 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 50 | }); |
Matteo Scandolo | 7bc39c4 | 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 | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 56 | beforeEach(inject(($rootScope) => { |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 57 | |
| 58 | scope = $rootScope.$new(); |
| 59 | |
| 60 | scope.config = { |
| 61 | exclude: ['excludedField'], |
Matteo Scandolo | b3d686f | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 62 | formName: 'testForm', |
Matteo Scandolo | 7bc39c4 | 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 | 9f0e5ae | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 71 | fields: { |
| 72 | first_name: { |
| 73 | label: 'Custom Label' |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 74 | } |
Matteo Scandolo | 9f0e5ae | 2016-04-20 12:24:52 -0700 | [diff] [blame] | 75 | } |
Matteo Scandolo | 7bc39c4 | 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 | 4ba4cf1 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 83 | email: 'test@onlab.us', |
Matteo Scandolo | 7bc39c4 | 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 | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 88 | ], |
Matteo Scandolo | 974c0e4 | 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 | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 93 | } |
| 94 | }; |
| 95 | |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 96 | compileElement(); |
Matteo Scandolo | 7bc39c4 | 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 | 03d8b8e | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 104 | it('should render 10 input field', () => { |
Matteo Scandolo | 4ba4cf1 | 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 | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 107 | var field = element[0].getElementsByTagName('input'); |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 108 | expect(field.length).toEqual(10); |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 109 | }); |
| 110 | |
Matteo Scandolo | 4ba4cf1 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 111 | it('should render 1 boolean field', () => { |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 112 | expect($(element).find('.boolean-field > a').length).toEqual(2) |
Matteo Scandolo | 4ba4cf1 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 113 | }); |
| 114 | |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 115 | it('when clicking on action should invoke callback', () => { |
Matteo Scandolo | 4ba4cf1 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 116 | var link = $(element).find('[role="button"]'); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 117 | //console.log(link); |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 118 | link.click(); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 119 | // TODO : Check correct parameters |
| 120 | expect(cb).toHaveBeenCalled(); |
| 121 | |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 122 | }); |
| 123 | |
| 124 | it('should set a custom label', () => { |
| 125 | let nameField = element[0].getElementsByClassName('form-group')[0]; |
| 126 | let label = angular.element(nameField.getElementsByTagName('label')[0]).text() |
| 127 | expect(label).toEqual('Custom Label:'); |
| 128 | }); |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 129 | |
Matteo Scandolo | 6e2e6ff | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 130 | it('should use the correct input type', () => { |
| 131 | expect($(element).find('[name="age"]')).toHaveAttr('type', 'number'); |
| 132 | expect($(element).find('[name="birthDate"]')).toHaveAttr('type', 'date'); |
Matteo Scandolo | 4ba4cf1 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 133 | expect($(element).find('[name="email"]')).toHaveAttr('type', 'email'); |
| 134 | }); |
| 135 | |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 136 | xdescribe('the boolean field test', () => { |
Matteo Scandolo | 4ba4cf1 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 137 | |
| 138 | let setFalse, setTrue; |
| 139 | |
| 140 | beforeEach(() => { |
| 141 | setFalse= $(element).find('.boolean-field > button:first-child'); |
| 142 | setTrue = $(element).find('.boolean-field > button:last-child'); |
| 143 | }); |
| 144 | |
| 145 | it('should change value to false', () => { |
| 146 | expect(isolatedScope.ngModel.enabled).toEqual(true); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 147 | setFalse.click(); |
Matteo Scandolo | 4ba4cf1 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 148 | expect(isolatedScope.ngModel.enabled).toEqual(false); |
| 149 | }); |
| 150 | |
Matteo Scandolo | 974c0e4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 151 | it('should change value to true', () => { |
Matteo Scandolo | 4ba4cf1 | 2016-04-20 16:36:17 -0700 | [diff] [blame] | 152 | isolatedScope.ngModel.enabled = false; |
| 153 | scope.$apply(); |
| 154 | expect(isolatedScope.ngModel.enabled).toEqual(false); |
| 155 | setTrue.click() |
| 156 | expect(isolatedScope.ngModel.enabled).toEqual(true); |
| 157 | }); |
Matteo Scandolo | 6e2e6ff | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 158 | }); |
Matteo Scandolo | b3d686f | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 159 | |
Matteo Scandolo | b9fb625 | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 160 | describe('when a deep model is passed', () => { |
| 161 | |
| 162 | beforeEach(inject(($rootScope) => { |
| 163 | |
| 164 | scope = $rootScope.$new(); |
| 165 | |
| 166 | scope.config = { |
| 167 | exclude: ['excludedField'], |
| 168 | formName: 'testForm', |
| 169 | actions: [ |
| 170 | { |
| 171 | label: 'Save', |
| 172 | icon: 'ok', // refers to bootstraps glyphicon |
| 173 | cb: cb, |
| 174 | class: 'success' |
| 175 | } |
| 176 | ], |
| 177 | fields: { |
| 178 | object_field: { |
| 179 | field_one: { |
| 180 | label: 'Custom Label' |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | |
| 186 | scope.model = { |
| 187 | object_field: { |
| 188 | field_one: 'bar', |
| 189 | number: 1, |
| 190 | email: 'teo@onlab.us' |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | compileElement(); |
| 195 | })); |
| 196 | |
| 197 | it('should print nested field', () => { |
| 198 | expect($(element).find('input').length).toBe(3); |
| 199 | }); |
| 200 | |
| 201 | xit('should configure nested fields', () => { |
| 202 | let custom_label = $(element).find('input[name=field_one]').parent().find('label'); |
| 203 | expect(custom_label.text()).toBe('Custom Label'); |
| 204 | }); |
| 205 | }); |
Matteo Scandolo | 6e2e6ff | 2016-04-20 14:59:39 -0700 | [diff] [blame] | 206 | }); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 207 | describe('when correctly configured for feedback', () => { |
| 208 | |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 209 | let fb = jasmine.createSpy('feedback').and.callFake(function(statusFlag) { |
| 210 | if(statusFlag){ |
| 211 | scope.config.feedback.show = true; |
| 212 | scope.config.feedback.message = 'Form Submitted'; |
| 213 | scope.config.feedback.type = 'success'; |
| 214 | } |
| 215 | else { |
| 216 | scope.config.feedback.show = true; |
| 217 | scope.config.feedback.message = 'Error'; |
| 218 | scope.config.feedback.type = 'danger'; |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 219 | |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 220 | } |
| 221 | }); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 222 | |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 223 | beforeEach(()=> { |
| 224 | scope = rootScope.$new(); |
| 225 | scope.config = |
| 226 | { |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 227 | |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 228 | feedback: { |
| 229 | show: false, |
| 230 | message: 'Form submitted successfully !!!', |
| 231 | type: 'success' |
| 232 | }, |
| 233 | actions: [ |
| 234 | { |
| 235 | label: 'Save', |
| 236 | icon: 'ok', // refers to bootstraps glyphicon |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 237 | cb: () => {}, |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 238 | class: 'success' |
| 239 | } |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 240 | ] |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 241 | }; |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 242 | scope.model={}; |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 243 | compileElement(); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 244 | }); |
| 245 | |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 246 | it('should not show feedback when loaded', () => { |
| 247 | expect($(element).find('xos-alert > div')).toHaveClass('alert alert-success ng-hide'); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 248 | }); |
| 249 | |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 250 | it('should show a success feedback', () => { |
| 251 | fb(true); |
| 252 | scope.$digest(); |
| 253 | expect(isolatedScope.config.feedback.type).toEqual('success'); |
| 254 | expect(fb).toHaveBeenCalledWith(true); |
| 255 | expect($(element).find('xos-alert > div')).toHaveClass('alert alert-success'); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 256 | }); |
| 257 | |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 258 | it('should show an error feedback', function() { |
| 259 | fb(false); |
| 260 | scope.$digest(); |
| 261 | expect(isolatedScope.config.feedback.type).toEqual('danger'); |
| 262 | expect(fb).toHaveBeenCalledWith(false); |
| 263 | expect($(element).find('xos-alert > div')).toHaveClass('alert alert-danger'); |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 264 | }); |
| 265 | }); |
| 266 | |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 267 | }); |
| 268 | }); |
| 269 | })(); |