PaginationData.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { MessageButton, MessageEmbed } from "discord.js";
  2. import PaginationData from "../../../src/Classes/Paginations/Basic/PaginationData";
  3. import Constants from "../../../src/Constants";
  4. import FirstPageButton from "../../../src/Classes/Buttons/FirstPageButton";
  5. import ButtonData from "../../../src/Classes/Buttons/Basic/ButtonData";
  6. describe("Data that is necessary", () => {
  7. test("should initialize with default values.", () => {
  8. const pagination = new PaginationData();
  9. expect(pagination.time).toBeNull();
  10. expect(pagination.onStop).toBeNull();
  11. expect(pagination.isActive).toBeFalsy();
  12. expect(pagination.filterOptions).toStrictEqual({singleUserAccess: true, noAccessReply: true, noAccessReplyContent: "You're disallowed to use this very pagination!"});
  13. expect(pagination.embeds).toBeNull();
  14. expect(pagination.currentPage).toBe(0);
  15. expect(pagination.collectorOptions).toBeNull();
  16. expect(pagination.collector).toBeNull();
  17. expect(pagination.buttons).toBeNull();
  18. expect(pagination.afterSending).toBeNull();
  19. });
  20. test("should set afterSending action.", () => {
  21. const afterSending = () => {
  22. return;
  23. },
  24. pagination = new PaginationData();
  25. expect(pagination.setAfterSendingAction(afterSending).afterSending).toBe(afterSending);
  26. });
  27. test("should set onStop action.", () => {
  28. const onStop = () => {
  29. return;
  30. },
  31. pagination = new PaginationData();
  32. expect(pagination.setOnStopAction(onStop).onStop).toBe(onStop);
  33. });
  34. test("setTime should throw error if supplied number is not natural.", () => {
  35. const pagination = new PaginationData();
  36. expect(() => pagination.setTime(5.5)).toThrow("Time should be natural.");
  37. expect(() => pagination.setTime(-2)).toThrow("Time should be natural.");
  38. });
  39. test("setTime should throw error if supplied number didn't bypass lowest library limit.", () => {
  40. const pagination = new PaginationData();
  41. expect(() => pagination.setTime(Constants.LIBRARY_MIN_PAGES_LIFE_TIME - 1)).toThrow("Pagination should exist at least one second.");
  42. });
  43. test("setTime should throw error if supplied number didn't bypass highest library limit.", () => {
  44. const pagination = new PaginationData();
  45. expect(() => pagination.setTime(Constants.LIBRARY_MAX_PAGES_LIFE_TIME + 1)).toThrow("Pagination total life time should be no more than an hour due to optimization. If you want to bypass the limit, pass true as the second argument to this method.");
  46. });
  47. test("setTime should accept number that is greater than highest library limit if bypassLibraryLimits is true.", () => {
  48. const pagination = new PaginationData();
  49. expect(pagination.setTime(Constants.LIBRARY_MAX_PAGES_LIFE_TIME + 1, true).time).toBe(Constants.LIBRARY_MAX_PAGES_LIFE_TIME + 1);
  50. });
  51. test("setTime should accept normal numbers.", () => {
  52. const pagination = new PaginationData();
  53. expect(pagination.setTime(Constants.LIBRARY_MIN_PAGES_LIFE_TIME + 1).time).toBe(Constants.LIBRARY_MIN_PAGES_LIFE_TIME + 1);
  54. });
  55. test("insertEmbeds should throw error if some of the embeds are empty.", () => {
  56. const pagination = new PaginationData(),
  57. embeds = [new MessageEmbed().setDescription("description"), new MessageEmbed()];
  58. expect(() => pagination.insertEmbeds(embeds)).toThrow("No embeds from the array can be empty.");
  59. });
  60. test("insertEmbeds should throw error if some of the embeds are longer than Discord's limits.", () => {
  61. const pagination = new PaginationData(),
  62. text = new Array(Constants.DISCORD_MAX_EMBED_LENGTH + 2).join("a"),
  63. embeds = [new MessageEmbed().setDescription("description"), new MessageEmbed().setDescription(text)];
  64. expect(() => pagination.insertEmbeds(embeds)).toThrow(`No embeds from the array can be longer than ${Constants.DISCORD_MAX_EMBED_LENGTH}.`);
  65. });
  66. test("insertEmbeds should throw error if index is not natural.", () => {
  67. const pagination = new PaginationData(),
  68. embeds = [new MessageEmbed().setDescription("description"), new MessageEmbed().setDescription("description")];
  69. expect(() => pagination.insertEmbeds(embeds, -1)).toThrow("Index should be natural.");
  70. expect(() => pagination.insertEmbeds(embeds, 2.5)).toThrow("Index should be natural.");
  71. });
  72. test("insertEmbeds should set array with old embeds and new.", () => {
  73. const pagination = new PaginationData(),
  74. embeds = [new MessageEmbed().setDescription("description"), new MessageEmbed().setDescription("description")];
  75. expect(pagination.insertEmbeds(embeds).embeds).toStrictEqual(embeds);
  76. const anotherEmbed = new MessageEmbed().setDescription("another description");
  77. expect(pagination.insertEmbeds(anotherEmbed).embeds).toStrictEqual([...embeds, anotherEmbed]);
  78. expect(pagination.insertEmbeds(anotherEmbed, 0).embeds).toStrictEqual([anotherEmbed, ...embeds, anotherEmbed]);
  79. });
  80. test("removeEmbeds should throw error if index is not natural.", () => {
  81. const pagination = new PaginationData();
  82. expect(() => pagination.removeEmbeds(-1)).toThrow("Index should be natural.");
  83. expect(() => pagination.removeEmbeds(2.5)).toThrow("Index should be natural.");
  84. });
  85. test("removeEmbeds should throw error if count is not natural or equals to zero.", () => {
  86. const pagination = new PaginationData();
  87. expect(() => pagination.removeEmbeds(2, -1)).toThrow("Count should be natural and greater than zero.");
  88. expect(() => pagination.removeEmbeds(2, 2.5)).toThrow("Count should be natural and greater than zero.");
  89. expect(() => pagination.removeEmbeds(2, 0)).toThrow("Count should be natural and greater than zero.");
  90. });
  91. test("removeEmbeds should remove embeds with specified parameters.", () => {
  92. const pagination = new PaginationData(),
  93. embeds = [new MessageEmbed().setDescription("description 1"), new MessageEmbed().setDescription("description 2"), new MessageEmbed().setDescription("description 3"), new MessageEmbed().setDescription("description 4")];
  94. pagination.insertEmbeds(embeds);
  95. expect(pagination.removeEmbeds(0, 4).embeds).toBeNull();
  96. pagination.insertEmbeds(embeds);
  97. expect(pagination.removeEmbeds(0).embeds).toStrictEqual([new MessageEmbed().setDescription("description 2"), new MessageEmbed().setDescription("description 3"), new MessageEmbed().setDescription("description 4")]);
  98. });
  99. test("setEmbeds should throw error if some of the embeds are empty.", () => {
  100. const pagination = new PaginationData(),
  101. embeds = [new MessageEmbed().setDescription("description"), new MessageEmbed()];
  102. expect(() => pagination.setEmbeds(embeds)).toThrow("No embeds from the array can be empty.");
  103. });
  104. test("setEmbeds should throw error if some of the embeds are longer than Discord's limits.", () => {
  105. const pagination = new PaginationData(),
  106. text = new Array(Constants.DISCORD_MAX_EMBED_LENGTH + 2).join("a"),
  107. embeds = [new MessageEmbed().setDescription("description"), new MessageEmbed().setDescription(text)];
  108. expect(() => pagination.setEmbeds(embeds)).toThrow(`No embeds from the array can be longer than ${Constants.DISCORD_MAX_EMBED_LENGTH}.`);
  109. });
  110. test("setEmbeds should override previous embeds.", () => {
  111. const pagination = new PaginationData(),
  112. embeds = [new MessageEmbed().setDescription("description"), new MessageEmbed().setDescription("description")];
  113. expect(pagination.setEmbeds(embeds).embeds).toBe(embeds);
  114. const anotherEmbed = new MessageEmbed().setDescription("another description");
  115. expect(pagination.setEmbeds(anotherEmbed).embeds).toStrictEqual([anotherEmbed]);
  116. });
  117. test("setButtons should throw error if there are more buttons than Discord's Limit.", () => {
  118. const pagination = new PaginationData(),
  119. buttons:Array<ButtonData> = new Array(Constants.DISCORD_MAX_BUTTONS_PER_ROW * Constants.DISCORD_MAX_ROWS_PER_MESSAGE + 1).fill(new FirstPageButton());
  120. expect(() => pagination.setButtons(buttons)).toThrow(`There can not be more than ${Constants.DISCORD_MAX_BUTTONS_PER_ROW * Constants.DISCORD_MAX_ROWS_PER_MESSAGE} buttons per message because of Discord's limit.`);
  121. });
  122. test("setButtons should throw error if there are buttons with similar customIds.", () => {
  123. const pagination = new PaginationData(),
  124. buttons = [new FirstPageButton().setStyle(new MessageButton().setCustomId("testid").setLabel("firstLabel").setStyle("DANGER")), new FirstPageButton().setStyle(new MessageButton().setCustomId("testid").setLabel("secondLabel").setStyle("PRIMARY"))];
  125. expect(() => pagination.setButtons(buttons)).toThrow("Buttons can not have similar customIds.");
  126. });
  127. test("setButtons should override previous buttons.", () => {
  128. const pagination = new PaginationData(),
  129. buttons = [new FirstPageButton().setStyle(new MessageButton().setCustomId("testid").setLabel("firstLabel").setStyle("DANGER")), new FirstPageButton().setStyle(new MessageButton().setCustomId("testid2").setLabel("secondLabel").setStyle("PRIMARY"))];
  130. expect(pagination.setButtons(buttons).buttons).toBe(buttons);
  131. const anotherButton = new FirstPageButton().setStyle(new MessageButton().setCustomId("testid3").setLabel("thirdLabel").setStyle("DANGER"));
  132. expect(pagination.setButtons(anotherButton).buttons).toStrictEqual([anotherButton]);
  133. });
  134. test("getButtonByCustomId should return undefined if there are no buttons with this customId.", () => {
  135. const pagination = new PaginationData();
  136. expect(pagination.getButtonByCustomId("undefined")).toBeUndefined();
  137. });
  138. test("getButtonByCustomId should return ButtonData if there is button with this customId.", () => {
  139. const pagination = new PaginationData(),
  140. button = new FirstPageButton().setStyle(new MessageButton().setCustomId("testid").setLabel("label").setStyle("DANGER"));
  141. pagination.setButtons(button);
  142. expect(pagination.getButtonByCustomId("testid")).toBe(button);
  143. });
  144. test("setFilterOptions should throw error if noAccessReplyContent is too short.", () => {
  145. const pagination = new PaginationData(),
  146. filterOptions = {noAccessReplyContent: ""};
  147. expect(() => pagination.setFilterOptions(filterOptions)).toThrow("Reply should be longer than zero symbols.");
  148. });
  149. test("setFilterOptions should set options if everything is fine.", () => {
  150. const pagination = new PaginationData(),
  151. filterOptions = {singleUserAccess: true, noAccessReply: true, noAccessReplyContent: "You're disallowed to use this very pagination!"};
  152. expect(pagination.setFilterOptions(filterOptions).filterOptions).toBe(filterOptions);
  153. });
  154. test("setCollectorOptions should throw error if maxIdleTime is not a natural number.", () => {
  155. const pagination = new PaginationData(),
  156. firstCollectorOptions = {maxIdleTime: -2},
  157. secondCollectorOptions = {maxIdleTime: 2.5};
  158. expect(() => pagination.setCollectorOptions(firstCollectorOptions)).toThrow("Max collector's idle time should be a natural number!");
  159. expect(() => pagination.setCollectorOptions(secondCollectorOptions)).toThrow("Max collector's idle time should be a natural number!");
  160. });
  161. test("setCollectorOptions should throw error if maxInteractions is not a natural number.", () => {
  162. const pagination = new PaginationData(),
  163. firstCollectorOptions = {maxInteractions: -2},
  164. secondCollectorOptions = {maxInteractions: 2.5};
  165. expect(() => pagination.setCollectorOptions(firstCollectorOptions)).toThrow("Max number of interactions should be a natural number!");
  166. expect(() => pagination.setCollectorOptions(secondCollectorOptions)).toThrow("Max number of interactions should be a natural number!");
  167. });
  168. test("setCollectorOptions should throw error if maxUsers is not a natural number.", () => {
  169. const pagination = new PaginationData(),
  170. firstCollectorOptions = {maxUsers: -2},
  171. secondCollectorOptions = {maxUsers: 2.5};
  172. expect(() => pagination.setCollectorOptions(firstCollectorOptions)).toThrow("Max number of users should be a natural number!");
  173. expect(() => pagination.setCollectorOptions(secondCollectorOptions)).toThrow("Max number of users should be a natural number!");
  174. });
  175. test("setCollectorOptions should set collectorOptions if everything is fine.", () => {
  176. const pagination = new PaginationData(),
  177. collectorOptions = {maxIdleTime: 1000, maxInteractions: 25, maxUsers: 10};
  178. expect(pagination.setCollectorOptions(collectorOptions).collectorOptions).toBe(collectorOptions);
  179. });
  180. test("should initialize from another class.", () => {
  181. const previousPagination = new PaginationData()
  182. .setFilterOptions({singleUserAccess: true, noAccessReply: true, noAccessReplyContent: "Some cool reply!"})
  183. .setCollectorOptions({maxIdleTime: 20, maxInteractions: 20, maxUsers: 15})
  184. .setOnStopAction(() => 1)
  185. .setAfterSendingAction(() => 2)
  186. .setTime(20000)
  187. .setEmbeds(new MessageEmbed().setDescription("some cool description"))
  188. .setButtons(new FirstPageButton().setStyle(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER"))),
  189. { filterOptions, collectorOptions, onStop, afterSending, time, embeds, buttons } = previousPagination,
  190. newPagination = new PaginationData(previousPagination);
  191. expect(newPagination.afterSending).toBe(afterSending);
  192. expect(newPagination.buttons).toBe(buttons);
  193. expect(newPagination.collectorOptions).toBe(collectorOptions);
  194. expect(newPagination.embeds).toBe(embeds);
  195. expect(newPagination.filterOptions).toBe(filterOptions);
  196. expect(newPagination.onStop).toBe(onStop)
  197. expect(newPagination.time).toBe(time);
  198. });
  199. });