CommonCalendarPreviewFlyout.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { renderComponent } from '#tests/support/components/index.ts'
  3. import { mockApplicationConfig } from '#tests/support/mock-applicationConfig.ts'
  4. import { waitForNextTick } from '#tests/support/utils.ts'
  5. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  6. import { mockCalendarIcsFileEventsQuery } from '#desktop/entities/calendar/ics-file/graphql/queries/events.mocks.ts'
  7. import CommonCalendarPreviewFlyout from '../CommonCalendarPreviewFlyout.vue'
  8. // FIXME: Is there any more logical way of mocking the default export and getting the mock reference back?!
  9. // Note that the following does not work as it results in the following error:
  10. // Error: [vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are
  11. // no top level variables inside, since this call is hoisted to top of the file.
  12. // Read more: https://vitest.dev/api/vi.html#vi-mock
  13. // Caused by: ReferenceError: Cannot access 'openExternalLinkMock' before initialization
  14. // const openExternalLinkMock = vi.fn()
  15. vi.mock('#shared/utils/openExternalLink.ts', async () => ({
  16. default: vi.fn(),
  17. }))
  18. const { default: openExternalLinkMock } = await import(
  19. '#shared/utils/openExternalLink.ts'
  20. )
  21. const renderCommonCalendarPreviewFlyout = async (
  22. props: Record<string, unknown> = {},
  23. options: any = {},
  24. ) => {
  25. const result = renderComponent(CommonCalendarPreviewFlyout, {
  26. props: {
  27. fileId: convertToGraphQLId('Store', 1),
  28. fileType: 'text/calendar',
  29. fileName: 'calendar.ics',
  30. ...props,
  31. },
  32. ...options,
  33. router: true,
  34. global: {
  35. stubs: {
  36. teleport: true,
  37. },
  38. },
  39. })
  40. await waitForNextTick()
  41. return result
  42. }
  43. describe('TicketSidebarSharedDraftFlyout.vue', () => {
  44. beforeAll(() => {
  45. mockApplicationConfig({
  46. api_path: '/api',
  47. })
  48. mockCalendarIcsFileEventsQuery({
  49. calendarIcsFileEvents: [
  50. {
  51. __typename: 'CalendarIcsFileEvent',
  52. title: 'event 1',
  53. location: 'location 1',
  54. startDate: '2024-08-22T12:00:00:00+00:00',
  55. endDate: '2024-08-22T13:00:00:00+00:00',
  56. },
  57. {
  58. __typename: 'CalendarIcsFileEvent',
  59. title: 'event 2',
  60. location: 'location 2',
  61. startDate: '2024-08-22T14:00:00:00+00:00',
  62. endDate: '2024-08-22T15:00:00:00+00:00',
  63. },
  64. {
  65. __typename: 'CalendarIcsFileEvent',
  66. title: 'event 3',
  67. location: 'location 3',
  68. startDate: '2024-08-22T16:00:00:00+00:00',
  69. endDate: '2024-08-22T17:00:00:00+00:00',
  70. },
  71. ],
  72. })
  73. })
  74. it('renders calendar preview', async () => {
  75. const wrapper = await renderCommonCalendarPreviewFlyout()
  76. expect(
  77. wrapper.getByRole('complementary', {
  78. name: 'Preview Calendar',
  79. }),
  80. ).toBeInTheDocument()
  81. expect(
  82. wrapper.getByRole('heading', { name: 'Preview Calendar' }),
  83. ).toBeInTheDocument()
  84. expect(wrapper.getByText('event 1')).toBeInTheDocument()
  85. expect(wrapper.getByText('location 1')).toBeInTheDocument()
  86. expect(wrapper.getByText('2024-08-22 12:00')).toBeInTheDocument()
  87. expect(wrapper.getByText('2024-08-22 13:00')).toBeInTheDocument()
  88. expect(wrapper.getByText('event 2')).toBeInTheDocument()
  89. expect(wrapper.getByText('location 2')).toBeInTheDocument()
  90. expect(wrapper.getByText('2024-08-22 14:00')).toBeInTheDocument()
  91. expect(wrapper.getByText('2024-08-22 15:00')).toBeInTheDocument()
  92. expect(wrapper.getByText('event 3')).toBeInTheDocument()
  93. expect(wrapper.getByText('location 3')).toBeInTheDocument()
  94. expect(wrapper.getByText('2024-08-22 16:00')).toBeInTheDocument()
  95. expect(wrapper.getByText('2024-08-22 17:00')).toBeInTheDocument()
  96. })
  97. it('supports downloading calendar', async () => {
  98. const wrapper = await renderCommonCalendarPreviewFlyout()
  99. await wrapper.events.click(
  100. wrapper.getByRole('button', {
  101. name: 'Download',
  102. }),
  103. )
  104. expect(openExternalLinkMock).toHaveBeenCalledWith(
  105. '/api/attachments/1?disposition=attachment',
  106. '_blank',
  107. 'calendar.ics',
  108. )
  109. })
  110. })