Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * Created by robin on 12/11/16.
  3. */
  4. describe('lu-webapi-crud-dataaccess model factory', function() {
  5. var luWebApiCrudDataAccess;
  6. var fakeDataAccess;
  7. var $q;
  8. var $rootScope;
  9. beforeEach(angular.mock.module('luticate2Utils'));
  10. beforeEach(function () {
  11. module(function ($provide) {
  12. $provide.value('luWebApiDataAccess', {
  13. create: function (entry_point) {
  14. return {
  15. get: function (url, dataGet, luBusyGroups) {
  16. var deferred = $q.defer();
  17. if (url == '') {
  18. fakeDataAccess.getMultiple(dataGet.orderBy, dataGet.filter, dataGet.page, dataGet.perPage, deferred);
  19. }
  20. else {
  21. fakeDataAccess.getSingleById(url, deferred);
  22. }
  23. // deferred.resolve({
  24. // get: dataGet,
  25. // post: null,
  26. // url: entry_point + url
  27. // });
  28. return deferred.promise;
  29. },
  30. post: function (url, dataGet, dataPost, luBusyGroups) {
  31. var deferred = $q.defer();
  32. if (url == '') {
  33. fakeDataAccess.addDbo(dataPost, deferred);
  34. }
  35. else {
  36. fakeDataAccess.editSingleByIdDbo(url, dataPost, deferred);
  37. }
  38. deferred.resolve({
  39. get: dataGet,
  40. post: dataPost,
  41. url: entry_point + url
  42. });
  43. return deferred.promise;
  44. },
  45. put: function (url, dataGet, dataPost, luBusyGroups) {
  46. var deferred = $q.defer();
  47. deferred.resolve({
  48. get: dataGet,
  49. post: dataPost,
  50. url: entry_point + url
  51. });
  52. return deferred.promise;
  53. },
  54. delete: function (url, dataGet, dataPost, luBusyGroups) {
  55. var deferred = $q.defer();
  56. fakeDataAccess.deleteDbo(url, deferred);
  57. return deferred.promise;
  58. }
  59. }
  60. }
  61. });
  62. });
  63. });
  64. beforeEach(function () {
  65. fakeDataAccess = {
  66. getSingleById: function(id, deferred) {
  67. if (id == 1) {
  68. deferred.resolve({
  69. id: id,
  70. name: "Test."
  71. });
  72. }
  73. else if (id == 2) {
  74. deferred.resolve({
  75. id: id,
  76. name: "Test.2",
  77. obj: {
  78. value: 4242
  79. }
  80. });
  81. }
  82. else if (id == 3) {
  83. deferred.resolve({
  84. id: id,
  85. name: "Test.2",
  86. obj: {
  87. value: 4242
  88. },
  89. createdAt: "2016-12-27T05:00:00-05:00",
  90. updatedAt: null
  91. });
  92. }
  93. else if (id == 4) {
  94. deferred.resolve({
  95. id: id,
  96. name: "Test.2",
  97. obj: {
  98. value: 4242
  99. },
  100. createdAt: "2016-12-27T05:00:00-05:00",
  101. updatedAt: "2016-12-28T05:00:00-05:00"
  102. });
  103. }
  104. return deferred.promise;
  105. },
  106. getMultiple: function (orderBy, filter, page, perPage, deferred) {
  107. deferred.resolve({
  108. count: 42,
  109. data: [{
  110. id: 1,
  111. name: "Test.",
  112. obj: {
  113. value: 4242
  114. }
  115. }, {
  116. id: 2,
  117. name: "Test.2",
  118. obj: {
  119. anotherValue: 2424
  120. }
  121. }]
  122. });
  123. return deferred.promise;
  124. },
  125. addDbo: function(data, deferred) {
  126. return this.getSingleById(1, deferred);
  127. },
  128. editSingleByIdDbo: function(id, data, deferred) {
  129. return this.getSingleById(id, deferred);
  130. },
  131. deleteDbo: function(id, deferred) {
  132. return this.getSingleById(id, deferred);
  133. }
  134. };
  135. });
  136. beforeEach(inject(function(_luWebApiCrudDataAccess_, _$q_, _$rootScope_) {
  137. luWebApiCrudDataAccess = _luWebApiCrudDataAccess_;
  138. $q = _$q_;
  139. $rootScope = _$rootScope_;
  140. }));
  141. it('should add a dummy function to getSingleById dbo', function()
  142. {
  143. var dataAccess = luWebApiCrudDataAccess.create('');
  144. dataAccess.defaultDbo = {
  145. id: null,
  146. name: "",
  147. dummyFunction: function () {
  148. return '_' + this.name + '_';
  149. }
  150. };
  151. var id = 1;
  152. dataAccess.getSingleById(id).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 test complex extend', function()
  163. {
  164. var dataAccess = luWebApiCrudDataAccess.create('');
  165. dataAccess.defaultDbo = {
  166. id: null,
  167. name: "",
  168. obj: {
  169. value: 42,
  170. anotherValue: 24
  171. }
  172. };
  173. var id = 2;
  174. dataAccess.getSingleById(id).then(function (data) {
  175. expect(data.id).toEqual(id);
  176. expect(data.name).toEqual("Test.2");
  177. expect(data.obj).not.toBeNull();
  178. expect(data.obj.value).toEqual(4242);
  179. expect(data.obj.anotherValue).toEqual(24);
  180. }, function (error) {
  181. expect(error).toBeNull();
  182. });
  183. $rootScope.$digest();
  184. });
  185. it('should add a dummy function to getSingleById dbo', function()
  186. {
  187. var dataAccess = luWebApiCrudDataAccess.create('');
  188. dataAccess.defaultDbo = {
  189. id: null,
  190. name: "",
  191. dummyFunction: function () {
  192. return '_' + this.name + '_';
  193. }
  194. };
  195. dataAccess.getMultiple(null, null, 0, 2).then(function (data) {
  196. expect(data.count).toEqual(42);
  197. expect(data.data.length).toEqual(2);
  198. var i = 0;
  199. expect(data.data[i].id).toEqual(1);
  200. expect(data.data[i].name).toEqual("Test.");
  201. expect(data.data[i].dummyFunction).not.toBeNull();
  202. expect(data.data[i].dummyFunction()).toEqual("_Test._");
  203. i = 1;
  204. expect(data.data[i].id).toEqual(2);
  205. expect(data.data[i].name).toEqual("Test.2");
  206. expect(data.data[i].dummyFunction).not.toBeNull();
  207. expect(data.data[i].dummyFunction()).toEqual("_Test.2_");
  208. }, function (error) {
  209. expect(error).toBeNull();
  210. });
  211. $rootScope.$digest();
  212. });
  213. it('should add a dummy function to addDbo dbo', function()
  214. {
  215. var dataAccess = luWebApiCrudDataAccess.create('');
  216. dataAccess.defaultDbo = {
  217. id: null,
  218. name: "",
  219. dummyFunction: function () {
  220. return '_' + this.name + '_';
  221. }
  222. };
  223. var id = 1;
  224. dataAccess.addDbo({}).then(function (data) {
  225. expect(data.id).toEqual(id);
  226. expect(data.name).toEqual("Test.");
  227. expect(data.dummyFunction).not.toBeNull();
  228. expect(data.dummyFunction()).toEqual("_Test._");
  229. }, function (error) {
  230. expect(error).toBeNull();
  231. });
  232. $rootScope.$digest();
  233. });
  234. it('should add a dummy function to editSingleByIdDbo dbo', function()
  235. {
  236. var dataAccess = luWebApiCrudDataAccess.create('');
  237. dataAccess.defaultDbo = {
  238. id: null,
  239. name: "",
  240. dummyFunction: function () {
  241. return '_' + this.name + '_';
  242. }
  243. };
  244. var id = 1;
  245. dataAccess.editSingleByIdDbo(id, {}).then(function (data) {
  246. expect(data.id).toEqual(id);
  247. expect(data.name).toEqual("Test.");
  248. expect(data.dummyFunction).not.toBeNull();
  249. expect(data.dummyFunction()).toEqual("_Test._");
  250. }, function (error) {
  251. expect(error).toBeNull();
  252. });
  253. $rootScope.$digest();
  254. });
  255. it('should add a dummy function to deleteDbo dbo', function()
  256. {
  257. var dataAccess = luWebApiCrudDataAccess.create('');
  258. dataAccess.defaultDbo = {
  259. id: null,
  260. name: "",
  261. dummyFunction: function () {
  262. return '_' + this.name + '_';
  263. }
  264. };
  265. var id = 1;
  266. dataAccess.deleteDbo(id, null).then(function (data) {
  267. expect(data.id).toEqual(id);
  268. expect(data.name).toEqual("Test.");
  269. expect(data.dummyFunction).not.toBeNull();
  270. expect(data.dummyFunction()).toEqual("_Test._");
  271. }, function (error) {
  272. expect(error).toBeNull();
  273. });
  274. $rootScope.$digest();
  275. });
  276. it('should convert createdAt into Date', function()
  277. {
  278. var dataAccess = luWebApiCrudDataAccess.create('');
  279. dataAccess.defaultDbo = {
  280. id: null,
  281. name: "",
  282. createdAt: null,
  283. updatedAt: null,
  284. dummyFunction: function () {
  285. return '_' + this.name + '_';
  286. }
  287. };
  288. var id = 3;
  289. dataAccess.getSingleById(id, null).then(function (data) {
  290. expect(data.id).toEqual(id);
  291. expect(data.name).toEqual("Test.2");
  292. expect(data.dummyFunction).not.toBeNull();
  293. expect(data.dummyFunction()).toEqual("_Test.2_");
  294. expect(data.createdAt).toEqual(new Date("2016-12-27T05:00:00-05:00"));
  295. expect(data.updatedAt).toBeNull();
  296. }, function (error) {
  297. expect(error).toBeNull();
  298. });
  299. $rootScope.$digest();
  300. });
  301. it('should convert createdAt and updatedAt into Date', function()
  302. {
  303. var dataAccess = luWebApiCrudDataAccess.create('');
  304. dataAccess.defaultDbo = {
  305. id: null,
  306. name: "",
  307. createdAt: null,
  308. updatedAt: null,
  309. dummyFunction: function () {
  310. return '_' + this.name + '_';
  311. }
  312. };
  313. var id = 4;
  314. dataAccess.getSingleById(id, null).then(function (data) {
  315. expect(data.id).toEqual(id);
  316. expect(data.name).toEqual("Test.2");
  317. expect(data.dummyFunction).not.toBeNull();
  318. expect(data.dummyFunction()).toEqual("_Test.2_");
  319. expect(data.createdAt).toEqual(new Date("2016-12-27T05:00:00-05:00"));
  320. expect(data.updatedAt).toEqual(new Date("2016-12-28T05:00:00-05:00"));
  321. }, function (error) {
  322. expect(error).toBeNull();
  323. });
  324. $rootScope.$digest();
  325. });
  326. });