Added tests for XosFormHelper Service
Change-Id: Ic4e4102e6b34b8304e02d3207c426a5b5fa7670d
diff --git a/src/app/core/form/form-helpers.spec.ts b/src/app/core/form/form-helpers.spec.ts
new file mode 100644
index 0000000..52a9769
--- /dev/null
+++ b/src/app/core/form/form-helpers.spec.ts
@@ -0,0 +1,56 @@
+import * as angular from 'angular';
+import 'angular-mocks';
+import {IXosFormHelpersService, XosFormHelpers} from './form-helpers';
+
+let service: IXosFormHelpersService;
+
+describe('The XosFormHelpers service', () => {
+
+ beforeEach(() => {
+ angular.module('formHelpers', [])
+ .service('XosFormHelpers', XosFormHelpers);
+ angular.mock.module('formHelpers');
+
+ angular.mock.inject((
+ XosFormHelpers: IXosFormHelpersService,
+ ) => {
+ service = XosFormHelpers;
+ });
+ });
+
+ describe('the getFieldFormat method', () => {
+ it('should return text', () => {
+ expect(service._getFieldFormat('cordSubscriber-1')).toEqual('text');
+ expect(service._getFieldFormat('a random text')).toEqual('text');
+ expect(service._getFieldFormat(null)).toEqual('text');
+ expect(service._getFieldFormat('1')).toEqual('text');
+ });
+ it('should return mail', () => {
+ expect(service._getFieldFormat('test@onlab.us')).toEqual('email');
+ expect(service._getFieldFormat('testonlab.us')).not.toEqual('email');
+ });
+ it('should return number', () => {
+ expect(service._getFieldFormat(1)).toEqual('number');
+ });
+ it('should return boolean', () => {
+ expect(service._getFieldFormat(false)).toEqual('boolean');
+ expect(service._getFieldFormat(true)).toEqual('boolean');
+ });
+
+ it('should return date', () => {
+ expect(service._getFieldFormat('2016-04-19T23:09:1092Z')).toEqual('text');
+ expect(service._getFieldFormat(new Date())).toEqual('date');
+ expect(service._getFieldFormat('2016-04-19T23:09:10.208092Z')).toEqual('date');
+ });
+
+ it('should return array', () => {
+ expect(service._getFieldFormat([])).toEqual('array');
+ expect(service._getFieldFormat(['a', 'b'])).toEqual('array');
+ });
+
+ it('should return object', () => {
+ expect(service._getFieldFormat({})).toEqual('object');
+ expect(service._getFieldFormat({foo: 'bar'})).toEqual('object');
+ });
+ });
+});
diff --git a/src/app/core/form/form-helpers.ts b/src/app/core/form/form-helpers.ts
index bbad2c8..3ae3fe6 100644
--- a/src/app/core/form/form-helpers.ts
+++ b/src/app/core/form/form-helpers.ts
@@ -1,20 +1,10 @@
-import {IXosFormInput} from './form';
-
export interface IXosFormHelpersService {
_getFieldFormat(value: any): string;
- parseModelField(fields: any): any[];
- buildFormStructure(modelField: any[], customField: any[], model: any, order: string[]): any;
- buildFormData(fields: IXosFormInput[], model: any): any;
}
-export class XosFormHelpers {
+export class XosFormHelpers implements IXosFormHelpersService {
static $inject = [];
- public _isEmail = (text) => {
- const re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
- return re.test(text);
- };
-
public _getFieldFormat = (value) => {
if (angular.isArray(value)) {
return 'array';
@@ -48,5 +38,10 @@
return typeof value;
};
+
+ private _isEmail = (text) => {
+ const re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
+ return re.test(text);
+ };
}