/** * Created by robin on 12/11/16. */ describe('lu-webapi-crud-business factory', function() { var luWebApiCrudBusiness; var fakeDataAccess; var $q; var $rootScope; // Before each test load our api.users module beforeEach(angular.mock.module('luticate2Utils')); // Before each test set our injected Users factory (_Users_) to our local Users variable beforeEach(inject(function(_luWebApiCrudBusiness_, _$q_, _$rootScope_) { luWebApiCrudBusiness = _luWebApiCrudBusiness_; $q = _$q_; $rootScope = _$rootScope_; })); beforeEach(function () { fakeDataAccess = { getSingleById: function(id, luBusyGroups) { var deferred = $q.defer(); if (id == 1) { deferred.resolve({ id: id, name: "Test." }); } else if (id == 2) { deferred.resolve({ id: id, name: "Test.2", obj: { value: 4242 } }); } else if (id == 3) { deferred.resolve({ id: id, name: "Test.2", obj: { value: 4242 }, createdAt: "2016-12-27T05:00:00-05:00", updatedAt: null }); } else if (id == 4) { deferred.resolve({ id: id, name: "Test.2", obj: { value: 4242 }, createdAt: "2016-12-27T05:00:00-05:00", updatedAt: "2016-12-28T05:00:00-05:00" }); } return deferred.promise; }, getMultiple: function (orderBy, filter, page, perPage, luBusyGroups) { var deferred = $q.defer(); deferred.resolve({ count: 42, data: [{ id: 1, name: "Test.", obj: { value: 4242 } }, { id: 2, name: "Test.2", obj: { anotherValue: 2424 } }] }); return deferred.promise; }, addDbo: function(data, luBusyGroups) { return this.getSingleById(1, luBusyGroups); }, editSingleByIdDbo: function(id, data, luBusyGroups) { return this.getSingleById(id, luBusyGroups); }, deleteDbo: function(id, luBusyGroups) { return this.getSingleById(id, luBusyGroups); } }; }); it('should add a dummy function to getSingleById dbo', function() { var business = luWebApiCrudBusiness.create(fakeDataAccess); business.defaultDbo = { id: null, name: "", dummyFunction: function () { return '_' + this.name + '_'; } }; var id = 1; business.getSingleById(id).then(function (data) { expect(data.id).toEqual(id); expect(data.name).toEqual("Test."); expect(data.dummyFunction).not.toBeNull(); expect(data.dummyFunction()).toEqual("_Test._"); }, function (error) { expect(error).toBeNull(); }); $rootScope.$digest(); }); it('should test complex extend', function() { var business = luWebApiCrudBusiness.create(fakeDataAccess); business.defaultDbo = { id: null, name: "", obj: { value: 42, anotherValue: 24 } }; var id = 2; business.getSingleById(id).then(function (data) { expect(data.id).toEqual(id); expect(data.name).toEqual("Test.2"); expect(data.obj).not.toBeNull(); expect(data.obj.value).toEqual(4242); expect(data.obj.anotherValue).toEqual(24); }, function (error) { expect(error).toBeNull(); }); $rootScope.$digest(); }); it('should add a dummy function to getSingleById dbo', function() { var business = luWebApiCrudBusiness.create(fakeDataAccess); business.defaultDbo = { id: null, name: "", dummyFunction: function () { return '_' + this.name + '_'; } }; business.getMultiple(null, null, 0, 2).then(function (data) { expect(data.count).toEqual(42); expect(data.data.length).toEqual(2); var i = 0; expect(data.data[i].id).toEqual(1); expect(data.data[i].name).toEqual("Test."); expect(data.data[i].dummyFunction).not.toBeNull(); expect(data.data[i].dummyFunction()).toEqual("_Test._"); i = 1; expect(data.data[i].id).toEqual(2); expect(data.data[i].name).toEqual("Test.2"); expect(data.data[i].dummyFunction).not.toBeNull(); expect(data.data[i].dummyFunction()).toEqual("_Test.2_"); }, function (error) { expect(error).toBeNull(); }); $rootScope.$digest(); }); it('should add a dummy function to addDbo dbo', function() { var business = luWebApiCrudBusiness.create(fakeDataAccess); business.defaultDbo = { id: null, name: "", dummyFunction: function () { return '_' + this.name + '_'; } }; var id = 1; business.addDbo(null).then(function (data) { expect(data.id).toEqual(id); expect(data.name).toEqual("Test."); expect(data.dummyFunction).not.toBeNull(); expect(data.dummyFunction()).toEqual("_Test._"); }, function (error) { expect(error).toBeNull(); }); $rootScope.$digest(); }); it('should add a dummy function to editSingleByIdDbo dbo', function() { var business = luWebApiCrudBusiness.create(fakeDataAccess); business.defaultDbo = { id: null, name: "", dummyFunction: function () { return '_' + this.name + '_'; } }; var id = 1; business.editSingleByIdDbo(id, null).then(function (data) { expect(data.id).toEqual(id); expect(data.name).toEqual("Test."); expect(data.dummyFunction).not.toBeNull(); expect(data.dummyFunction()).toEqual("_Test._"); }, function (error) { expect(error).toBeNull(); }); $rootScope.$digest(); }); it('should add a dummy function to deleteDbo dbo', function() { var business = luWebApiCrudBusiness.create(fakeDataAccess); business.defaultDbo = { id: null, name: "", dummyFunction: function () { return '_' + this.name + '_'; } }; var id = 1; business.deleteDbo(id, null).then(function (data) { expect(data.id).toEqual(id); expect(data.name).toEqual("Test."); expect(data.dummyFunction).not.toBeNull(); expect(data.dummyFunction()).toEqual("_Test._"); }, function (error) { expect(error).toBeNull(); }); $rootScope.$digest(); }); it('should convert createdAt into Date', function() { var business = luWebApiCrudBusiness.create(fakeDataAccess); business.defaultDbo = { id: null, name: "", createdAt: null, updatedAt: null, dummyFunction: function () { return '_' + this.name + '_'; } }; var id = 3; business.getSingleById(id, null).then(function (data) { expect(data.id).toEqual(id); expect(data.name).toEqual("Test.2"); expect(data.dummyFunction).not.toBeNull(); expect(data.dummyFunction()).toEqual("_Test.2_"); expect(data.createdAt).toEqual(new Date("2016-12-27T05:00:00-05:00")); expect(data.updatedAt).toBeNull(); }, function (error) { expect(error).toBeNull(); }); $rootScope.$digest(); }); it('should convert createdAt and updatedAt into Date', function() { var business = luWebApiCrudBusiness.create(fakeDataAccess); business.defaultDbo = { id: null, name: "", createdAt: null, updatedAt: null, dummyFunction: function () { return '_' + this.name + '_'; } }; var id = 4; business.getSingleById(id, null).then(function (data) { expect(data.id).toEqual(id); expect(data.name).toEqual("Test.2"); expect(data.dummyFunction).not.toBeNull(); expect(data.dummyFunction()).toEqual("_Test.2_"); expect(data.createdAt).toEqual(new Date("2016-12-27T05:00:00-05:00")); expect(data.updatedAt).toEqual(new Date("2016-12-28T05:00:00-05:00")); }, function (error) { expect(error).toBeNull(); }); $rootScope.$digest(); }); });