test.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { describe, it, expect, afterEach } from 'vitest';
  2. import { render, cleanup } from '@testing-library/preact'
  3. import { IconAccessible, IconAccessibleFilled } from "./src/tabler-icons-preact"
  4. describe("Preact Icon component", () => {
  5. afterEach(() => {
  6. cleanup();
  7. });
  8. it("should render icon component", () => {
  9. const { container } = render(<IconAccessible/>)
  10. expect(container.getElementsByTagName("svg").length).toBeGreaterThan(0)
  11. })
  12. it("should update svg attributes when there are props passed to the component", () => {
  13. const { container } = render(<IconAccessible size={48} color={"red"} stroke={4}/>)
  14. const svg = container.getElementsByTagName("svg")[0]
  15. expect(svg.getAttribute("width")).toBe("48")
  16. expect(svg.getAttribute("fill")).toBe("none")
  17. expect(svg.getAttribute("stroke")).toBe("red")
  18. expect(svg.getAttribute("stroke-width")).toBe("4")
  19. })
  20. it("should update svg attributes when there are props passed to the filled version of component", () => {
  21. const { container } = render(<IconAccessibleFilled size={48} color={"red"}/>)
  22. const svg = container.getElementsByTagName("svg")[0]
  23. expect(svg.getAttribute("width")).toBe("48")
  24. expect(svg.getAttribute("fill")).toBe("red")
  25. expect(svg.getAttribute("stroke")).toBe("none")
  26. expect(svg.getAttribute("stroke-width")).toBe(null)
  27. })
  28. it('should apply all classNames to the element', () => {
  29. const testClass = 'test-class';
  30. const { container } = render(
  31. <IconAccessible className={testClass} />,
  32. );
  33. expect(container.firstChild).toHaveClass(testClass);
  34. expect(container.firstChild).toHaveClass('tabler-icon');
  35. expect(container.firstChild).toHaveClass('tabler-icon-accessible');
  36. });
  37. it('should add a style attribute to the element', () => {
  38. const { container } = render(<IconAccessible style={{ color: "red" }}/>)
  39. const svg = container.getElementsByTagName("svg")[0]
  40. expect(svg).toHaveStyle('color: rgb(255, 0, 0)')
  41. })
  42. it('should add a title element to the svg', () => {
  43. const { container } = render(<IconAccessible title="Accessible Icon"/>)
  44. const svg = container.getElementsByTagName("svg")[0]
  45. const title = container.getElementsByTagName("title")[0]
  46. expect(svg.getAttribute("title")).toBe("Accessible Icon");
  47. expect(title).toHaveTextContent("Accessible Icon");
  48. })
  49. it("should match snapshot", () => {
  50. const { container } = render(<IconAccessible/>)
  51. expect(container.innerHTML).toMatchInlineSnapshot(`
  52. <svg xmlns="http://www.w3.org/2000/svg"
  53. width="24"
  54. height="24"
  55. viewbox="0 0 24 24"
  56. fill="none"
  57. stroke="currentColor"
  58. stroke-width="2"
  59. stroke-linecap="round"
  60. stroke-linejoin="round"
  61. class="tabler-icon tabler-icon-accessible "
  62. >
  63. <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0">
  64. </path>
  65. <path d="M10 16.5l2 -3l2 3m-2 -3v-2l3 -1m-6 0l3 1">
  66. </path>
  67. <circle cx="12"
  68. cy="7.5"
  69. r=".5"
  70. fill="currentColor"
  71. >
  72. </circle>
  73. </svg>
  74. `)
  75. })
  76. })