ButtonWrapper.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { ButtonStyle, ComponentType } from "discord.js";
  2. import { ButtonWrapper, ButtonData } from "../../src/Paginations";
  3. describe("ButtonWrapper: class that wrapps functionality of a button.", () => {
  4. test("should be able to store button's data.", () => {
  5. const data1:ButtonData = {custom_id: "example_id", style: ButtonStyle.Success, label: "example_label"},
  6. data2:ButtonData = {custom_id: "example_id2", style: ButtonStyle.Danger, label: "example_label2", emoji: "❤"};
  7. const wrapper = new ButtonWrapper(data1);
  8. expect(wrapper.data).toStrictEqual(data1);
  9. expect(wrapper.toJSON()).toStrictEqual(data1);
  10. wrapper.setData(data2);
  11. expect(wrapper.data).toStrictEqual(data2);
  12. expect(wrapper.toJSON()).toStrictEqual(data2);
  13. });
  14. test("should be able to make builtComponent from button's data.", () => {
  15. const data1:ButtonData = {custom_id: "example_id", style: ButtonStyle.Success, label: "example_label"},
  16. data2:ButtonData = {custom_id: "example_id2", style: ButtonStyle.Danger, label: "example_label2", emoji: "❤"};
  17. const discordData1 = {...data1, type: ComponentType.Button, emoji: undefined},
  18. discordData2 = {...data2, type: ComponentType.Button, emoji: {animated: false, id: undefined, name: data2.emoji}};
  19. const wrapper = new ButtonWrapper(discordData1);
  20. expect(wrapper.builtComponent.data).toStrictEqual(discordData1);
  21. expect(wrapper.builtComponent.toJSON()).toStrictEqual(discordData1);
  22. wrapper.setData(data2);
  23. expect(wrapper.builtComponent.data).toStrictEqual(discordData2);
  24. expect(wrapper.builtComponent.toJSON()).toStrictEqual(discordData2);
  25. });
  26. test("should be able to store action function.", () => {
  27. const action = () => false;
  28. const wrapper = new ButtonWrapper()
  29. .setAction(action);
  30. expect(wrapper.action).toStrictEqual(action);
  31. });
  32. test("should be able to store switch function.", () => {
  33. const switchFunction = () => false;
  34. const wrapper = new ButtonWrapper()
  35. .setSwitch(switchFunction);
  36. expect(wrapper.switch).toStrictEqual(switchFunction);
  37. });
  38. test("should be able to re-create from another instance of class.", () => {
  39. const wrapper1 = new ButtonWrapper({custom_id: "example_id", style: ButtonStyle.Primary, emoji: "❤"})
  40. .setAction(() => false)
  41. .setSwitch(() => false),
  42. wrapper2 = new ButtonWrapper(wrapper1),
  43. wrapper3 = new ButtonWrapper().setData(wrapper2);
  44. expect(wrapper2).toStrictEqual(wrapper1);
  45. expect(wrapper3).toStrictEqual(wrapper2);
  46. });
  47. });