test.spec.tsx 3.1 KB

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