You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lu-webapi-crud-business.spec.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Created by robin on 12/11/16.
  3. */
  4. describe('lu-webapi-crud-business factory', function() {
  5. var luWebApiCrudBusiness;
  6. var fakeDataAccess;
  7. var $q;
  8. var $rootScope;
  9. // Before each test load our api.users module
  10. beforeEach(angular.mock.module('luticate2Utils'));
  11. // Before each test set our injected Users factory (_Users_) to our local Users variable
  12. beforeEach(inject(function(_luWebApiCrudBusiness_, _$q_, _$rootScope_) {
  13. luWebApiCrudBusiness = _luWebApiCrudBusiness_;
  14. $q = _$q_;
  15. $rootScope = _$rootScope_;
  16. }));
  17. beforeEach(function () {
  18. fakeDataAccess = {
  19. getSingleById: function(id, luBusyGroups) {
  20. var deferred = $q.defer();
  21. if (id == 1) {
  22. deferred.resolve({
  23. id: id,
  24. name: "Test."
  25. });
  26. }
  27. else if (id == 2) {
  28. deferred.resolve({
  29. id: id,
  30. name: "Test.2",
  31. obj: {
  32. value: 4242
  33. }
  34. });
  35. }
  36. return deferred.promise;
  37. },
  38. getMultiple: function (orderBy, filter, page, perPage, luBusyGroups) {
  39. var deferred = $q.defer();
  40. deferred.resolve({
  41. count: 42,
  42. data: [{
  43. id: 1,
  44. name: "Test.",
  45. obj: {
  46. value: 4242
  47. }
  48. }, {
  49. id: 2,
  50. name: "Test.2",
  51. obj: {
  52. anotherValue: 2424
  53. }
  54. }]
  55. });
  56. return deferred.promise;
  57. },
  58. addDbo: function(data, luBusyGroups) {
  59. return this.getSingleById(1, luBusyGroups);
  60. },
  61. editSingleByIdDbo: function(id, data, luBusyGroups) {
  62. return this.getSingleById(id, luBusyGroups);
  63. },
  64. deleteDbo: function(id, luBusyGroups) {
  65. return this.getSingleById(id, luBusyGroups);
  66. }
  67. };
  68. });
  69. it('should add a dummy function to getSingleById dbo', function()
  70. {
  71. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  72. business.defaultDbo = {
  73. id: null,
  74. name: "",
  75. dummyFunction: function () {
  76. return '_' + this.name + '_';
  77. }
  78. };
  79. var id = 1;
  80. business.getSingleById(id).then(function (data) {
  81. expect(data.id).toEqual(id);
  82. expect(data.name).toEqual("Test.");
  83. expect(data.dummyFunction).not.toBeNull();
  84. expect(data.dummyFunction()).toEqual("_Test._");
  85. }, function (error) {
  86. expect(error).toBeNull();
  87. });
  88. $rootScope.$digest();
  89. });
  90. it('should test complex extend', function()
  91. {
  92. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  93. business.defaultDbo = {
  94. id: null,
  95. name: "",
  96. obj: {
  97. value: 42,
  98. anotherValue: 24
  99. }
  100. };
  101. var id = 2;
  102. business.getSingleById(id).then(function (data) {
  103. expect(data.id).toEqual(id);
  104. expect(data.name).toEqual("Test.2");
  105. expect(data.obj).not.toBeNull();
  106. expect(data.obj.value).toEqual(4242);
  107. expect(data.obj.anotherValue).toEqual(24);
  108. }, function (error) {
  109. expect(error).toBeNull();
  110. });
  111. $rootScope.$digest();
  112. });
  113. it('should add a dummy function to getSingleById dbo', function()
  114. {
  115. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  116. business.defaultDbo = {
  117. id: null,
  118. name: "",
  119. dummyFunction: function () {
  120. return '_' + this.name + '_';
  121. }
  122. };
  123. business.getMultiple(null, null, 0, 2).then(function (data) {
  124. expect(data.count).toEqual(42);
  125. expect(data.data.length).toEqual(2);
  126. var i = 0;
  127. expect(data.data[i].id).toEqual(1);
  128. expect(data.data[i].name).toEqual("Test.");
  129. expect(data.data[i].dummyFunction).not.toBeNull();
  130. expect(data.data[i].dummyFunction()).toEqual("_Test._");
  131. i = 1;
  132. expect(data.data[i].id).toEqual(2);
  133. expect(data.data[i].name).toEqual("Test.2");
  134. expect(data.data[i].dummyFunction).not.toBeNull();
  135. expect(data.data[i].dummyFunction()).toEqual("_Test.2_");
  136. }, function (error) {
  137. expect(error).toBeNull();
  138. });
  139. $rootScope.$digest();
  140. });
  141. it('should add a dummy function to addDbo dbo', function()
  142. {
  143. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  144. business.defaultDbo = {
  145. id: null,
  146. name: "",
  147. dummyFunction: function () {
  148. return '_' + this.name + '_';
  149. }
  150. };
  151. var id = 1;
  152. business.addDbo(null).then(function (data) {
  153. expect(data.id).toEqual(id);
  154. expect(data.name).toEqual("Test.");
  155. expect(data.dummyFunction).not.toBeNull();
  156. expect(data.dummyFunction()).toEqual("_Test._");
  157. }, function (error) {
  158. expect(error).toBeNull();
  159. });
  160. $rootScope.$digest();
  161. });
  162. it('should add a dummy function to editSingleByIdDbo dbo', function()
  163. {
  164. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  165. business.defaultDbo = {
  166. id: null,
  167. name: "",
  168. dummyFunction: function () {
  169. return '_' + this.name + '_';
  170. }
  171. };
  172. var id = 1;
  173. business.editSingleByIdDbo(id, null).then(function (data) {
  174. expect(data.id).toEqual(id);
  175. expect(data.name).toEqual("Test.");
  176. expect(data.dummyFunction).not.toBeNull();
  177. expect(data.dummyFunction()).toEqual("_Test._");
  178. }, function (error) {
  179. expect(error).toBeNull();
  180. });
  181. $rootScope.$digest();
  182. });
  183. it('should add a dummy function to deleteDbo dbo', function()
  184. {
  185. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  186. business.defaultDbo = {
  187. id: null,
  188. name: "",
  189. dummyFunction: function () {
  190. return '_' + this.name + '_';
  191. }
  192. };
  193. var id = 1;
  194. business.deleteDbo(id, null).then(function (data) {
  195. expect(data.id).toEqual(id);
  196. expect(data.name).toEqual("Test.");
  197. expect(data.dummyFunction).not.toBeNull();
  198. expect(data.dummyFunction()).toEqual("_Test._");
  199. }, function (error) {
  200. expect(error).toBeNull();
  201. });
  202. $rootScope.$digest();
  203. });
  204. });