test.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 add title child element to svg when title prop is passed', () => {
  57. const { container } = render(IconAccessible, {
  58. props: {
  59. title: 'Test Title',
  60. }
  61. })
  62. const svg = container.getElementsByTagName("svg")[0]
  63. const title = container.getElementsByTagName("title")[0]
  64. expect(title).toHaveTextContent('Test Title')
  65. expect(svg).toContainElement(title)
  66. })
  67. it('should call the onClick event', async () => {
  68. const onClick = vi.fn()
  69. const { container } = render(IconAccessible, {
  70. attrs: {
  71. onClick,
  72. }
  73. })
  74. const svg = container.getElementsByTagName("svg")[0]
  75. await fireEvent.click(svg)
  76. expect(onClick).toHaveBeenCalled()
  77. })
  78. it('should pass children to the icon slot', () => {
  79. const testText = 'Hello World'
  80. const template = {
  81. name: 'Stub',
  82. template: `<text>${testText}</text>`
  83. }
  84. const { getByText, container } = render(IconAccessible, {
  85. slots: {
  86. default: template
  87. }
  88. })
  89. const textElement = getByText(testText)
  90. expect(textElement).toBeInTheDocument()
  91. expect(container.innerHTML).toMatchInlineSnapshot(`"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" 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>"`);
  92. });
  93. it("should match snapshot", () => {
  94. const { container } = render(IconAccessible);
  95. expect(container.innerHTML).toMatchInlineSnapshot(`"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" 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>"`)
  96. })
  97. })