Preparation to bower release
diff --git a/spec/services/helpers/comparator.test.js b/spec/services/helpers/comparator.test.js
new file mode 100644
index 0000000..66e26d3
--- /dev/null
+++ b/spec/services/helpers/comparator.test.js
@@ -0,0 +1,60 @@
+(function () {
+ 'use strict';
+
+ describe('The xos.helper module', function(){
+ describe('The Comparator service', () => {
+
+ let service;
+
+ // load the application module
+ beforeEach(module('xos.helpers'));
+
+ // inject the cartService
+ beforeEach(inject(function (_Comparator_) {
+ // The injector unwraps the underscores (_) from around the parameter names when matching
+ service = _Comparator_;
+ }));
+
+ describe('given a string', () => {
+ it('should return true if expected is substring of actual', () => {
+ const res = service('test', 'te');
+ expect(res).toBeTruthy();
+ });
+
+ it('should return false if expected is not substring of actual', () => {
+ const res = service('test', 'ab');
+ expect(res).toBeFalsy();
+ });
+ });
+
+ describe('given a boolean', () => {
+ it('should return true if values match', () => {
+ expect(service(false, false)).toBeTruthy();
+ expect(service(true, true)).toBeTruthy();
+ expect(service(0, false)).toBeTruthy();
+ expect(service(1, true)).toBeTruthy();
+ });
+
+ it('should return false if values doesn\'t match', () => {
+ expect(service(false, true)).toBeFalsy();
+ expect(service(true, false)).toBeFalsy();
+ expect(service(1, false)).toBeFalsy();
+ expect(service(0, true)).toBeFalsy();
+ });
+ });
+
+ describe('given a number', () => {
+ // NOTE if numbers should we compare with === ??
+ it('should return true if expected is substring of actual', () => {
+ expect(service(12, 1)).toBeTruthy();
+ });
+
+ it('should return false if expected is not substring of actual', () => {
+ expect(service(12, 3)).toBeFalsy();
+ });
+ });
+
+ });
+ });
+
+})();
\ No newline at end of file
diff --git a/spec/services/helpers/form.helpers.test.js b/spec/services/helpers/form.helpers.test.js
new file mode 100644
index 0000000..a126db5
--- /dev/null
+++ b/spec/services/helpers/form.helpers.test.js
@@ -0,0 +1,335 @@
+/**
+ * © OpenCORD
+ *
+ * Created by teone on 5/25/16.
+ */
+
+(function () {
+ 'use strict';
+
+ describe('The xos.helper module', function(){
+
+ describe('The XosFormHelper service', () => {
+ let service;
+
+ let fields = [
+ 'id',
+ 'name',
+ 'mail',
+ 'active',
+ 'created'
+ ];
+
+ let modelField = {
+ id: {},
+ name: {},
+ mail: {},
+ active: {},
+ created: {}
+ };
+
+ let model = {
+ id: 1,
+ name: 'test',
+ mail: 'test@onlab.us',
+ active: true,
+ created: '2016-04-18T23:44:16.883181Z',
+ custom: 'MyCustomValue'
+ };
+
+ let customField = {
+ id: {
+ label: 'Id',
+ type: 'number',
+ validators: {
+ required: true
+ },
+ hint: ''
+ },
+ custom: {
+ label: 'Custom Label',
+ type: 'number',
+ validators: {},
+ hint: 'Test Hint'
+ }
+ };
+
+ let formObject = {
+ id: {
+ label: 'Id:',
+ type: 'number',
+ validators: {
+ required: true
+ },
+ hint: ''
+ },
+ name: {
+ label: 'Name:',
+ type: 'text',
+ validators: {},
+ hint: ''
+ },
+ mail: {
+ label: 'Mail:',
+ type: 'email',
+ validators: {},
+ hint: ''
+ },
+ active: {
+ label: 'Active:',
+ type: 'boolean',
+ validators: {},
+ hint: ''
+ },
+ created: {
+ label: 'Created:',
+ type: 'date',
+ validators: {},
+ hint: ''
+ },
+ custom: {
+ label: 'Custom Label:',
+ type: 'number',
+ validators: {},
+ hint: 'Test Hint'
+ }
+ };
+
+ // load the application module
+ beforeEach(module('xos.helpers'));
+
+ // inject the cartService
+ beforeEach(inject(function (_XosFormHelpers_) {
+ // The injector unwraps the underscores (_) from around the parameter names when matching
+ service = _XosFormHelpers_;
+ }));
+
+ describe('the _isEmail method', () => {
+ it('should return true', () => {
+ expect(service._isEmail('test@onlab.us')).toEqual(true);
+ });
+ it('should return false', () => {
+ expect(service._isEmail('testonlab.us')).toEqual(false);
+ expect(service._isEmail('test@onlab')).toEqual(false);
+ });
+ });
+
+ describe('the _getFieldFormat method', () => {
+ it('should return 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');
+ });
+ 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');
+ });
+ });
+
+ describe('the parseModelField mehtod', () => {
+ it('should convert the fields array in an empty form object', () => {
+ expect(service.parseModelField(fields)).toEqual(modelField);
+ });
+
+ xit('should handle nested config', () => {
+
+ });
+ });
+
+ describe('when modelField are provided', () => {
+ it('should combine modelField and customField in a form object', () => {
+ const form = service.buildFormStructure(modelField, customField, model);
+ expect(form).toEqual(formObject);
+ });
+
+ it('should override modelField properties whith customField properties', () => {
+ const customFieldOverride = {
+ id: {
+ hint: 'something',
+ type: 'select',
+ options: [
+ {id: 1, label: 'one'},
+ {id: 2, label: 'two'}
+ ],
+ validators: {
+ required: true
+ }
+ }
+ };
+ const form = service.buildFormStructure({id: {}}, customFieldOverride, model);
+
+ expect(form).toEqual({
+ id: {
+ label: 'Id:',
+ validators: {required: true},
+ hint: customFieldOverride.id.hint,
+ type: customFieldOverride.id.type,
+ options: customFieldOverride.id.options
+ }
+ });
+ });
+ });
+
+ describe('when model field is an empty array', () => {
+ let empty_modelField = {
+ // 5: {}
+ };
+ let empty_customFields = {
+ id: {
+ label: 'Id',
+ type: 'number'
+ },
+ name: {
+ label: 'Name',
+ type: 'text'
+ },
+ mail: {
+ label: 'Mail',
+ type: 'email'
+ },
+ active: {
+ label: 'Active',
+ type: 'boolean'
+ },
+ created: {
+ label: 'Created',
+ type: 'date'
+ },
+ custom: {
+ label: 'Custom Label',
+ type: 'number',
+ hint: 'Test Hint'
+ },
+ select: {
+ label: 'Select Label',
+ type: 'select',
+ hint: 'Select Hint',
+ options: [
+ {id: 1, label: 'something'}
+ ]
+ },
+ object: {
+ label: 'Object Label',
+ type: 'object',
+ hint: 'Object Hint',
+ properties: {
+ foo: {
+ type: 'string',
+ label: 'FooLabel',
+ validators: {
+ required: true
+ }
+ },
+ bar: {
+ type: 'number'
+ }
+ }
+ }
+ };
+
+ let empty_formObject = {
+ id: {
+ label: 'Id:',
+ type: 'number',
+ validators: {},
+ hint: ''
+ },
+ name: {
+ label: 'Name:',
+ type: 'text',
+ validators: {},
+ hint: ''
+ },
+ mail: {
+ label: 'Mail:',
+ type: 'email',
+ validators: {},
+ hint: ''
+ },
+ active: {
+ label: 'Active:',
+ type: 'boolean',
+ validators: {},
+ hint: ''
+ },
+ created: {
+ label: 'Created:',
+ type: 'date',
+ validators: {},
+ hint: ''
+ },
+ custom: {
+ label: 'Custom Label:',
+ type: 'number',
+ validators: {},
+ hint: 'Test Hint'
+ },
+ select: {
+ label: 'Select Label:',
+ type: 'select',
+ hint: 'Select Hint',
+ validators: {},
+ options: [
+ {id: 1, label: 'something'}
+ ]
+ },
+ object: {
+ label: 'Object Label:',
+ type: 'object',
+ hint: 'Object Hint',
+ validators: {},
+ properties: {
+ foo: {
+ type: 'string',
+ label: 'FooLabel',
+ validators: {
+ required: true
+ }
+ },
+ bar: {
+ type: 'number'
+ }
+ }
+ }
+ };
+
+ let empty_model = {5: 'Nan'}
+
+ it('should create a form object', () => {
+ let res = service.buildFormStructure(empty_modelField, empty_customFields, empty_model);
+ expect(res.id).toEqual(empty_formObject.id);
+ expect(res.name).toEqual(empty_formObject.name);
+ expect(res.mail).toEqual(empty_formObject.mail);
+ expect(res.active).toEqual(empty_formObject.active);
+ expect(res.created).toEqual(empty_formObject.created);
+ expect(res.custom).toEqual(empty_formObject.custom);
+ expect(res.select).toEqual(empty_formObject.select);
+ expect(res.object).toEqual(empty_formObject.object);
+ expect(res).toEqual(empty_formObject);
+ });
+ });
+ });
+ });
+})();
\ No newline at end of file
diff --git a/spec/services/helpers/user-prefs.test.js b/spec/services/helpers/user-prefs.test.js
new file mode 100644
index 0000000..63b55c9
--- /dev/null
+++ b/spec/services/helpers/user-prefs.test.js
@@ -0,0 +1,183 @@
+/**
+ * © OpenCORD
+ *
+ * Created by teone on 5/25/16.
+ */
+
+(function () {
+ 'use strict';
+
+ let cookies = {
+ xosUserPrefs: JSON.stringify({test: true})
+ };
+
+ const cookieMock = {
+ get: (name) => {
+ return cookies[name]
+ },
+ put: (name, value) => {
+ cookies[name] = value
+ }
+ };
+
+ describe('The xos.helper module', function(){
+
+ describe('The XosUserPrefs service', () => {
+ let service, deffered, rootScope;
+
+ // load the application module
+ beforeEach(module('xos.helpers', ($provide) => {
+ $provide.value('$cookies', cookieMock);
+ }));
+
+ // inject the cartService
+ beforeEach(inject(function (_XosUserPrefs_) {
+ // The injector unwraps the underscores (_) from around the parameter names when matching
+ service = _XosUserPrefs_;
+ spyOn(cookieMock, 'put').and.callThrough();
+ }));
+
+ beforeEach(inject(($q, $rootScope) => {
+ rootScope = $rootScope;
+ }));
+ it('should exists and have methods', () => {
+ expect(service).toBeDefined();
+ expect(service.getAll).toBeDefined();
+ expect(service.setAll).toBeDefined();
+ expect(service.getSynchronizerNotificationStatus).toBeDefined();
+ expect(service.setSynchronizerNotificationStatus).toBeDefined();
+ expect(service.setUserDetailsCookie).toBeDefined();
+ expect(service.getUserDetailsCookie).toBeDefined();
+ });
+
+ describe('the getAll method', () => {
+ it('should return all the stored prefs', () => {
+ let prefs = service.getAll();
+ expect(prefs).toEqual(JSON.parse(cookies.xosUserPrefs));
+ });
+ });
+
+ describe('the setAll method', () => {
+ it('should override all preferences', () => {
+ service.setAll({test: true, updated: true});
+ expect(JSON.parse(cookies.xosUserPrefs)).toEqual({test: true, updated: true});
+ });
+ });
+
+ describe('the synchronizers status', () => {
+ let syncNotification;
+ beforeEach(() => {
+ syncNotification = {
+ synchronizers: {
+ notification: {
+ first: true,
+ second: false
+ }
+ },
+ userData: {
+ userId: 1
+ }
+ }
+ cookies.xosUserPrefs = JSON.stringify(syncNotification);
+ });
+
+ describe('the getSynchronizerNotificationStatus method', () => {
+ it('should return notification status for all synchronizers', () => {
+ expect(service.getSynchronizerNotificationStatus()).toEqual(syncNotification.synchronizers.notification);
+ });
+
+ it('should return notification status for a single synchronizers', () => {
+ expect(service.getSynchronizerNotificationStatus('first')).toEqual(syncNotification.synchronizers.notification.first);
+ expect(service.getSynchronizerNotificationStatus('second')).toEqual(syncNotification.synchronizers.notification.second);
+ });
+ });
+
+ describe('the setSynchronizerNotificationStatus', () => {
+
+ it('should throw an error if called without synchronizer name', () => {
+ function wrapper (){
+ service.setSynchronizerNotificationStatus();
+ }
+ expect(wrapper).toThrowError('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.');
+ });
+
+ it('should update a synchronizer notification status', () => {
+ service.setSynchronizerNotificationStatus('second', true);
+ expect(service.getSynchronizerNotificationStatus('second')).toEqual(true);
+ expect(service.getSynchronizerNotificationStatus('first')).toEqual(true);
+
+ // should persist the change
+ expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"synchronizers":{"notification":{"first":true,"second":true}},"userData":{"userId":1}}');
+ });
+
+ it('should handle empty cookies', () => {
+ cookies.xosUserPrefs = '';
+ service.setSynchronizerNotificationStatus('second', true);
+ expect(service.getSynchronizerNotificationStatus('second')).toEqual(true);
+ });
+ });
+ });
+
+ describe('the userdetails status', () => {
+ let userdata, meMock;
+ const userData = {
+ current_user_id: 2
+ };
+ beforeEach(inject(($q, Me) => {
+ userdata = {
+ userData: {
+ current_user_id: 1
+ }
+ }
+ cookies.xosUserPrefs = JSON.stringify(userdata);
+ deffered= $q.defer();
+ meMock = Me;
+ spyOn(meMock, 'get').and.callFake(function(){
+ return deffered.promise;
+ });
+ }));
+ describe('Get user data method', ()=>{
+ it('it should return the stored data', (done) => {
+ service.getUserDetailsCookie().$promise.then((data) => {
+ expect(data).toEqual(userdata.userData);
+ done();
+ });
+ rootScope.$apply();
+ });
+
+ it('Should call the Api to return the data', (done) => {
+ cookies.xosUserPrefs = JSON.stringify();
+ service.getUserDetailsCookie().$promise.then((res) => {
+ expect(res.userId).toEqual(userData.userId);
+ expect(meMock.get).toHaveBeenCalled();
+ done();
+ });
+ deffered.resolve(userData);
+ rootScope.$apply();
+ });
+
+ });
+ describe('Set user data method', ()=>{
+ it('it should save the provided data', (done) => {
+ service.setUserDetailsCookie(userData).$promise.then(data => {
+ expect(data).toEqual(userData);
+ expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"userData":{"current_user_id":2}}');
+ done();
+ });
+ rootScope.$apply();
+ });
+ it('Should call the Api to save the data', (done)=>{
+ service.setUserDetailsCookie().$promise.then(data => {
+ expect(meMock.get).toHaveBeenCalled();
+ expect(data).toEqual(userData);
+ expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"userData":{"current_user_id":2}}');
+ done();
+ });
+ deffered.resolve(userData);
+ rootScope.$apply();
+ });
+ });
+ });
+ });
+ });
+})();
\ No newline at end of file