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 | |
| 62 | it('should throw an error if no field model is passed', inject(($compile, $rootScope) => { |
| 63 | function errorFunctionWrapper(){ |
| 64 | // setup the parent scope |
| 65 | scope = $rootScope.$new(); |
| 66 | scope.name = 'label'; |
| 67 | scope.field = { |
| 68 | label: 'Label', |
| 69 | type: 'number', |
| 70 | validators: {} |
| 71 | }; |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 72 | compileElement(angular.element('<xos-field name="name" field="field"></xos-field>')); |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 73 | } |
| 74 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide an ng-model')); |
| 75 | })); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 76 | |
| 77 | describe('when a text input is passed', () => { |
| 78 | beforeEach(() => { |
| 79 | scope = rootScope.$new(); |
| 80 | scope.name = 'label'; |
| 81 | scope.field = { |
| 82 | label: 'Label', |
| 83 | type: 'text', |
| 84 | validators: {} |
| 85 | }; |
| 86 | scope.ngModel = 'label'; |
| 87 | compileElement(); |
| 88 | }); |
| 89 | |
| 90 | it('should print a text field', () => { |
| 91 | expect($(element).find('[name="label"]')).toHaveAttr('type', 'text'); |
| 92 | }); |
| 93 | }); |
| 94 | |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 95 | |
| 96 | |
| 97 | |
| 98 | describe('when a option is selected in dropdown', () => { |
| 99 | beforeEach(() => { |
| 100 | scope = rootScope.$new(); |
| 101 | scope.name = 'label'; |
| 102 | scope.field = { |
| 103 | label: 'Label', |
| 104 | type: 'select', |
| 105 | validators: {}, |
Matteo Scandolo | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 106 | options: [ |
| 107 | { |
| 108 | id: 0, |
| 109 | label: '---Site---' |
| 110 | }, |
| 111 | { |
| 112 | id: 1, |
| 113 | label: '---Site1---' |
| 114 | } |
| 115 | ] |
arpiagariu | 7c1975d | 2016-06-03 16:41:39 -0700 | [diff] [blame] | 116 | }; |
| 117 | scope.ngModel = 'label'; |
| 118 | compileElement(); |
| 119 | }); |
| 120 | |
| 121 | it('No of select elements', () => { |
| 122 | expect($(element).find('select').children('option').length).toEqual(3); |
| 123 | }); |
| 124 | |
| 125 | it('should show a selected value', () => { |
| 126 | var elem = angular.element($(element).find('select').children('option')[1]); |
| 127 | expect(elem.text()).toEqual('---Site---'); |
| 128 | }); |
| 129 | }); |
| 130 | |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 131 | describe('when a number input is passed', () => { |
| 132 | beforeEach(() => { |
| 133 | scope = rootScope.$new(); |
| 134 | scope.name = 'label'; |
| 135 | scope.field = { |
| 136 | label: 'Label', |
| 137 | type: 'number', |
| 138 | validators: {} |
| 139 | }; |
| 140 | scope.ngModel = 10; |
| 141 | compileElement(); |
| 142 | }); |
| 143 | |
| 144 | it('should print a number field', () => { |
| 145 | expect($(element).find('[name="label"]')).toHaveAttr('type', 'number'); |
| 146 | }); |
| 147 | }); |
| 148 | |
| 149 | describe('when a boolean input is passed', () => { |
| 150 | beforeEach(() => { |
| 151 | scope = rootScope.$new(); |
| 152 | scope.name = 'label'; |
| 153 | scope.field = { |
| 154 | label: 'Label', |
| 155 | type: 'boolean', |
| 156 | validators: {} |
| 157 | }; |
| 158 | scope.ngModel = true; |
| 159 | compileElement(); |
| 160 | }); |
| 161 | |
| 162 | let setFalse, setTrue; |
| 163 | |
| 164 | beforeEach(() => { |
| 165 | setFalse= $(element).find('.boolean-field > button:first-child'); |
| 166 | setTrue = $(element).find('.boolean-field > button:last-child'); |
| 167 | }); |
| 168 | |
| 169 | it('should print two buttons', () => { |
| 170 | expect($(element).find('.boolean-field > button').length).toEqual(2) |
| 171 | }); |
| 172 | |
| 173 | it('should change value to false', () => { |
| 174 | expect(isolatedScope.ngModel).toEqual(true); |
| 175 | setFalse.click() |
| 176 | expect(isolatedScope.ngModel).toEqual(false); |
| 177 | }); |
| 178 | |
| 179 | it('should change value to true', () => { |
| 180 | isolatedScope.ngModel = false; |
| 181 | scope.$apply(); |
| 182 | expect(isolatedScope.ngModel).toEqual(false); |
| 183 | setTrue.click() |
| 184 | expect(isolatedScope.ngModel).toEqual(true); |
| 185 | }); |
| 186 | }); |
| 187 | |
| 188 | describe('when an object input is passed', () => { |
| 189 | beforeEach(() => { |
| 190 | scope = rootScope.$new(); |
| 191 | scope.name = 'label'; |
| 192 | scope.field = { |
| 193 | label: 'Label', |
| 194 | type: 'object', |
| 195 | validators: {} |
| 196 | }; |
| 197 | scope.ngModel = { |
| 198 | baz: true, |
| 199 | foo: 'bar', |
| 200 | foz: 1, |
| 201 | }; |
| 202 | compileElement(); |
| 203 | }); |
| 204 | |
| 205 | it('should print a panel to contain object property field', () => { |
| 206 | expect($(element).find('.panel.object-field')).toExist() |
| 207 | }); |
| 208 | |
| 209 | it('should print the right input type for each property', () => { |
| 210 | expect($(element).find('input').length).toBe(2); |
| 211 | expect($(element).find('.boolean-field > button').length).toEqual(2); |
| 212 | }); |
| 213 | |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 214 | it('should format labels', () => { |
| 215 | expect($(element).find('input[name="foo"]').parent().find('label').text()).toBe('Foo:'); |
| 216 | }); |
| 217 | |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 218 | describe('and the model is empty', () => { |
| 219 | beforeEach(() => { |
| 220 | scope.ngModel = { |
| 221 | }; |
| 222 | compileElement(); |
| 223 | }); |
| 224 | |
| 225 | it('should not print the panel', () => { |
Matteo Scandolo | 2aca9ab | 2016-05-26 15:09:55 -0700 | [diff] [blame] | 226 | // console.log($(element).find('.panel.object-field')); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 227 | expect($(element).find('.panel.object-field')).not.toExist() |
| 228 | }); |
| 229 | }); |
| 230 | }); |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 231 | }); |
| 232 | }); |
| 233 | })(); |