PaginationWrapper.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { EmbedBuilder } from "discord.js";
  2. import { PaginationWrapper,
  3. ButtonWrapper,
  4. Constants } from "../../src/Paginations";
  5. describe("PaginationWrapper: class that wrapps pagination functionality.", () => {
  6. test("should correctly set pagination life time if it meets the conditions.", () => {
  7. const pagination = new PaginationWrapper();
  8. pagination.setTime(20 * 1000);
  9. expect(pagination.time).toBe(20 * 1000);
  10. expect(() => {
  11. pagination.setTime(500);
  12. }).toThrow();
  13. expect(() => {
  14. pagination.setTime(120 * 60 * 1000);
  15. }).toThrow();
  16. expect(() => {
  17. pagination.setTime(-500);
  18. }).toThrow();
  19. pagination.setTime(120 * 60 * 1000, true);
  20. expect(pagination.time).toBe(120 * 60 * 1000);
  21. });
  22. test("should correctly set afterSendingAction.", () => {
  23. const afterSendingAction = () => null;
  24. const pagination = new PaginationWrapper()
  25. .setAfterSendingAction(afterSendingAction);
  26. expect(pagination.afterSendingAction).toStrictEqual(afterSendingAction);
  27. });
  28. test("should correctly set beforeStopAction.", () => {
  29. const beforeStopAction = () => null;
  30. const pagination = new PaginationWrapper()
  31. .setBeforeStopAction(beforeStopAction);
  32. expect(pagination.beforeStopAction).toStrictEqual(beforeStopAction);
  33. });
  34. test("should correctly set afterStopAction.", () => {
  35. const afterStopAction = () => null;
  36. const pagination = new PaginationWrapper()
  37. .setAfterStopAction(afterStopAction);
  38. expect(pagination.afterStopAction).toStrictEqual(afterStopAction);
  39. });
  40. test("should correctly find button by customId.", () => {
  41. const button = new ButtonWrapper()
  42. .setAction(() => false)
  43. .setData({custom_id: "custom_Id"})
  44. .setSwitch(() => false);
  45. const pagination = new PaginationWrapper()
  46. .setButtons(button);
  47. expect(pagination.getButtonByCustomId("custom_Id")).toStrictEqual(button);
  48. expect(pagination.getButtonByCustomId("incorrect_Id")).toBeUndefined();
  49. });
  50. test("should correctly set embeds if they meet conditions.", () => {
  51. const embeds =
  52. [
  53. new EmbedBuilder()
  54. .setDescription("Totally normal description."),
  55. new EmbedBuilder(),
  56. ];
  57. const pagination = new PaginationWrapper();
  58. expect(() => {
  59. pagination.setEmbeds(embeds);
  60. }).toThrow();
  61. embeds[1]
  62. .setDescription(new Array(4096 + 1).join("A"))
  63. .setTitle(new Array(256 + 1).join("A"))
  64. .setFields(
  65. {
  66. name: new Array(128 + 1).join("A"),
  67. value: new Array(1024 + 1).join("A"),
  68. },
  69. {
  70. name: new Array(128 + 1).join("A"),
  71. value: new Array(1024 + 1).join("A"),
  72. },
  73. );
  74. expect(() => {
  75. pagination.setEmbeds(embeds);
  76. }).toThrow();
  77. expect(pagination.setEmbeds([embeds[0]]).embeds).toStrictEqual([embeds[0].data]);
  78. });
  79. test("should correctly set buttons if they meet the conditions.", () =>
  80. {
  81. const buttons = new Array(Constants.DiscordMaxButtonsPerRow * Constants.DiscordMaxRowsPerMessage + 1).fill(new ButtonWrapper());
  82. const pagination = new PaginationWrapper();
  83. expect(() => {
  84. pagination.setButtons(buttons);
  85. }).toThrow(`[DJS-Button-Pages]: There can not be more than ${Constants.DiscordMaxButtonsPerRow * Constants.DiscordMaxRowsPerMessage}.`);
  86. const button = new ButtonWrapper();
  87. expect(() => {
  88. pagination.setButtons(button);
  89. }).toThrow();
  90. const buttons2 = new Array(2).fill(
  91. new ButtonWrapper()
  92. .setData({custom_id: "custom_Id"})
  93. .setAction(() => false)
  94. .setSwitch(() => false)
  95. );
  96. expect(() => {
  97. pagination.setButtons(buttons2);
  98. }).toThrow();
  99. const normalButton = new ButtonWrapper()
  100. .setData({custom_id: "custom_Id"})
  101. .setAction(() => false)
  102. .setSwitch(() => false)
  103. expect(pagination.setButtons(normalButton).buttons).toStrictEqual([normalButton]);
  104. });
  105. test("should correctly set filterOptions if they meet the conditions.", () => {
  106. const pagination = new PaginationWrapper();
  107. const exampleOptions = {removeButtonsOnEnd: true};
  108. expect(pagination.setFilterOptions(exampleOptions).filterOptions).toStrictEqual(exampleOptions);
  109. expect(() => {
  110. pagination.setFilterOptions({maxIdleTime: -1});
  111. }).toThrow();
  112. expect(() => {
  113. pagination.setFilterOptions({maxIdleTime: 2.5});
  114. }).toThrow();
  115. expect(() => {
  116. pagination.setFilterOptions({maxInteractions: -1});
  117. }).toThrow();
  118. expect(() => {
  119. pagination.setFilterOptions({maxInteractions: 2.5});
  120. }).toThrow();
  121. expect(() => {
  122. pagination.setFilterOptions({maxUsers: -1});
  123. }).toThrow();
  124. expect(() => {
  125. pagination.setFilterOptions({maxUsers: 2.5});
  126. }).toThrow();
  127. expect(() => {
  128. pagination.setFilterOptions({noAccessReplyContent: ""});
  129. }).toThrow();
  130. });
  131. test("should correctly set allowed users.", () => {
  132. const allowedUsers = ["497808224817381377"];
  133. const pagination = new PaginationWrapper();
  134. expect(pagination.setAllowedUsers(allowedUsers).filterOptions.allowedUsers).toStrictEqual(allowedUsers);
  135. });
  136. test("should correctly add allowed users.", () => {
  137. const allowedUser = "497808224817381377";
  138. const pagination = new PaginationWrapper();
  139. expect(pagination.addAllowedUsers(allowedUser).filterOptions.allowedUsers).toStrictEqual([allowedUser]);
  140. });
  141. test("should correctly re-create from another instance of class.", () => {
  142. const previousPagination = {
  143. embeds:
  144. [
  145. new EmbedBuilder()
  146. .setDescription("Description.")
  147. .data,
  148. new EmbedBuilder()
  149. .setDescription("Description.")
  150. .data
  151. ],
  152. time: 10 * 1000,
  153. filterOptions: {},
  154. buttons:
  155. [
  156. new ButtonWrapper({custom_id: "customId"})
  157. .setAction(() => false)
  158. .setSwitch(() => false)
  159. ],
  160. };
  161. const pagination = new PaginationWrapper(previousPagination);
  162. expect(pagination.toJSON()).toStrictEqual(previousPagination);
  163. });
  164. });