top_bar_test.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { act, assertEquals, cleanup, fireEvent, render } from "./setup.ts";
  2. import { EditorMode } from "../../islands/Editor.tsx";
  3. import TopBar from "../../islands/TopBar.tsx";
  4. const baseProps = {
  5. allowMode: EditorMode.Read,
  6. isLogined: false,
  7. shared: false,
  8. title: "Test Post",
  9. id: "abc123",
  10. };
  11. Deno.test({
  12. name: "TopBar - renders download icon for non-logged-in user",
  13. fn() {
  14. const { container } = render(<TopBar {...baseProps} isLogined={false} />);
  15. const downloadIcons = container.querySelectorAll(".bi-download");
  16. assertEquals(downloadIcons.length, 1);
  17. assertEquals(downloadIcons[0].getAttribute("title"), "Download");
  18. cleanup();
  19. },
  20. sanitizeResources: false,
  21. sanitizeOps: false,
  22. });
  23. Deno.test({
  24. name: "TopBar - renders download icon for logged-in user",
  25. fn() {
  26. const { container } = render(<TopBar {...baseProps} isLogined />);
  27. const downloadIcons = container.querySelectorAll(".bi-download");
  28. assertEquals(downloadIcons.length, 1);
  29. assertEquals(downloadIcons[0].getAttribute("title"), "Download");
  30. cleanup();
  31. },
  32. sanitizeResources: false,
  33. sanitizeOps: false,
  34. });
  35. Deno.test({
  36. name: "TopBar - click download dispatches DownloadRequest event with title",
  37. fn() {
  38. const dispatched: { type: string; detail: string }[] = [];
  39. const originalDispatchEvent = globalThis.dispatchEvent;
  40. // deno-lint-ignore no-explicit-any
  41. (globalThis as any).dispatchEvent = (e: Event) => {
  42. dispatched.push({
  43. type: e.type,
  44. detail: (e as CustomEvent).detail,
  45. });
  46. };
  47. const { container } = render(<TopBar {...baseProps} />);
  48. const icon = container.querySelector(".bi-download")!;
  49. act(() => {
  50. fireEvent.click(icon);
  51. });
  52. assertEquals(dispatched.length, 1);
  53. assertEquals(dispatched[0].type, "DownloadRequest");
  54. assertEquals(dispatched[0].detail, "Test Post");
  55. // deno-lint-ignore no-explicit-any
  56. (globalThis as any).dispatchEvent = originalDispatchEvent;
  57. cleanup();
  58. },
  59. sanitizeResources: false,
  60. sanitizeOps: false,
  61. });
  62. Deno.test({
  63. name: "TopBar - click download dispatches empty string for empty title",
  64. fn() {
  65. const dispatched: { type: string; detail: string }[] = [];
  66. const originalDispatchEvent = globalThis.dispatchEvent;
  67. // deno-lint-ignore no-explicit-any
  68. (globalThis as any).dispatchEvent = (e: Event) => {
  69. dispatched.push({
  70. type: e.type,
  71. detail: (e as CustomEvent).detail,
  72. });
  73. };
  74. const { container } = render(<TopBar {...baseProps} title="" />);
  75. const icon = container.querySelector(".bi-download")!;
  76. act(() => {
  77. fireEvent.click(icon);
  78. });
  79. assertEquals(dispatched.length, 1);
  80. assertEquals(dispatched[0].type, "DownloadRequest");
  81. assertEquals(dispatched[0].detail, "");
  82. // deno-lint-ignore no-explicit-any
  83. (globalThis as any).dispatchEvent = originalDispatchEvent;
  84. cleanup();
  85. },
  86. sanitizeResources: false,
  87. sanitizeOps: false,
  88. });