CommonLink.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { renderComponent } from '#tests/support/components/index.ts'
  3. import { useApplicationStore } from '#shared/stores/application.ts'
  4. import CommonLink, { type Props } from '../CommonLink.vue'
  5. import type { MountingOptions } from '@vue/test-utils'
  6. const wrapperParameters = {
  7. router: true,
  8. slots: {
  9. default: 'A test link',
  10. },
  11. }
  12. const renderCommonLink = (options: MountingOptions<Props> = {}) => {
  13. const view = renderComponent(CommonLink, {
  14. ...wrapperParameters,
  15. props: {
  16. link: 'https://www.zammad.org',
  17. },
  18. ...options,
  19. })
  20. return {
  21. ...view,
  22. getLink: () => view.getByTestId('common-link'),
  23. }
  24. }
  25. describe('CommonLink.vue', () => {
  26. it('renders external link element with attributes', () => {
  27. const { getLink } = renderCommonLink()
  28. const link = getLink()
  29. expect(link).toHaveTextContent('A test link')
  30. expect(link).toHaveAttribute('href', 'https://www.zammad.org')
  31. expect(link).not.toHaveAttribute('target')
  32. })
  33. it('supports click event', async (context) => {
  34. context.skipConsole = true
  35. const { getLink, ...wrapper } = renderCommonLink()
  36. const link = getLink()
  37. await wrapper.events.click(link)
  38. expect(wrapper.emitted().click).toBeTruthy()
  39. const emittedClick = wrapper.emitted().click as Array<Array<MouseEvent>>
  40. expect(emittedClick[0][0].isTrusted).toEqual(false)
  41. })
  42. it('supports disabled prop', async () => {
  43. const { getLink, ...wrapper } = renderCommonLink({
  44. props: {
  45. link: 'https://www.zammad.org',
  46. disabled: true,
  47. },
  48. })
  49. const link = getLink()
  50. expect(link).toHaveClass('pointer-events-none')
  51. await wrapper.events.click(link)
  52. expect(wrapper.emitted().click).toBeFalsy()
  53. })
  54. it('title attribute can be used without a real prop', async () => {
  55. const title = 'a link title'
  56. const { getLink, ...wrapper } = renderCommonLink({
  57. props: {
  58. link: 'https://www.zammad.org',
  59. },
  60. attrs: {
  61. title,
  62. },
  63. })
  64. expect(getLink()).toHaveAttribute('title', title)
  65. await wrapper.rerender({
  66. openInNewTab: true,
  67. })
  68. expect(getLink()).toHaveAttribute('target', '_blank')
  69. await wrapper.rerender({
  70. target: '_self',
  71. })
  72. expect(getLink()).toHaveAttribute('target', '_self')
  73. })
  74. it('link route detection', async () => {
  75. const { getLink, ...wrapper } = renderCommonLink({
  76. props: {
  77. link: '/example',
  78. },
  79. })
  80. expect(getLink()).toHaveAttribute('href', '/mobile/example')
  81. await wrapper.rerender({
  82. link: 'https://www.zammad.org',
  83. })
  84. expect(getLink()).toHaveAttribute('href', 'https://www.zammad.org')
  85. await wrapper.rerender({
  86. link: '/#hello-world',
  87. })
  88. expect(getLink()).toHaveAttribute('href', '/#hello-world')
  89. })
  90. it('supports link prop with route object', () => {
  91. const { getLink } = renderCommonLink({
  92. props: {
  93. link: {
  94. name: 'Example',
  95. },
  96. },
  97. })
  98. const link = getLink()
  99. expect(link).toHaveTextContent('A test link')
  100. expect(link).toHaveAttribute('href', '/mobile/example')
  101. })
  102. it('supports api urls', () => {
  103. const app = useApplicationStore()
  104. app.config.api_path = '/api'
  105. const { getLink } = renderCommonLink({
  106. props: {
  107. link: '/example',
  108. restApi: true,
  109. },
  110. })
  111. expect(getLink()).toHaveAttribute('href', '/api/example')
  112. })
  113. })