123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- /**
- * Created by robin on 12/11/16.
- */
-
- describe('lu-webapi-crud-dataaccess model factory', function() {
- var luWebApiCrudDataAccess;
- var fakeDataAccess;
- var $q;
- var $rootScope;
-
- beforeEach(angular.mock.module('luticate2Utils'));
-
- beforeEach(function () {
- module(function ($provide) {
- $provide.value('luWebApiDataAccess', {
- create: function (entry_point) {
- return {
-
- get: function (url, dataGet, luBusyGroups) {
- var deferred = $q.defer();
- if (url == '') {
- fakeDataAccess.getMultiple(dataGet.orderBy, dataGet.filter, dataGet.page, dataGet.perPage, deferred);
- }
- else {
- fakeDataAccess.getSingleById(url, deferred);
- }
- // deferred.resolve({
- // get: dataGet,
- // post: null,
- // url: entry_point + url
- // });
- return deferred.promise;
- },
-
- post: function (url, dataGet, dataPost, luBusyGroups) {
- var deferred = $q.defer();
- if (url == '') {
- fakeDataAccess.addDbo(dataPost, deferred);
- }
- else {
- fakeDataAccess.editSingleByIdDbo(url, dataPost, deferred);
- }
- deferred.resolve({
- get: dataGet,
- post: dataPost,
- url: entry_point + url
- });
- return deferred.promise;
- },
-
- put: function (url, dataGet, dataPost, luBusyGroups) {
- var deferred = $q.defer();
- deferred.resolve({
- get: dataGet,
- post: dataPost,
- url: entry_point + url
- });
- return deferred.promise;
- },
-
- delete: function (url, dataGet, dataPost, luBusyGroups) {
- var deferred = $q.defer();
- fakeDataAccess.deleteDbo(url, deferred);
- return deferred.promise;
- }
- }
- }
- });
- });
- });
-
- beforeEach(function () {
- fakeDataAccess = {
- getSingleById: function(id, deferred) {
- 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, deferred) {
- 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, deferred) {
- return this.getSingleById(1, deferred);
- },
- editSingleByIdDbo: function(id, data, deferred) {
- return this.getSingleById(id, deferred);
- },
- deleteDbo: function(id, deferred) {
- return this.getSingleById(id, deferred);
- }
- };
- });
-
- beforeEach(inject(function(_luWebApiCrudDataAccess_, _$q_, _$rootScope_) {
- luWebApiCrudDataAccess = _luWebApiCrudDataAccess_;
- $q = _$q_;
- $rootScope = _$rootScope_;
- }));
-
-
- it('should add a dummy function to getSingleById dbo', function()
- {
- var dataAccess = luWebApiCrudDataAccess.create('');
- dataAccess.defaultDbo = {
- id: null,
- name: "",
- dummyFunction: function () {
- return '_' + this.name + '_';
- }
- };
-
- var id = 1;
- dataAccess.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 dataAccess = luWebApiCrudDataAccess.create('');
- dataAccess.defaultDbo = {
- id: null,
- name: "",
- obj: {
- value: 42,
- anotherValue: 24
- }
- };
-
- var id = 2;
- dataAccess.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 dataAccess = luWebApiCrudDataAccess.create('');
- dataAccess.defaultDbo = {
- id: null,
- name: "",
- dummyFunction: function () {
- return '_' + this.name + '_';
- }
- };
-
- dataAccess.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 dataAccess = luWebApiCrudDataAccess.create('');
- dataAccess.defaultDbo = {
- id: null,
- name: "",
- dummyFunction: function () {
- return '_' + this.name + '_';
- }
- };
-
- var id = 1;
- dataAccess.addDbo({}).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 dataAccess = luWebApiCrudDataAccess.create('');
- dataAccess.defaultDbo = {
- id: null,
- name: "",
- dummyFunction: function () {
- return '_' + this.name + '_';
- }
- };
-
- var id = 1;
- dataAccess.editSingleByIdDbo(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 add a dummy function to deleteDbo dbo', function()
- {
- var dataAccess = luWebApiCrudDataAccess.create('');
- dataAccess.defaultDbo = {
- id: null,
- name: "",
- dummyFunction: function () {
- return '_' + this.name + '_';
- }
- };
-
- var id = 1;
- dataAccess.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 dataAccess = luWebApiCrudDataAccess.create('');
- dataAccess.defaultDbo = {
- id: null,
- name: "",
- createdAt: null,
- updatedAt: null,
- dummyFunction: function () {
- return '_' + this.name + '_';
- }
- };
-
- var id = 3;
- dataAccess.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 dataAccess = luWebApiCrudDataAccess.create('');
- dataAccess.defaultDbo = {
- id: null,
- name: "",
- createdAt: null,
- updatedAt: null,
- dummyFunction: function () {
- return '_' + this.name + '_';
- }
- };
-
- var id = 4;
- dataAccess.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();
- });
- });
|