test.spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { describe, it, expect, vi, afterEach } from 'vitest';
  2. import { render, fireEvent, cleanup } from "@testing-library/vue"
  3. import { IconAccessible, IconAccessibleFilled } from "./src/tabler-icons-vue.ts"
  4. describe("Vue Icon component", () => {
  5. afterEach(() => cleanup())
  6. it("should render icon component", () => {
  7. const { container } = render(IconAccessible)
  8. expect(container.getElementsByTagName("svg").length).toBeGreaterThan(0);
  9. })
  10. it('should add a class to the element', () => {
  11. const { container } = render(IconAccessible, {
  12. props: {
  13. class: 'test-class',
  14. },
  15. })
  16. const svg = container.getElementsByTagName("svg")[0]
  17. expect(svg).toHaveClass('test-class')
  18. expect(svg).toHaveClass('tabler-icon')
  19. expect(svg).toHaveClass('tabler-icon-accessible')
  20. })
  21. it('should add a style attribute to the element', () => {
  22. const { container } = render(IconAccessible, {
  23. props: {
  24. style: 'color: red',
  25. },
  26. })
  27. const svg = container.getElementsByTagName("svg")[0]
  28. expect(svg).toHaveStyle('color: rgb(255, 0, 0)')
  29. })
  30. it("should update svg attributes when there are props passed to the component", () => {
  31. const { container } = render(IconAccessible, {
  32. props: {
  33. size: 48,
  34. color: "red",
  35. "stroke": 4,
  36. },
  37. })
  38. const svg = container.getElementsByTagName("svg")[0]
  39. expect(svg.getAttribute("width")).toBe("48")
  40. expect(svg.getAttribute("stroke")).toBe("red")
  41. expect(svg.getAttribute("stroke-width")).toBe("4")
  42. })
  43. it("should update svg attributes when there are props passed to the filled version of component", () => {
  44. const { container } = render(IconAccessibleFilled, {
  45. props: {
  46. size: 48,
  47. color: "red"
  48. },
  49. })
  50. const svg = container.getElementsByTagName("svg")[0]
  51. expect(svg.getAttribute("width")).toBe("48")
  52. expect(svg.getAttribute("fill")).toBe("red")
  53. expect(svg.getAttribute("stroke")).toBe("none")
  54. expect(svg.getAttribute("stroke-width")).toBe(null)
  55. })
  56. it('should call the onClick event', async () => {
  57. const onClick = vi.fn()
  58. const { container } = render(IconAccessible, {
  59. attrs: {
  60. onClick,
  61. }
  62. })
  63. const svg = container.getElementsByTagName("svg")[0]
  64. await fireEvent.click(svg)
  65. expect(onClick).toHaveBeenCalled()
  66. })
  67. it('should pass children to the icon slot', () => {
  68. const testText = 'Hello World'
  69. const template = {
  70. name: 'Stub',
  71. template: `<text>${testText}</text>`
  72. }
  73. const { getByText, container } = render(IconAccessible, {
  74. slots: {
  75. default: template
  76. }
  77. })
  78. const textElement = getByText(testText)
  79. expect(textElement).toBeInTheDocument()
  80. expect(container.innerHTML).toMatchInlineSnapshot(`"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-linecap="round" stroke-linejoin="round" class="tabler-icon tabler-icon-accessible"><path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0"></path><path d="M10 16.5l2 -3l2 3m-2 -3v-2l3 -1m-6 0l3 1"></path><circle cx="12" cy="7.5" r=".5" fill="currentColor"></circle><text>Hello World</text></svg>"`);
  81. });
  82. it("should match snapshot", () => {
  83. const { container } = render(IconAccessible);
  84. expect(container.innerHTML).toMatchInlineSnapshot(`"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke-linecap="round" stroke-linejoin="round" class="tabler-icon tabler-icon-accessible"><path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0"></path><path d="M10 16.5l2 -3l2 3m-2 -3v-2l3 -1m-6 0l3 1"></path><circle cx="12" cy="7.5" r=".5" fill="currentColor"></circle></svg>"`)
  85. })
  86. })