PaginationSent.test.ts 3.8 KB

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