InteractionPagination.test.ts 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { MessageActionRow, MessageButton, MessageEmbed, Interaction } from "discord.js";
  2. import InteractionPagination from "../../src/Classes/Paginations/InteractionPagination";
  3. import FirstPageButton from "../../src/Classes/Buttons/FirstPageButton";
  4. import Constants from "../../src/Constants";
  5. describe("Pagination that is sent to a channel", () => {
  6. test("should initialize with default values.", () => {
  7. const pagination = new InteractionPagination();
  8. expect(pagination.messageOptions).toBeNull();
  9. });
  10. test("should initialize from another ChannelPagination.", () => {
  11. const pagination = new InteractionPagination()
  12. .setMessageOptions({allowedMentions: {repliedUser: false}})
  13. .setFilterOptions({noAccessReply: true, singleUserAccess: true}),
  14. { filterOptions, messageOptions } = pagination,
  15. newPagination = new InteractionPagination(pagination);
  16. expect(newPagination.filterOptions).toBe(filterOptions);
  17. expect(newPagination.messageOptions).toBe(messageOptions);
  18. });
  19. test("should set messageOptions and clear embeds and components fields.", () => {
  20. const pagination = new InteractionPagination(),
  21. messageOptions = {components: [new MessageActionRow<MessageButton>()], embeds: [new MessageEmbed()], allowedMentions: {repliedUser: false}, ephemeral: true}
  22. expect(pagination.setMessageOptions(messageOptions).messageOptions).toStrictEqual({components: [], embeds: [], allowedMentions: {repliedUser: false}, ephemeral: true});
  23. });
  24. test("setting time should fail if supplied time isn't a natural one.", () => {
  25. const pagination = new InteractionPagination();
  26. expect(() => pagination.setTime(-1)).toThrow("Time should be natural.");
  27. expect(() => pagination.setTime(2.5)).toThrow("Time should be natural.");
  28. });
  29. test("setting time should fail if supplied time is lesser than a minimum.", () => {
  30. const pagination = new InteractionPagination();
  31. expect(() => pagination.setTime(500)).toThrow("Pagination should exist at least one second.");
  32. });
  33. test("setting time should fail if supplied time is greater than 15 minutes.", () => {
  34. const pagination = new InteractionPagination();
  35. expect(() => pagination.setTime(Constants.DISCORD_MAX_INTERACTION_LIFE_TIME + 1)).toThrow("Total life time of InteractionPagination should be no more than fifteen minutes.");
  36. });
  37. test("sending should fail if there are no embeds.", () => {
  38. const pagination = new InteractionPagination()
  39. .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
  40. .setTime(60000);
  41. const interactionMock:Interaction = ({} as unknown) as Interaction;
  42. expect(async () => await pagination.send(interactionMock)).rejects.toThrow("Pagination should have at least one button, page and settep up time.");
  43. });
  44. test("sending should fail if there are no buttons.", () => {
  45. const pagination = new InteractionPagination()
  46. .setEmbeds(new MessageEmbed().setDescription("description"))
  47. .setTime(60000);
  48. const interactionMock:Interaction = ({} as unknown) as Interaction;
  49. expect(async () => await pagination.send(interactionMock)).rejects.toThrow("Pagination should have at least one button, page and settep up time.");
  50. });
  51. test("sending should fail if there are no time setted up.", () => {
  52. const pagination = new InteractionPagination()
  53. .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
  54. .setEmbeds(new MessageEmbed().setDescription("description"));
  55. const interactionMock:Interaction = ({} as unknown) as Interaction;
  56. expect(async () => await pagination.send(interactionMock)).rejects.toThrow("Pagination should have at least one button, page and settep up time.");
  57. });
  58. test("sending should fail if interaction is not repliable.", () => {
  59. const pagination = new InteractionPagination()
  60. .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
  61. .setEmbeds(new MessageEmbed().setDescription("description"))
  62. .setTime(60000);
  63. const interactionMock:Interaction = ({isRepliable: () => false} as unknown) as Interaction;
  64. expect(async () => await pagination.send(interactionMock)).rejects.toThrow("Interaction should be repliable!");
  65. });
  66. });