test.spec.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { describe, it, expect, vi, afterEach } from 'vitest';
  2. import { render, fireEvent, cleanup } from "@testing-library/vue"
  3. import { Icon2fa } from "./src/tabler-icons-vue.ts"
  4. describe("Vue Icon component", () => {
  5. afterEach(() => cleanup())
  6. it("should render icon component", () => {
  7. const { container } = render(Icon2fa)
  8. expect(container.getElementsByTagName("svg").length).toBeGreaterThan(0);
  9. })
  10. it('should add a class to the element', () => {
  11. const { container } = render(Icon2fa, {
  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-2fa')
  20. })
  21. it('should add a style attribute to the element', () => {
  22. const { container } = render(Icon2fa, {
  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(Icon2fa, {
  32. props: {
  33. size: 48,
  34. color: "red",
  35. "stroke-width": 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 call the onClick event', async () => {
  44. const onClick = vi.fn()
  45. const { container } = render(Icon2fa, {
  46. attrs: {
  47. onClick,
  48. }
  49. })
  50. const svg = container.getElementsByTagName("svg")[0]
  51. await fireEvent.click(svg)
  52. expect(onClick).toHaveBeenCalled()
  53. })
  54. it('should pass children to the icon slot', () => {
  55. const testText = 'Hello World'
  56. const template = {
  57. name: 'Stub',
  58. template: `<text>${testText}</text>`
  59. }
  60. const { getByText, container } = render(Icon2fa, {
  61. slots: {
  62. default: template
  63. }
  64. })
  65. const textElement = getByText(testText)
  66. expect(textElement).toBeInTheDocument()
  67. expect(container.innerHTML).toMatchInlineSnapshot(`"<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="tabler-icon tabler-icon-2fa"><path d="M7 16h-4l3.47 -4.66a2 2 0 1 0 -3.47 -1.54"></path><path d="M10 16v-8h4"></path><path d="M10 12l3 0"></path><path d="M17 16v-6a2 2 0 0 1 4 0v6"></path><path d="M17 13l4 0"></path><text>Hello World</text></svg>"`);
  68. });
  69. it("should match snapshot", () => {
  70. const { container } = render(Icon2fa);
  71. expect(container.innerHTML).toMatchInlineSnapshot(`"<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="tabler-icon tabler-icon-2fa"><path d="M7 16h-4l3.47 -4.66a2 2 0 1 0 -3.47 -1.54"></path><path d="M10 16v-8h4"></path><path d="M10 12l3 0"></path><path d="M17 16v-6a2 2 0 0 1 4 0v6"></path><path d="M17 13l4 0"></path></svg>"`)
  72. })
  73. })