123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- import { ButtonInteraction, InteractionCollector, Message, MessageActionRow, MessageButton, MessageComponentCollectorOptions, MessageEmbed } from "discord.js";
- import MessageReplyPagination from "../../src/Classes/Paginations/MessageReplyPagination";
- import FirstPageButton from "../../src/Classes/Buttons/FirstPageButton";
- import LastPageButton from "../../src/Classes/Buttons/LastPageButton";
- import PreviousPageButton from "../../src/Classes/Buttons/PreviousPageButton";
- import NextPageButton from "../../src/Classes/Buttons/NextPageButton";
- import StopButton from "../../src/Classes/Buttons/StopButton";
- describe("Pagination that is sent as a reply to a message", () => {
- test("should initialize with default values.", () => {
- const pagination = new MessageReplyPagination();
- expect(pagination.messageOptions).toBeNull();
- });
-
- test("should initialize from another ChannelPagination.", () => {
- const pagination = new MessageReplyPagination()
- .setMessageOptions({allowedMentions: {repliedUser: false}})
- .setFilterOptions({noAccessReply: true, singleUserAccess: true}),
-
- { filterOptions, messageOptions } = pagination,
- newPagination = new MessageReplyPagination(pagination);
- expect(newPagination.filterOptions).toBe(filterOptions);
- expect(newPagination.messageOptions).toBe(messageOptions);
- });
- test("should set messageOptions and clear embeds and components fields.", () => {
- const pagination = new MessageReplyPagination(),
- messageOptions = {components: [new MessageActionRow<MessageButton>()], embeds: [new MessageEmbed()], allowedMentions: {repliedUser: false}, failIfNotExists: false}
- expect(pagination.setMessageOptions(messageOptions).messageOptions).toStrictEqual({components: [], embeds: [], allowedMentions: {repliedUser: false}, failIfNotExists: false});
- });
- test("sending should fail if there are no embeds.", () => {
- const pagination = new MessageReplyPagination()
- .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
- .setTime(60000);
- const messageMock:Message = ({} as unknown) as Message;
- expect(async () => await pagination.send(messageMock)).rejects.toThrow("Pagination should have at least one button, page and settep up time.");
- });
- test("sending should fail if there are no buttons.", () => {
- const pagination = new MessageReplyPagination()
- .setEmbeds(new MessageEmbed().setDescription("description"))
- .setTime(60000);
- const messageMock:Message = ({} as unknown) as Message;
- expect(async () => await pagination.send(messageMock)).rejects.toThrow("Pagination should have at least one button, page and settep up time.");
- });
- test("sending should fail if there are no time setted up.", () => {
- const pagination = new MessageReplyPagination()
- .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
- .setEmbeds(new MessageEmbed().setDescription("description"));
- const messageMock:Message = ({} as unknown) as Message;
- expect(async () => await pagination.send(messageMock)).rejects.toThrow("Pagination should have at least one button, page and settep up time.");
- });
- test("should send a message.", async () => {
- const pagination = new MessageReplyPagination()
- .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
- .setEmbeds(new MessageEmbed().setDescription("description"))
- .setTime(60000),
- interactionCollectorMock:InteractionCollector<ButtonInteraction> = ({on: jest.fn()} as unknown) as InteractionCollector<ButtonInteraction>,
- replyMock:Message = ({createMessageComponentCollector: jest.fn(() => interactionCollectorMock)} as unknown) as Message,
- messageMock:Message = ({reply: jest.fn(() => replyMock)} as unknown) as Message;
- await pagination.send(messageMock);
- expect(messageMock.reply).toHaveBeenCalledWith({
- embeds: [new MessageEmbed().setDescription("description")],
- components: [new MessageActionRow().setComponents(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER").setDisabled(true))]
- });
- });
- test("should execute afterSending action.", async () => {
- const actionAfterSending = jest.fn(),
-
- pagination = new MessageReplyPagination()
- .setAfterSendingAction(actionAfterSending)
- .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
- .setEmbeds(new MessageEmbed().setDescription("description"))
- .setTime(60000),
- interactionCollectorMock:InteractionCollector<ButtonInteraction> = ({on: jest.fn()} as unknown) as InteractionCollector<ButtonInteraction>,
- replyMock:Message = ({createMessageComponentCollector: jest.fn(() => interactionCollectorMock)} as unknown) as Message,
- messageMock:Message = ({reply: jest.fn(() => replyMock)} as unknown) as Message;
- await pagination.send(messageMock);
- expect(actionAfterSending).toHaveBeenCalledWith(replyMock);
- });
- test("all methods that change class properties should throw errors.", async () => {
- const pagination = new MessageReplyPagination()
- .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
- .setEmbeds(new MessageEmbed().setDescription("description"))
- .setTime(60000),
- interactionCollectorMock:InteractionCollector<ButtonInteraction> = ({on: jest.fn()} as unknown) as InteractionCollector<ButtonInteraction>,
- replyMock:Message = ({createMessageComponentCollector: jest.fn(() => interactionCollectorMock)} as unknown) as Message,
- messageMock:Message = ({reply: jest.fn(() => replyMock)} as unknown) as Message;
- await pagination.send(messageMock);
- await expect(async () => await pagination.send(messageMock)).rejects.toThrow("The pagination is already sent.");
- expect(() => pagination.setTime(60000)).toThrow("The pagination is already sent.");
- expect(() => pagination.setOnStopAction(() => {})).toThrow("The pagination is already sent.");
- expect(() => pagination.setMessageOptions({stickers: []})).toThrow("The pagination is already sent.");
- expect(() => pagination.setEmbeds(new MessageEmbed().setDescription("description"))).toThrow("The pagination is already sent.");
- expect(() => pagination.setCollectorOptions({maxIdleTime: 10})).toThrow("The pagination is already sent.");
- expect(() => pagination.setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))).toThrow("The pagination is already sent.");
- expect(() => pagination.setAfterSendingAction(() => {})).toThrow("The pagination is already sent.");
- expect(() => pagination.removeEmbeds(1)).toThrow("The pagination is already sent.");
- expect(() => pagination.insertEmbeds(new MessageEmbed().setDescription("description"))).toThrow("The pagination is already sent.");
- expect(() => pagination.setFilterOptions({singleUserAccess: true})).toThrow("The pagination is already sent.");
- });
- test("filter should filter out messages with another ids.", async () => {
- const pagination = new MessageReplyPagination()
- .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
- .setEmbeds(new MessageEmbed().setDescription("description"))
- .setTime(60000),
- interactionCollectorMock:InteractionCollector<ButtonInteraction> = ({on: jest.fn(), constructor: jest.fn()} as unknown) as InteractionCollector<ButtonInteraction>,
- collectorMock = jest.fn((data:MessageComponentCollectorOptions<ButtonInteraction>) => interactionCollectorMock),
- replyMock:Message = ({createMessageComponentCollector: collectorMock, id: 1} as unknown) as Message,
- messageMock:Message = ({reply: jest.fn(() => replyMock)} as unknown) as Message;
- await pagination.send(messageMock);
- if (!collectorMock.mock.calls[0][0].filter)
- throw new Error("Filter should be defined!");
- const interactionMock:ButtonInteraction = ({message: {id: 2}} as unknown) as ButtonInteraction,
- result = await collectorMock.mock.calls[0][0].filter(interactionMock);
- expect(result).toBeFalsy();
- });
- test("should set interactionCollector events' scripts.", async () => {
- const pagination = new MessageReplyPagination()
- .setButtons(new FirstPageButton(new MessageButton().setCustomId("testid").setEmoji("▶").setStyle("DANGER")))
- .setEmbeds(new MessageEmbed().setDescription("description"))
- .setTime(60000),
- mockOn = jest.fn(),
- interactionCollectorMock:InteractionCollector<ButtonInteraction> = ({on: mockOn} as unknown) as InteractionCollector<ButtonInteraction>,
- replyMock:Message = ({createMessageComponentCollector: jest.fn(() => interactionCollectorMock)} as unknown) as Message,
- messageMock:Message = ({reply: jest.fn(() => replyMock)} as unknown) as Message;
- await pagination.send(messageMock);
- expect(mockOn).toHaveBeenCalledTimes(2);
- expect(mockOn.mock.calls[0][0]).toBe("collect");
- expect(mockOn.mock.calls[0][1]).toBeInstanceOf(Function);
- expect(mockOn.mock.calls[1][0]).toBe("end");
- expect(mockOn.mock.calls[1][1]).toBeInstanceOf(Function);
- });
- test("should edit message on collect.", async () => {
- const buttonStyles =
- [
- new MessageButton().setCustomId("first").setLabel("First").setStyle("SUCCESS"),
- new MessageButton().setCustomId("prev").setLabel("Previous").setStyle("PRIMARY"),
- new MessageButton().setCustomId("stop").setLabel("Stop").setStyle("DANGER"),
- new MessageButton().setCustomId("next").setLabel("Next").setStyle("PRIMARY"),
- new MessageButton().setCustomId("last").setLabel("Last").setStyle("SUCCESS")
- ],
-
- buttons =
- [
- new FirstPageButton(buttonStyles[0]),
- new PreviousPageButton(buttonStyles[1]),
- new StopButton(buttonStyles[2]),
- new NextPageButton(buttonStyles[3]),
- new LastPageButton(buttonStyles[4])
- ],
-
- embeds =
- [
- new MessageEmbed().setColor("RANDOM").setDescription("First page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Wow! It's second page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Unbelivable! Third class page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Not possible! Fourth page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Not probable! Special fifth page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Progress! It's page with number six that is stored with number five!"),
- new MessageEmbed().setColor("RANDOM").setDescription("You're feeling with determination because of the seven page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("You shall not pass! It's the last and the latest page!"),
- ],
- pagination = new MessageReplyPagination()
- .setFilterOptions({resetTimer: true})
- .setButtons(buttons)
- .setEmbeds(embeds)
- .setTime(60000),
- mockOn = jest.fn(),
- editMock = jest.fn(),
- interactionCollectorMock:InteractionCollector<ButtonInteraction> = ({on: mockOn, resetTimer: jest.fn()} as unknown) as InteractionCollector<ButtonInteraction>,
- replyMock:Message = ({createMessageComponentCollector: jest.fn(() => interactionCollectorMock)} as unknown) as Message,
- messageMock:Message = ({reply: jest.fn(() => replyMock)} as unknown) as Message;
- await pagination.send(messageMock);
-
- const collectEvent = mockOn.mock.calls[0][1];
- let interactionMock:ButtonInteraction = ({customId: "last", deferUpdate: jest.fn(), editReply: editMock} as unknown) as ButtonInteraction;
- await collectEvent(interactionMock);
- interactionMock = ({customId: "prev", deferUpdate: jest.fn(), editReply: editMock} as unknown) as ButtonInteraction;
- await collectEvent(interactionMock);
- interactionMock = ({customId: "next", deferUpdate: jest.fn(), editReply: editMock} as unknown) as ButtonInteraction;
- await collectEvent(interactionMock);
- interactionMock = ({customId: "first", deferUpdate: jest.fn(), editReply: editMock} as unknown) as ButtonInteraction;
- await collectEvent(interactionMock);
- expect(editMock.mock.calls[0][0]).toStrictEqual({embeds: [embeds[embeds.length - 1]], components: [new MessageActionRow().setComponents([
- buttonStyles[0],
- buttonStyles[1],
- buttonStyles[2],
- buttonStyles[3].setDisabled(true),
- buttonStyles[4].setDisabled(true)
- ])]});
- expect(editMock.mock.calls[1][0]).toStrictEqual({embeds: [embeds[embeds.length - 2]], components: [new MessageActionRow().setComponents([
- buttonStyles[0],
- buttonStyles[1],
- buttonStyles[2],
- buttonStyles[3],
- buttonStyles[4]
- ])]});
- expect(editMock.mock.calls[2][0]).toStrictEqual({embeds: [embeds[embeds.length - 1]], components: [new MessageActionRow().setComponents([
- buttonStyles[0],
- buttonStyles[1],
- buttonStyles[2],
- buttonStyles[3].setDisabled(true),
- buttonStyles[4].setDisabled(true)
- ])]});
- expect(editMock.mock.calls[3][0]).toStrictEqual({embeds: [embeds[0]], components: [new MessageActionRow().setComponents([
- buttonStyles[0].setDisabled(true),
- buttonStyles[1].setDisabled(true),
- buttonStyles[2],
- buttonStyles[3],
- buttonStyles[4]
- ])]});
- });
- test("should stop pagination and trigger onStop action if stop button is triggered.", async () => {
- const buttons =
- [
- new FirstPageButton(new MessageButton().setCustomId("first").setLabel("First").setStyle("SUCCESS")),
- new PreviousPageButton(new MessageButton().setCustomId("prev").setLabel("Previous").setStyle("PRIMARY")),
- new StopButton(new MessageButton().setCustomId("stop").setLabel("Stop").setStyle("DANGER")),
- new NextPageButton(new MessageButton().setCustomId("next").setLabel("Next").setStyle("PRIMARY")),
- new LastPageButton(new MessageButton().setCustomId("last").setLabel("Last").setStyle("SUCCESS"))
- ],
-
- embeds =
- [
- new MessageEmbed().setColor("RANDOM").setDescription("First page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Wow! It's second page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Unbelivable! Third class page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Not possible! Fourth page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Not probable! Special fifth page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("Progress! It's page with number six that is stored with number five!"),
- new MessageEmbed().setColor("RANDOM").setDescription("You're feeling with determination because of the seven page!"),
- new MessageEmbed().setColor("RANDOM").setDescription("You shall not pass! It's the last and the latest page!"),
- ],
- pagination = new MessageReplyPagination()
- .setFilterOptions({resetTimer: true, removeButtonsOnEnd: true})
- .setOnStopAction(jest.fn())
- .setButtons(buttons)
- .setEmbeds(embeds)
- .setTime(60000),
- mockOn = jest.fn(),
- editMock = jest.fn(),
- interactionCollectorMock:InteractionCollector<ButtonInteraction> = ({on: mockOn, stop: jest.fn()} as unknown) as InteractionCollector<ButtonInteraction>,
- replyMock:Message = Object.assign(Object.create(Message.prototype), {createMessageComponentCollector: jest.fn(() => interactionCollectorMock)}),
- messageMock:Message = ({reply: jest.fn(() => replyMock)} as unknown) as Message;
- jest.spyOn(Message.prototype, 'editable', 'get').mockImplementation(() => true);
- jest.spyOn(Message.prototype, 'edit').mockImplementation(editMock);
-
- await pagination.send(messageMock);
-
- const collectEvent = mockOn.mock.calls[0][1],
- stopEvent = mockOn.mock.calls[1][1],
- interactionMock:ButtonInteraction = ({customId: "stop", deferUpdate: jest.fn()} as unknown) as ButtonInteraction;
- await collectEvent(interactionMock);
- expect(interactionCollectorMock.stop).toHaveBeenCalled();
- await stopEvent(new Map(), 'user');
- expect(editMock).toHaveBeenCalled();
- expect(editMock.mock.calls[0][0]).toStrictEqual({components: []});
- expect(pagination.onStop).toHaveBeenCalledWith('user');
- });
- });
|