Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. else if (id == 3) {
  37. deferred.resolve({
  38. id: id,
  39. name: "Test.2",
  40. obj: {
  41. value: 4242
  42. },
  43. createdAt: "2016-12-27T05:00:00-05:00",
  44. updatedAt: null
  45. });
  46. }
  47. else if (id == 4) {
  48. deferred.resolve({
  49. id: id,
  50. name: "Test.2",
  51. obj: {
  52. value: 4242
  53. },
  54. createdAt: "2016-12-27T05:00:00-05:00",
  55. updatedAt: "2016-12-28T05:00:00-05:00"
  56. });
  57. }
  58. return deferred.promise;
  59. },
  60. getMultiple: function (orderBy, filter, page, perPage, luBusyGroups) {
  61. var deferred = $q.defer();
  62. deferred.resolve({
  63. count: 42,
  64. data: [{
  65. id: 1,
  66. name: "Test.",
  67. obj: {
  68. value: 4242
  69. }
  70. }, {
  71. id: 2,
  72. name: "Test.2",
  73. obj: {
  74. anotherValue: 2424
  75. }
  76. }]
  77. });
  78. return deferred.promise;
  79. },
  80. addDbo: function(data, luBusyGroups) {
  81. return this.getSingleById(1, luBusyGroups);
  82. },
  83. editSingleByIdDbo: function(id, data, luBusyGroups) {
  84. return this.getSingleById(id, luBusyGroups);
  85. },
  86. deleteDbo: function(id, luBusyGroups) {
  87. return this.getSingleById(id, luBusyGroups);
  88. }
  89. };
  90. });
  91. it('should add a dummy function to getSingleById dbo', function()
  92. {
  93. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  94. business.defaultDbo = {
  95. id: null,
  96. name: "",
  97. dummyFunction: function () {
  98. return '_' + this.name + '_';
  99. }
  100. };
  101. var id = 1;
  102. business.getSingleById(id).then(function (data) {
  103. expect(data.id).toEqual(id);
  104. expect(data.name).toEqual("Test.");
  105. expect(data.dummyFunction).not.toBeNull();
  106. expect(data.dummyFunction()).toEqual("_Test._");
  107. }, function (error) {
  108. expect(error).toBeNull();
  109. });
  110. $rootScope.$digest();
  111. });
  112. it('should test complex extend', function()
  113. {
  114. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  115. business.defaultDbo = {
  116. id: null,
  117. name: "",
  118. obj: {
  119. value: 42,
  120. anotherValue: 24
  121. }
  122. };
  123. var id = 2;
  124. business.getSingleById(id).then(function (data) {
  125. expect(data.id).toEqual(id);
  126. expect(data.name).toEqual("Test.2");
  127. expect(data.obj).not.toBeNull();
  128. expect(data.obj.value).toEqual(4242);
  129. expect(data.obj.anotherValue).toEqual(24);
  130. }, function (error) {
  131. expect(error).toBeNull();
  132. });
  133. $rootScope.$digest();
  134. });
  135. it('should add a dummy function to getSingleById dbo', function()
  136. {
  137. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  138. business.defaultDbo = {
  139. id: null,
  140. name: "",
  141. dummyFunction: function () {
  142. return '_' + this.name + '_';
  143. }
  144. };
  145. business.getMultiple(null, null, 0, 2).then(function (data) {
  146. expect(data.count).toEqual(42);
  147. expect(data.data.length).toEqual(2);
  148. var i = 0;
  149. expect(data.data[i].id).toEqual(1);
  150. expect(data.data[i].name).toEqual("Test.");
  151. expect(data.data[i].dummyFunction).not.toBeNull();
  152. expect(data.data[i].dummyFunction()).toEqual("_Test._");
  153. i = 1;
  154. expect(data.data[i].id).toEqual(2);
  155. expect(data.data[i].name).toEqual("Test.2");
  156. expect(data.data[i].dummyFunction).not.toBeNull();
  157. expect(data.data[i].dummyFunction()).toEqual("_Test.2_");
  158. }, function (error) {
  159. expect(error).toBeNull();
  160. });
  161. $rootScope.$digest();
  162. });
  163. it('should add a dummy function to addDbo dbo', function()
  164. {
  165. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  166. business.defaultDbo = {
  167. id: null,
  168. name: "",
  169. dummyFunction: function () {
  170. return '_' + this.name + '_';
  171. }
  172. };
  173. var id = 1;
  174. business.addDbo(null).then(function (data) {
  175. expect(data.id).toEqual(id);
  176. expect(data.name).toEqual("Test.");
  177. expect(data.dummyFunction).not.toBeNull();
  178. expect(data.dummyFunction()).toEqual("_Test._");
  179. }, function (error) {
  180. expect(error).toBeNull();
  181. });
  182. $rootScope.$digest();
  183. });
  184. it('should add a dummy function to editSingleByIdDbo dbo', function()
  185. {
  186. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  187. business.defaultDbo = {
  188. id: null,
  189. name: "",
  190. dummyFunction: function () {
  191. return '_' + this.name + '_';
  192. }
  193. };
  194. var id = 1;
  195. business.editSingleByIdDbo(id, null).then(function (data) {
  196. expect(data.id).toEqual(id);
  197. expect(data.name).toEqual("Test.");
  198. expect(data.dummyFunction).not.toBeNull();
  199. expect(data.dummyFunction()).toEqual("_Test._");
  200. }, function (error) {
  201. expect(error).toBeNull();
  202. });
  203. $rootScope.$digest();
  204. });
  205. it('should add a dummy function to deleteDbo dbo', function()
  206. {
  207. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  208. business.defaultDbo = {
  209. id: null,
  210. name: "",
  211. dummyFunction: function () {
  212. return '_' + this.name + '_';
  213. }
  214. };
  215. var id = 1;
  216. business.deleteDbo(id, null).then(function (data) {
  217. expect(data.id).toEqual(id);
  218. expect(data.name).toEqual("Test.");
  219. expect(data.dummyFunction).not.toBeNull();
  220. expect(data.dummyFunction()).toEqual("_Test._");
  221. }, function (error) {
  222. expect(error).toBeNull();
  223. });
  224. $rootScope.$digest();
  225. });
  226. it('should convert createdAt into Date', function()
  227. {
  228. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  229. business.defaultDbo = {
  230. id: null,
  231. name: "",
  232. createdAt: null,
  233. updatedAt: null,
  234. dummyFunction: function () {
  235. return '_' + this.name + '_';
  236. }
  237. };
  238. var id = 3;
  239. business.getSingleById(id, null).then(function (data) {
  240. expect(data.id).toEqual(id);
  241. expect(data.name).toEqual("Test.2");
  242. expect(data.dummyFunction).not.toBeNull();
  243. expect(data.dummyFunction()).toEqual("_Test.2_");
  244. expect(data.createdAt).toEqual(new Date("2016-12-27T05:00:00-05:00"));
  245. expect(data.updatedAt).toBeNull();
  246. }, function (error) {
  247. expect(error).toBeNull();
  248. });
  249. $rootScope.$digest();
  250. });
  251. it('should convert createdAt and updatedAt into Date', function()
  252. {
  253. var business = luWebApiCrudBusiness.create(fakeDataAccess);
  254. business.defaultDbo = {
  255. id: null,
  256. name: "",
  257. createdAt: null,
  258. updatedAt: null,
  259. dummyFunction: function () {
  260. return '_' + this.name + '_';
  261. }
  262. };
  263. var id = 4;
  264. business.getSingleById(id, null).then(function (data) {
  265. expect(data.id).toEqual(id);
  266. expect(data.name).toEqual("Test.2");
  267. expect(data.dummyFunction).not.toBeNull();
  268. expect(data.dummyFunction()).toEqual("_Test.2_");
  269. expect(data.createdAt).toEqual(new Date("2016-12-27T05:00:00-05:00"));
  270. expect(data.updatedAt).toEqual(new Date("2016-12-28T05:00:00-05:00"));
  271. }, function (error) {
  272. expect(error).toBeNull();
  273. });
  274. $rootScope.$digest();
  275. });
  276. });