Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 5/25/16. |
| 5 | */ |
| 6 | |
| 7 | (function () { |
| 8 | 'use strict'; |
| 9 | |
| 10 | let element, scope, isolatedScope, rootScope, compile; |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 11 | const compileElement = (el) => { |
| 12 | element = el; |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 13 | |
| 14 | if(!scope){ |
| 15 | scope = rootScope.$new(); |
| 16 | } |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 17 | if(!angular.isDefined(element)){ |
| 18 | element = angular.element('<xos-field name="name" field="field" ng-model="ngModel"></xos-field>'); |
| 19 | } |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 20 | compile(element)(scope); |
| 21 | scope.$digest(); |
| 22 | isolatedScope = element.isolateScope().vm; |
| 23 | } |
| 24 | |
| 25 | describe('The xos.helper module', function(){ |
| 26 | |
| 27 | describe('The xosField component', () => { |
| 28 | |
| 29 | beforeEach(module('xos.helpers')); |
| 30 | |
| 31 | beforeEach(inject(function ($compile, $rootScope) { |
| 32 | compile = $compile; |
| 33 | rootScope = $rootScope; |
| 34 | })); |
| 35 | |
| 36 | it('should throw an error if no name is passed', inject(($compile, $rootScope) => { |
| 37 | function errorFunctionWrapper(){ |
| 38 | // setup the parent scope |
| 39 | scope = $rootScope.$new(); |
| 40 | scope.field = { |
| 41 | label: 'Label', |
| 42 | type: 'number', |
| 43 | validators: {} |
| 44 | }; |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 45 | scope.ngModel = 1; |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 46 | compileElement(); |
| 47 | } |
| 48 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field name')); |
| 49 | })); |
| 50 | |
| 51 | it('should throw an error if no field definition is passed', inject(($compile, $rootScope) => { |
| 52 | function errorFunctionWrapper(){ |
| 53 | // setup the parent scope |
| 54 | scope = $rootScope.$new(); |
| 55 | scope.name = 'label'; |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 56 | scope.ngModel = 1; |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 57 | compileElement(); |
| 58 | } |
| 59 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field definition')); |
| 60 | })); |
| 61 | |
Matteo Scandolo | e59cd4c | 2016-06-08 14:27:30 -0700 | [diff] [blame^] | 62 | it('should throw an error if no field type is passed', inject(($compile, $rootScope) => { |
| 63 | function errorFunctionWrapper(){ |
| 64 | // setup the parent scope |
| 65 | scope = $rootScope.$new(); |
| 66 | scope.name = 'label'; |
| 67 | scope.ngModel = 1; |
| 68 | scope.field = {label: 'Label:'} |
| 69 | compileElement(); |
| 70 | } |
| 71 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a type in the field definition')); |
| 72 | })); |
| 73 | |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 74 | it('should throw an error if no field model is passed', inject(($compile, $rootScope) => { |
| 75 | function errorFunctionWrapper(){ |
| 76 | // setup the parent scope |
| 77 | scope = $rootScope.$new(); |
| 78 | scope.name = 'label'; |
| 79 | scope.field = { |
| 80 | label: 'Label', |
| 81 | type: 'number', |
| 82 | validators: {} |
| 83 | }; |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 84 | compileElement(angular.element('<xos-field name="name" field="field"></xos-field>')); |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 85 | } |
| 86 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide an ng-model')); |
| 87 | })); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 88 | |
| 89 | describe('when a text input is passed', () => { |
| 90 | beforeEach(() => { |
| 91 | scope = rootScope.$new(); |
| 92 | scope.name = 'label'; |
| 93 | scope.field = { |
| 94 | label: 'Label', |
| 95 | type: 'text', |
| 96 | validators: {} |
| 97 | }; |
| 98 | scope.ngModel = 'label'; |
| 99 | compileElement(); |
| 100 | }); |
| 101 | |
| 102 | it('should print a text field', () => { |
| 103 | expect($(element).find('[name="label"]')).toHaveAttr('type', 'text'); |
| 104 | }); |
| 105 | }); |
| 106 | |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 107 | |
| 108 | |
| 109 | |
| 110 | describe('when a option is selected in dropdown', () => { |
| 111 | beforeEach(() => { |
| 112 | scope = rootScope.$new(); |
| 113 | scope.name = 'label'; |
| 114 | scope.field = { |
| 115 | label: 'Label', |
| 116 | type: 'select', |
| 117 | validators: {}, |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 118 | options: [ |
| 119 | { |
| 120 | id: 0, |
| 121 | label: '---Site---' |
| 122 | }, |
| 123 | { |
| 124 | id: 1, |
| 125 | label: '---Site1---' |
| 126 | } |
| 127 | ] |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 128 | }; |
| 129 | scope.ngModel = 'label'; |
| 130 | compileElement(); |
| 131 | }); |
| 132 | |
| 133 | it('No of select elements', () => { |
| 134 | expect($(element).find('select').children('option').length).toEqual(3); |
| 135 | }); |
| 136 | |
| 137 | it('should show a selected value', () => { |
| 138 | var elem = angular.element($(element).find('select').children('option')[1]); |
| 139 | expect(elem.text()).toEqual('---Site---'); |
| 140 | }); |
| 141 | }); |
| 142 | |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 143 | describe('when a number input is passed', () => { |
| 144 | beforeEach(() => { |
| 145 | scope = rootScope.$new(); |
| 146 | scope.name = 'label'; |
| 147 | scope.field = { |
| 148 | label: 'Label', |
| 149 | type: 'number', |
| 150 | validators: {} |
| 151 | }; |
| 152 | scope.ngModel = 10; |
| 153 | compileElement(); |
| 154 | }); |
| 155 | |
| 156 | it('should print a number field', () => { |
| 157 | expect($(element).find('[name="label"]')).toHaveAttr('type', 'number'); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | describe('when a boolean input is passed', () => { |
| 162 | beforeEach(() => { |
| 163 | scope = rootScope.$new(); |
| 164 | scope.name = 'label'; |
| 165 | scope.field = { |
| 166 | label: 'Label', |
| 167 | type: 'boolean', |
| 168 | validators: {} |
| 169 | }; |
| 170 | scope.ngModel = true; |
| 171 | compileElement(); |
| 172 | }); |
| 173 | |
| 174 | let setFalse, setTrue; |
| 175 | |
| 176 | beforeEach(() => { |
| 177 | setFalse= $(element).find('.boolean-field > button:first-child'); |
| 178 | setTrue = $(element).find('.boolean-field > button:last-child'); |
| 179 | }); |
| 180 | |
| 181 | it('should print two buttons', () => { |
| 182 | expect($(element).find('.boolean-field > button').length).toEqual(2) |
| 183 | }); |
| 184 | |
| 185 | it('should change value to false', () => { |
| 186 | expect(isolatedScope.ngModel).toEqual(true); |
| 187 | setFalse.click() |
| 188 | expect(isolatedScope.ngModel).toEqual(false); |
| 189 | }); |
| 190 | |
| 191 | it('should change value to true', () => { |
| 192 | isolatedScope.ngModel = false; |
| 193 | scope.$apply(); |
| 194 | expect(isolatedScope.ngModel).toEqual(false); |
| 195 | setTrue.click() |
| 196 | expect(isolatedScope.ngModel).toEqual(true); |
| 197 | }); |
| 198 | }); |
| 199 | |
| 200 | describe('when an object input is passed', () => { |
| 201 | beforeEach(() => { |
| 202 | scope = rootScope.$new(); |
| 203 | scope.name = 'label'; |
| 204 | scope.field = { |
| 205 | label: 'Label', |
| 206 | type: 'object', |
| 207 | validators: {} |
| 208 | }; |
| 209 | scope.ngModel = { |
| 210 | baz: true, |
| 211 | foo: 'bar', |
| 212 | foz: 1, |
| 213 | }; |
| 214 | compileElement(); |
| 215 | }); |
| 216 | |
| 217 | it('should print a panel to contain object property field', () => { |
| 218 | expect($(element).find('.panel.object-field')).toExist() |
| 219 | }); |
| 220 | |
| 221 | it('should print the right input type for each property', () => { |
| 222 | expect($(element).find('input').length).toBe(2); |
| 223 | expect($(element).find('.boolean-field > button').length).toEqual(2); |
| 224 | }); |
| 225 | |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 226 | it('should format labels', () => { |
| 227 | expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:'); |
| 228 | }); |
| 229 | |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 230 | describe('and the model is empty', () => { |
| 231 | beforeEach(() => { |
| 232 | scope.ngModel = { |
| 233 | }; |
| 234 | compileElement(); |
| 235 | }); |
| 236 | |
| 237 | it('should not print the panel', () => { |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 238 | expect($(element).find('.panel.object-field')).not.toExist() |
| 239 | }); |
Matteo Scandolo | e59cd4c | 2016-06-08 14:27:30 -0700 | [diff] [blame^] | 240 | |
| 241 | describe('but field is configured', () => { |
| 242 | beforeEach(() => { |
| 243 | scope.field.properties = { |
| 244 | foo: { |
| 245 | label: 'FooLabel:', |
| 246 | type: 'string', |
| 247 | validators: { |
| 248 | required: true |
| 249 | } |
| 250 | }, |
| 251 | bar: { |
| 252 | type: 'number' |
| 253 | } |
| 254 | }; |
| 255 | compileElement(); |
| 256 | }); |
| 257 | it('should render panel and configured fields', () => { |
| 258 | expect($(element).find('.panel.object-field')).toExist(); |
| 259 | expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('FooLabel:'); |
| 260 | expect($(element).find('input[name="foo"]')).toHaveAttr('type', 'string'); |
| 261 | expect($(element).find('input[name="foo"]')).toHaveAttr('required'); |
| 262 | |
| 263 | expect($(element).find('input[name="bar"]').parent().find('label').text()).toBe('Bar:'); |
| 264 | expect($(element).find('input[name="bar"]')).toHaveAttr('type', 'number'); |
| 265 | |
| 266 | }); |
| 267 | }); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 268 | }); |
| 269 | }); |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 270 | }); |
| 271 | }); |
| 272 | })(); |