blob: 5f73f306fd5253264d42d4ab7c9c0b047559b0a7 [file] [log] [blame]
Matteo Scandoloc49f53c2016-04-20 11:38:42 -07001/**
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 Scandolo2e804cb2016-04-27 16:29:33 -070012 describe('The xos-form component', () => {
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070013
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 Scandolo28e49b72016-04-22 14:14:03 -070047 formName: 'testForm',
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070048 actions: [
49 {
50 label: 'Save',
51 icon: 'ok', // refers to bootstraps glyphicon
52 cb: cb,
53 class: 'success'
54 }
55 ],
Matteo Scandolo824a7cb2016-04-20 12:24:52 -070056 fields: {
57 first_name: {
58 label: 'Custom Label'
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070059 }
Matteo Scandolo824a7cb2016-04-20 12:24:52 -070060 }
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070061 };
62
63 scope.model = {
64 id: 1,
65 first_name: 'Jhon',
66 last_name: 'Snow',
67 age: 25,
Matteo Scandolofb667b02016-04-20 16:36:17 -070068 email: 'test@onlab.us',
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070069 birthDate: '2016-04-18T23:44:16.883181Z',
70 enabled: true,
71 role: 'user', //select
72 a_permissions: [
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070073 ],
Matteo Scandolo741d6b42016-05-25 16:02:16 -070074 object_field: {
75 string: 'bar',
76 number: 1,
77 email: 'teo@onlab.us'
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070078 }
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 Scandolodf23cdb2016-05-25 17:37:37 -070092 it('should render 10 input field', () => {
Matteo Scandolofb667b02016-04-20 16:36:17 -070093 // boolean are in the form model, but are not input
94 expect(Object.keys(isolatedScope.formField).length).toEqual(9);
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070095 var field = element[0].getElementsByTagName('input');
Matteo Scandolo741d6b42016-05-25 16:02:16 -070096 expect(field.length).toEqual(10);
Matteo Scandoloc49f53c2016-04-20 11:38:42 -070097 });
98
Matteo Scandolofb667b02016-04-20 16:36:17 -070099 it('should render 1 boolean field', () => {
100 expect($(element).find('.boolean-field > button').length).toEqual(2)
101 });
102
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700103 it('when clicking on action should invoke callback', () => {
Matteo Scandolofb667b02016-04-20 16:36:17 -0700104 var link = $(element).find('[role="button"]');
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700105 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 Scandoloc49f53c2016-04-20 11:38:42 -0700114
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700115 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 Scandolofb667b02016-04-20 16:36:17 -0700118 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 Scandolo741d6b42016-05-25 16:02:16 -0700136 it('should change value to true', () => {
Matteo Scandolofb667b02016-04-20 16:36:17 -0700137 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 Scandolob389f7a2016-04-20 14:59:39 -0700143 });
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700144
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700145 // NOTE not sure why this tests are failing
146 xdescribe('the custom validation options', () => {
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700147 beforeEach(() => {
148 scope.config.fields.first_name.validators = {
149 minlength: 10,
150 maxlength: 15,
151 required: true
152 };
153
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700154 scope.config.fields.age = {
155 validators: {
156 min: 10,
157 max: 20
158 }
159 };
160
Matteo Scandolo28e49b72016-04-22 14:14:03 -0700161 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 Scandolo953ddad2016-04-25 08:15:24 -0700187
Matteo Scandolo2e804cb2016-04-27 16:29:33 -0700188 it('should validate min', () => {
Matteo Scandolo953ddad2016-04-25 08:15:24 -0700189 // 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 Scandolo28e49b72016-04-22 14:14:03 -0700196 });
Matteo Scandolob389f7a2016-04-20 14:59:39 -0700197 });
Matteo Scandoloc49f53c2016-04-20 11:38:42 -0700198 });
199 });
200})();