blob: 4cd3fec530a277b01cf67fc969f79e424d890ea1 [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
12 describe('The XosFormHelper service', () => {
13 let service;
14
15 let fields = [
16 'id',
17 'name',
18 'active',
19 'created',
20 'custom'
21 ];
22
23 let modelField = {
24 id: {},
25 name: {},
26 active: {},
27 created: {},
28 custom: {}
29 };
30
31 let model = {
32 id: 1,
33 name: 'test',
34 active: true,
35 created: '2016-04-18T23:44:16.883181Z',
36 custom: 'MyCustomValue'
37 };
38
39 let customField = {
40 custom: {
41 label: 'Custom Label',
42 type: 'number',
43 validators: {}
44 }
45 };
46
47 let formObject = {
48 id: {
49 label: 'Id:',
50 type: 'number',
51 validators: {}
52 },
53 name: {
54 label: 'Name:',
55 type: 'string',
56 validators: {}
57 },
58 active: {
59 label: 'Active:',
60 type: 'boolean',
61 validators: {}
62 },
63 created: {
64 label: 'Created:',
65 type: 'date',
66 validators: {}
67 },
68 custom: {
69 label: 'Custom Label:',
70 type: 'number',
71 validators: {}
72 }
73 };
74
75 // load the application module
76 beforeEach(module('xos.helpers'));
77
78 // inject the cartService
79 beforeEach(inject(function (_XosFormHelpers_) {
80 // The injector unwraps the underscores (_) from around the parameter names when matching
81 service = _XosFormHelpers_;
82 }));
83
84 describe('the _getFieldFormat method', () => {
85 xit('should return string', () => {
86 expect(service._getFieldFormat('string')).toEqual('string');
87 });
88 it('should return number', () => {
89 expect(service._getFieldFormat(1)).toEqual('number');
90 expect(service._getFieldFormat('1')).toEqual('number');
91 });
92 xit('should return boolean', () => {
93 expect(service._getFieldFormat(false)).toEqual('boolean');
94 expect(service._getFieldFormat(true)).toEqual('boolean');
95 });
96
97 it('should return date', () => {
98 expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('string');
99 expect(service._getFieldFormat(new Date())).toEqual('date');
100 expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date');
101 });
102 });
103
104 xit('should convert the fields array in an empty form object', () => {
105 expect(service.parseModelField(fields)).toEqual(modelField);
106 });
107
108 xit('should combine modelField and customField in a form object', () => {
109 expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject);
110 });
111 });
112
113 xdescribe('The xos-form component', () => {
114
115 let element, scope, isolatedScope;
116
117 beforeEach(module('xos.helpers'));
118
119 it('should throw an error if no config is specified', inject(($compile, $rootScope) => {
120 function errorFunctionWrapper(){
121 $compile(angular.element('<xos-form></xos-form>'))($rootScope);
122 $rootScope.$digest();
123 }
124 expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide a configuration via the "config" attribute'));
125 }));
126
127 it('should throw an error if no actions is specified', inject(($compile, $rootScope) => {
128 function errorFunctionWrapper(){
129 let scope = $rootScope.$new();
130 scope.config = 'green';
131 $compile(angular.element('<xos-form config="config"></xos-form>'))(scope);
132 $rootScope.$digest();
133 }
134 expect(errorFunctionWrapper).toThrow(new Error('[xosForm] Please provide an action list in the configuration'));
135 }));
136
137 describe('when correctly configured', () => {
138
139 let cb = jasmine.createSpy('callback');
140
141 beforeEach(inject(($compile, $rootScope) => {
142
143
144 scope = $rootScope.$new();
145
146 scope.config = {
147 exclude: ['excludedField'],
148 actions: [
149 {
150 label: 'Save',
151 icon: 'ok', // refers to bootstraps glyphicon
152 cb: cb,
153 class: 'success'
154 }
155 ],
156 fields: [
157 {
158 first_name: {
159 label: 'Custom Label'
160 }
161 }
162 ]
163 };
164
165 scope.model = {
166 id: 1,
167 first_name: 'Jhon',
168 last_name: 'Snow',
169 age: 25,
170 birthDate: '2016-04-18T23:44:16.883181Z',
171 enabled: true,
172 role: 'user', //select
173 a_permissions: [
174
175 ],
176 o_permissions: {
177
178 }
179 };
180
181 element = angular.element(`<xos-form config="config" ng-model="model"></xos-form>`);
182 $compile(element)(scope);
183 scope.$digest();
184 isolatedScope = element.isolateScope().vm;
185 }));
186
187 it('should add excluded properties to the list', () => {
188 let expected = ['id', 'validators', 'created', 'updated', 'deleted', 'backend_status', 'excludedField'];
189 expect(isolatedScope.excludedField).toEqual(expected);
190 });
191
192 it('should render 8 field', () => {
193 expect(isolatedScope.formField.length).toEqual(8);
194 var field = element[0].getElementsByTagName('input');
195 expect(field.length).toEqual(8);
196 });
197
198 it('when clicking on action should invoke callback', () => {
199 var link = element[0].getElementsByTagName('button')[0];
200 link.click();
201 expect(cb).toHaveBeenCalledWith(scope.model);
202 });
203
204 it('should set a custom label', () => {
205 let nameField = element[0].getElementsByClassName('form-group')[0];
206 let label = angular.element(nameField.getElementsByTagName('label')[0]).text()
207 expect(label).toEqual('Custom Label:');
208 });
209 });
210
211 });
212 });
213})();