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; |
| 11 | const compileElement = () => { |
| 12 | |
| 13 | if(!scope){ |
| 14 | scope = rootScope.$new(); |
| 15 | } |
| 16 | |
| 17 | element = angular.element('<xos-field name="name" field="field" ng-model="ngModel"></xos-field>'); |
| 18 | compile(element)(scope); |
| 19 | scope.$digest(); |
| 20 | isolatedScope = element.isolateScope().vm; |
| 21 | } |
| 22 | |
| 23 | describe('The xos.helper module', function(){ |
| 24 | |
| 25 | describe('The xosField component', () => { |
| 26 | |
| 27 | beforeEach(module('xos.helpers')); |
| 28 | |
| 29 | beforeEach(inject(function ($compile, $rootScope) { |
| 30 | compile = $compile; |
| 31 | rootScope = $rootScope; |
| 32 | })); |
| 33 | |
| 34 | it('should throw an error if no name is passed', inject(($compile, $rootScope) => { |
| 35 | function errorFunctionWrapper(){ |
| 36 | // setup the parent scope |
| 37 | scope = $rootScope.$new(); |
| 38 | scope.field = { |
| 39 | label: 'Label', |
| 40 | type: 'number', |
| 41 | validators: {} |
| 42 | }; |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 43 | scope.ngModel = 1; |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 44 | compileElement(); |
| 45 | } |
| 46 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field name')); |
| 47 | })); |
| 48 | |
| 49 | it('should throw an error if no field definition is passed', inject(($compile, $rootScope) => { |
| 50 | function errorFunctionWrapper(){ |
| 51 | // setup the parent scope |
| 52 | scope = $rootScope.$new(); |
| 53 | scope.name = 'label'; |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 54 | scope.ngModel = 1; |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 55 | compileElement(); |
| 56 | } |
| 57 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide a field definition')); |
| 58 | })); |
| 59 | |
| 60 | it('should throw an error if no field model is passed', inject(($compile, $rootScope) => { |
| 61 | function errorFunctionWrapper(){ |
| 62 | // setup the parent scope |
| 63 | scope = $rootScope.$new(); |
| 64 | scope.name = 'label'; |
| 65 | scope.field = { |
| 66 | label: 'Label', |
| 67 | type: 'number', |
| 68 | validators: {} |
| 69 | }; |
| 70 | compileElement(); |
| 71 | } |
| 72 | expect(errorFunctionWrapper).toThrow(new Error('[xosField] Please provide an ng-model')); |
| 73 | })); |
Matteo Scandolo | df23cdb | 2016-05-25 17:37:37 -0700 | [diff] [blame] | 74 | |
| 75 | describe('when a text input is passed', () => { |
| 76 | beforeEach(() => { |
| 77 | scope = rootScope.$new(); |
| 78 | scope.name = 'label'; |
| 79 | scope.field = { |
| 80 | label: 'Label', |
| 81 | type: 'text', |
| 82 | validators: {} |
| 83 | }; |
| 84 | scope.ngModel = 'label'; |
| 85 | compileElement(); |
| 86 | }); |
| 87 | |
| 88 | it('should print a text field', () => { |
| 89 | expect($(element).find('[name="label"]')).toHaveAttr('type', 'text'); |
| 90 | }); |
| 91 | }); |
| 92 | |
| 93 | describe('when a number input is passed', () => { |
| 94 | beforeEach(() => { |
| 95 | scope = rootScope.$new(); |
| 96 | scope.name = 'label'; |
| 97 | scope.field = { |
| 98 | label: 'Label', |
| 99 | type: 'number', |
| 100 | validators: {} |
| 101 | }; |
| 102 | scope.ngModel = 10; |
| 103 | compileElement(); |
| 104 | }); |
| 105 | |
| 106 | it('should print a number field', () => { |
| 107 | expect($(element).find('[name="label"]')).toHaveAttr('type', 'number'); |
| 108 | }); |
| 109 | }); |
| 110 | |
| 111 | describe('when a boolean input is passed', () => { |
| 112 | beforeEach(() => { |
| 113 | scope = rootScope.$new(); |
| 114 | scope.name = 'label'; |
| 115 | scope.field = { |
| 116 | label: 'Label', |
| 117 | type: 'boolean', |
| 118 | validators: {} |
| 119 | }; |
| 120 | scope.ngModel = true; |
| 121 | compileElement(); |
| 122 | }); |
| 123 | |
| 124 | let setFalse, setTrue; |
| 125 | |
| 126 | beforeEach(() => { |
| 127 | setFalse= $(element).find('.boolean-field > button:first-child'); |
| 128 | setTrue = $(element).find('.boolean-field > button:last-child'); |
| 129 | }); |
| 130 | |
| 131 | it('should print two buttons', () => { |
| 132 | expect($(element).find('.boolean-field > button').length).toEqual(2) |
| 133 | }); |
| 134 | |
| 135 | it('should change value to false', () => { |
| 136 | expect(isolatedScope.ngModel).toEqual(true); |
| 137 | setFalse.click() |
| 138 | expect(isolatedScope.ngModel).toEqual(false); |
| 139 | }); |
| 140 | |
| 141 | it('should change value to true', () => { |
| 142 | isolatedScope.ngModel = false; |
| 143 | scope.$apply(); |
| 144 | expect(isolatedScope.ngModel).toEqual(false); |
| 145 | setTrue.click() |
| 146 | expect(isolatedScope.ngModel).toEqual(true); |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | describe('when an object input is passed', () => { |
| 151 | beforeEach(() => { |
| 152 | scope = rootScope.$new(); |
| 153 | scope.name = 'label'; |
| 154 | scope.field = { |
| 155 | label: 'Label', |
| 156 | type: 'object', |
| 157 | validators: {} |
| 158 | }; |
| 159 | scope.ngModel = { |
| 160 | baz: true, |
| 161 | foo: 'bar', |
| 162 | foz: 1, |
| 163 | }; |
| 164 | compileElement(); |
| 165 | }); |
| 166 | |
| 167 | it('should print a panel to contain object property field', () => { |
| 168 | expect($(element).find('.panel.object-field')).toExist() |
| 169 | }); |
| 170 | |
| 171 | it('should print the right input type for each property', () => { |
| 172 | expect($(element).find('input').length).toBe(2); |
| 173 | expect($(element).find('.boolean-field > button').length).toEqual(2); |
| 174 | }); |
| 175 | |
| 176 | describe('and the model is empty', () => { |
| 177 | beforeEach(() => { |
| 178 | scope.ngModel = { |
| 179 | }; |
| 180 | compileElement(); |
| 181 | }); |
| 182 | |
| 183 | it('should not print the panel', () => { |
| 184 | console.log($(element).find('.panel.object-field')); |
| 185 | expect($(element).find('.panel.object-field')).not.toExist() |
| 186 | }); |
| 187 | }); |
| 188 | }); |
Matteo Scandolo | 741d6b4 | 2016-05-25 16:02:16 -0700 | [diff] [blame] | 189 | }); |
| 190 | }); |
| 191 | })(); |