ButtonWrapper.test.ts 2.5 KB

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