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 9.9KB

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