test.spec.tsx 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { describe, it, expect, afterEach, expectTypeOf } from 'vitest';
  2. import { render, cleanup } from '@testing-library/react'
  3. import { IconAccessible, IconAccessibleFilled, createReactComponent } from "./src/tabler-icons-react"
  4. describe("React 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("stroke")).toBe("red")
  17. expect(svg.getAttribute("stroke-width")).toBe("4")
  18. expect(svg.getAttribute("fill")).toBe("none")
  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 have proper type', () => {
  43. expectTypeOf(IconAccessible).toBeFunction();
  44. expectTypeOf(IconAccessible).toEqualTypeOf(createReactComponent('outline', 'accessible', 'Accessible', []));
  45. });
  46. it('should add title child element to svg when title prop is passed', () => {
  47. const { container } = render(<IconAccessible title="Accessible Icon"/>);
  48. const svg = container.getElementsByTagName("svg")[0];
  49. const title = svg.getElementsByTagName("title")[0];
  50. expect(title).toHaveTextContent("Accessible Icon");
  51. })
  52. it("should match snapshot", () => {
  53. const { container } = render(<IconAccessible/>)
  54. expect(container.innerHTML).toMatchInlineSnapshot(`
  55. <svg xmlns="http://www.w3.org/2000/svg"
  56. width="24"
  57. height="24"
  58. viewbox="0 0 24 24"
  59. fill="none"
  60. stroke="currentColor"
  61. stroke-width="2"
  62. stroke-linecap="round"
  63. stroke-linejoin="round"
  64. class="tabler-icon tabler-icon-accessible "
  65. >
  66. <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0">
  67. </path>
  68. <path d="M10 16.5l2 -3l2 3m-2 -3v-2l3 -1m-6 0l3 1">
  69. </path>
  70. <circle cx="12"
  71. cy="7.5"
  72. r=".5"
  73. fill="currentColor"
  74. >
  75. </circle>
  76. </svg>
  77. `)
  78. })
  79. })