blob: 4b33a5a97b0756e78cd0da750294c7a368608857 [file] [log] [blame]
Matteo Scandolo7bc39c42016-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', () => {
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -070085 it('should return string', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070086 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 });
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -070092 it('should return boolean', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070093 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
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700104 it('should convert the fields array in an empty form object', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700105 expect(service.parseModelField(fields)).toEqual(modelField);
106 });
107
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700108 it('should combine modelField and customField in a form object', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700109 expect(service.buildFormStructure(modelField, customField, model)).toEqual(formObject);
110 });
111 });
112
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700113 describe('The xos-form component', () => {
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700114
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 ],
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700156 fields: {
157 first_name: {
158 label: 'Custom Label'
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700159 }
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700160 }
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700161 };
162
163 scope.model = {
164 id: 1,
165 first_name: 'Jhon',
166 last_name: 'Snow',
167 age: 25,
168 birthDate: '2016-04-18T23:44:16.883181Z',
169 enabled: true,
170 role: 'user', //select
171 a_permissions: [
172
173 ],
174 o_permissions: {
175
176 }
177 };
178
179 element = angular.element(`<xos-form config="config" ng-model="model"></xos-form>`);
180 $compile(element)(scope);
181 scope.$digest();
182 isolatedScope = element.isolateScope().vm;
183 }));
184
185 it('should add excluded properties to the list', () => {
186 let expected = ['id', 'validators', 'created', 'updated', 'deleted', 'backend_status', 'excludedField'];
187 expect(isolatedScope.excludedField).toEqual(expected);
188 });
189
190 it('should render 8 field', () => {
Matteo Scandolo9f0e5ae2016-04-20 12:24:52 -0700191 expect(Object.keys(isolatedScope.formField).length).toEqual(8);
Matteo Scandolo7bc39c42016-04-20 11:38:42 -0700192 var field = element[0].getElementsByTagName('input');
193 expect(field.length).toEqual(8);
194 });
195
196 it('when clicking on action should invoke callback', () => {
197 var link = element[0].getElementsByTagName('button')[0];
198 link.click();
199 expect(cb).toHaveBeenCalledWith(scope.model);
200 });
201
202 it('should set a custom label', () => {
203 let nameField = element[0].getElementsByClassName('form-group')[0];
204 let label = angular.element(nameField.getElementsByTagName('label')[0]).text()
205 expect(label).toEqual('Custom Label:');
206 });
207 });
208
209 });
210 });
211})();