PaginationSent.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { ButtonInteraction,
  2. ButtonStyle,
  3. EmbedBuilder,
  4. InteractionCollector,
  5. Message } from "discord.js";
  6. import { ButtonWrapper,
  7. PaginationSent,
  8. PaginationState } from "../../src/Paginations";
  9. describe("PaginationSent: class that manages pagination after it was sent.", () => {
  10. test("should correctly set initial data.", () => {
  11. const data =
  12. {
  13. embeds:
  14. [
  15. new EmbedBuilder()
  16. .data
  17. ],
  18. time: 500,
  19. filterOptions: {},
  20. buttons:
  21. [
  22. new ButtonWrapper()
  23. ],
  24. },
  25. message = ({} as unknown) as Message,
  26. pageNumber = 2;
  27. const pagination = new PaginationSent(data, message, pageNumber);
  28. expect(pagination.data).toStrictEqual(data);
  29. expect(pagination.attachedTo).toStrictEqual(message);
  30. expect(pagination.page).toBe(pageNumber);
  31. expect(pagination.state).toBe(PaginationState.NotReady);
  32. expect(pagination.isActive).toBeFalsy();
  33. });
  34. test("should correctly find button by customId.", () => {
  35. const button = new ButtonWrapper()
  36. .setData({custom_id: "custom_Id"}),
  37. data =
  38. {
  39. embeds:
  40. [
  41. new EmbedBuilder()
  42. .data
  43. ],
  44. time: 500,
  45. filterOptions: {},
  46. buttons:
  47. [
  48. button,
  49. ],
  50. },
  51. message = ({} as unknown) as Message;
  52. const pagination = new PaginationSent(data, message);
  53. expect(pagination.getButtonByCustomId("custom_Id")).toStrictEqual(button);
  54. expect(pagination.getButtonByCustomId("incorrect_Id")).toBeUndefined();
  55. });
  56. test("should correctly initialize with message.", async () => {
  57. const data =
  58. {
  59. embeds:
  60. [
  61. new EmbedBuilder()
  62. .data
  63. ],
  64. time: 500,
  65. filterOptions: {},
  66. buttons:
  67. [
  68. new ButtonWrapper()
  69. .setData({custom_id: "customId", style: ButtonStyle.Primary, label: "Custom Id."})
  70. .setAction(() => false)
  71. .setSwitch(() => false)
  72. ],
  73. },
  74. attachEventMock = jest.fn(),
  75. attachOneEventMock = jest.fn(),
  76. message = ({createMessageComponentCollector: () => ({on: attachEventMock, once: attachOneEventMock} as unknown) as InteractionCollector<ButtonInteraction>} as unknown) as Message;
  77. Object.setPrototypeOf(message, Message.prototype);
  78. const pagination = new PaginationSent(data, message);
  79. await pagination.init();
  80. expect(async () => {
  81. await pagination.init();
  82. }).rejects.toThrow();
  83. });
  84. test("should correctly initialize with interaction.", async () => {
  85. const data =
  86. {
  87. embeds:
  88. [
  89. new EmbedBuilder()
  90. .data
  91. ],
  92. time: 500,
  93. filterOptions: {},
  94. buttons:
  95. [
  96. new ButtonWrapper()
  97. .setData({custom_id: "customId", style: ButtonStyle.Primary, label: "Custom Id."})
  98. .setAction(() => false)
  99. .setSwitch(() => false)
  100. ],
  101. },
  102. attachEventMock = jest.fn(),
  103. attachOneEventMock = jest.fn(),
  104. interaction = ({createMessageComponentCollector: () => ({on: attachEventMock, once: attachOneEventMock} as unknown) as InteractionCollector<ButtonInteraction>, fetchReply: async () => interaction} as unknown) as ButtonInteraction;
  105. Object.setPrototypeOf(interaction, ButtonInteraction.prototype);
  106. const pagination = new PaginationSent(data, interaction);
  107. await pagination.init();
  108. expect(async () => {
  109. await pagination.init();
  110. }).rejects.toThrow();
  111. });
  112. });