Application Android et IOS pour l'amicale des élèves
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.

EquipmentBooking.test.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import React from 'react';
  2. import * as EquipmentBooking from "../../src/utils/EquipmentBooking";
  3. import i18n from "i18n-js";
  4. test('getISODate', () => {
  5. let date = new Date("2020-03-05 12:00");
  6. expect(EquipmentBooking.getISODate(date)).toBe("2020-03-05");
  7. date = new Date("2020-03-05");
  8. expect(EquipmentBooking.getISODate(date)).toBe("2020-03-05");
  9. date = new Date("2020-03-05 00:00"); // Treated as local time
  10. expect(EquipmentBooking.getISODate(date)).toBe("2020-03-04"); // Treated as UTC
  11. });
  12. test('getCurrentDay', () => {
  13. jest.spyOn(Date, 'now')
  14. .mockImplementation(() =>
  15. new Date('2020-01-14 14:50:35').getTime()
  16. );
  17. expect(EquipmentBooking.getCurrentDay().getTime()).toBe(new Date("2020-01-14").getTime());
  18. });
  19. test('isEquipmentAvailable', () => {
  20. jest.spyOn(Date, 'now')
  21. .mockImplementation(() =>
  22. new Date('2020-07-09').getTime()
  23. );
  24. let testDevice = {
  25. id: 1,
  26. name: "Petit barbecue",
  27. caution: 100,
  28. booked_at: [{begin: "2020-07-07", end: "2020-07-10"}]
  29. };
  30. expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse();
  31. testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-09"}];
  32. expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse();
  33. testDevice.booked_at = [{begin: "2020-07-09", end: "2020-07-10"}];
  34. expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse();
  35. testDevice.booked_at = [
  36. {begin: "2020-07-07", end: "2020-07-8"},
  37. {begin: "2020-07-10", end: "2020-07-12"},
  38. ];
  39. expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeTrue();
  40. });
  41. test('getFirstEquipmentAvailability', () => {
  42. jest.spyOn(Date, 'now')
  43. .mockImplementation(() =>
  44. new Date('2020-07-09').getTime()
  45. );
  46. let testDevice = {
  47. id: 1,
  48. name: "Petit barbecue",
  49. caution: 100,
  50. booked_at: [{begin: "2020-07-07", end: "2020-07-10"}]
  51. };
  52. expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-11").getTime());
  53. testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-09"}];
  54. expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-10").getTime());
  55. testDevice.booked_at = [
  56. {begin: "2020-07-07", end: "2020-07-09"},
  57. {begin: "2020-07-10", end: "2020-07-16"},
  58. ];
  59. expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-17").getTime());
  60. testDevice.booked_at = [
  61. {begin: "2020-07-07", end: "2020-07-09"},
  62. {begin: "2020-07-10", end: "2020-07-12"},
  63. {begin: "2020-07-14", end: "2020-07-16"},
  64. ];
  65. expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-13").getTime());
  66. });
  67. test('getRelativeDateString', () => {
  68. jest.spyOn(Date, 'now')
  69. .mockImplementation(() =>
  70. new Date('2020-07-09').getTime()
  71. );
  72. jest.spyOn(i18n, 't')
  73. .mockImplementation((translationString: string) => {
  74. const prefix = "screens.equipment.";
  75. if (translationString === prefix + "otherYear")
  76. return "0";
  77. else if (translationString === prefix + "otherMonth")
  78. return "1";
  79. else if (translationString === prefix + "thisMonth")
  80. return "2";
  81. else if (translationString === prefix + "tomorrow")
  82. return "3";
  83. else if (translationString === prefix + "today")
  84. return "4";
  85. else
  86. return null;
  87. }
  88. );
  89. expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-09"))).toBe("4");
  90. expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-10"))).toBe("3");
  91. expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-11"))).toBe("2");
  92. expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-30"))).toBe("2");
  93. expect(EquipmentBooking.getRelativeDateString(new Date("2020-08-30"))).toBe("1");
  94. expect(EquipmentBooking.getRelativeDateString(new Date("2020-11-10"))).toBe("1");
  95. expect(EquipmentBooking.getRelativeDateString(new Date("2021-11-10"))).toBe("0");
  96. });
  97. test('getValidRange', () => {
  98. let testDevice = {
  99. id: 1,
  100. name: "Petit barbecue",
  101. caution: 100,
  102. booked_at: [{begin: "2020-07-07", end: "2020-07-10"}]
  103. };
  104. let start = new Date("2020-07-11");
  105. let end = new Date("2020-07-15");
  106. let result = [
  107. "2020-07-11",
  108. "2020-07-12",
  109. "2020-07-13",
  110. "2020-07-14",
  111. "2020-07-15",
  112. ];
  113. expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result);
  114. testDevice.booked_at = [
  115. {begin: "2020-07-07", end: "2020-07-10"},
  116. {begin: "2020-07-13", end: "2020-07-15"},
  117. ];
  118. result = [
  119. "2020-07-11",
  120. "2020-07-12",
  121. ];
  122. expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result);
  123. testDevice.booked_at = [{begin: "2020-07-12", end: "2020-07-13"}];
  124. result = ["2020-07-11"];
  125. expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result);
  126. testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-12"},];
  127. result = [
  128. "2020-07-13",
  129. "2020-07-14",
  130. "2020-07-15",
  131. ];
  132. expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result);
  133. start = new Date("2020-07-14");
  134. end = new Date("2020-07-14");
  135. result = [
  136. "2020-07-14",
  137. ];
  138. expect(EquipmentBooking.getValidRange(start, start, testDevice)).toStrictEqual(result);
  139. expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result);
  140. expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual(result);
  141. start = new Date("2020-07-14");
  142. end = new Date("2020-07-17");
  143. result = [
  144. "2020-07-14",
  145. "2020-07-15",
  146. "2020-07-16",
  147. "2020-07-17",
  148. ];
  149. expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual(result);
  150. testDevice.booked_at = [{begin: "2020-07-17", end: "2020-07-17"}];
  151. result = [
  152. "2020-07-14",
  153. "2020-07-15",
  154. "2020-07-16",
  155. ];
  156. expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result);
  157. testDevice.booked_at = [
  158. {begin: "2020-07-12", end: "2020-07-13"},
  159. {begin: "2020-07-15", end: "2020-07-20"},
  160. ];
  161. start = new Date("2020-07-11");
  162. end = new Date("2020-07-23");
  163. result = [
  164. "2020-07-21",
  165. "2020-07-22",
  166. "2020-07-23",
  167. ];
  168. expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result);
  169. });
  170. test('generateMarkedDates', () => {
  171. let theme = {
  172. colors: {
  173. primary: "primary",
  174. danger: "primary",
  175. textDisabled: "primary",
  176. }
  177. }
  178. let testDevice = {
  179. id: 1,
  180. name: "Petit barbecue",
  181. caution: 100,
  182. booked_at: [{begin: "2020-07-07", end: "2020-07-10"}]
  183. };
  184. let start = new Date("2020-07-11");
  185. let end = new Date("2020-07-13");
  186. let range = EquipmentBooking.getValidRange(start, end, testDevice);
  187. let result = {
  188. "2020-07-11": {
  189. startingDay: true,
  190. endingDay: false,
  191. color: theme.colors.primary
  192. },
  193. "2020-07-12": {
  194. startingDay: false,
  195. endingDay: false,
  196. color: theme.colors.danger
  197. },
  198. "2020-07-13": {
  199. startingDay: false,
  200. endingDay: true,
  201. color: theme.colors.primary
  202. },
  203. };
  204. expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result);
  205. result = {
  206. "2020-07-11": {
  207. startingDay: true,
  208. endingDay: false,
  209. color: theme.colors.textDisabled
  210. },
  211. "2020-07-12": {
  212. startingDay: false,
  213. endingDay: false,
  214. color: theme.colors.textDisabled
  215. },
  216. "2020-07-13": {
  217. startingDay: false,
  218. endingDay: true,
  219. color: theme.colors.textDisabled
  220. },
  221. };
  222. expect(EquipmentBooking.generateMarkedDates(false, theme, range)).toStrictEqual(result);
  223. result = {
  224. "2020-07-11": {
  225. startingDay: true,
  226. endingDay: false,
  227. color: theme.colors.textDisabled
  228. },
  229. "2020-07-12": {
  230. startingDay: false,
  231. endingDay: false,
  232. color: theme.colors.textDisabled
  233. },
  234. "2020-07-13": {
  235. startingDay: false,
  236. endingDay: true,
  237. color: theme.colors.textDisabled
  238. },
  239. };
  240. range = EquipmentBooking.getValidRange(end, start, testDevice);
  241. expect(EquipmentBooking.generateMarkedDates(false, theme, range)).toStrictEqual(result);
  242. testDevice.booked_at = [{begin: "2020-07-13", end: "2020-07-15"},];
  243. result = {
  244. "2020-07-11": {
  245. startingDay: true,
  246. endingDay: false,
  247. color: theme.colors.primary
  248. },
  249. "2020-07-12": {
  250. startingDay: false,
  251. endingDay: true,
  252. color: theme.colors.primary
  253. },
  254. };
  255. range = EquipmentBooking.getValidRange(start, end, testDevice);
  256. expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result);
  257. testDevice.booked_at = [{begin: "2020-07-12", end: "2020-07-13"},];
  258. result = {
  259. "2020-07-11": {
  260. startingDay: true,
  261. endingDay: true,
  262. color: theme.colors.primary
  263. },
  264. };
  265. range = EquipmentBooking.getValidRange(start, end, testDevice);
  266. expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result);
  267. testDevice.booked_at = [
  268. {begin: "2020-07-12", end: "2020-07-13"},
  269. {begin: "2020-07-15", end: "2020-07-20"},
  270. ];
  271. start = new Date("2020-07-11");
  272. end = new Date("2020-07-23");
  273. result = {
  274. "2020-07-11": {
  275. startingDay: true,
  276. endingDay: true,
  277. color: theme.colors.primary
  278. },
  279. };
  280. range = EquipmentBooking.getValidRange(start, end, testDevice);
  281. expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result);
  282. result = {
  283. "2020-07-21": {
  284. startingDay: true,
  285. endingDay: false,
  286. color: theme.colors.primary
  287. },
  288. "2020-07-22": {
  289. startingDay: false,
  290. endingDay: false,
  291. color: theme.colors.danger
  292. },
  293. "2020-07-23": {
  294. startingDay: false,
  295. endingDay: true,
  296. color: theme.colors.primary
  297. },
  298. };
  299. range = EquipmentBooking.getValidRange(end, start, testDevice);
  300. expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result);
  301. });