CommonCalendarPreviewFlyout.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. form: true,
  35. global: {
  36. stubs: {
  37. teleport: true,
  38. },
  39. },
  40. })
  41. await waitForNextTick()
  42. return result
  43. }
  44. describe('TicketSidebarSharedDraftFlyout.vue', () => {
  45. beforeAll(() => {
  46. mockApplicationConfig({
  47. api_path: '/api',
  48. })
  49. mockCalendarIcsFileEventsQuery({
  50. calendarIcsFileEvents: [
  51. {
  52. __typename: 'CalendarIcsFileEvent',
  53. title: 'event 1',
  54. location: 'location 1',
  55. startDate: '2024-08-22T12:00:00:00+00:00',
  56. endDate: '2024-08-22T13:00:00:00+00:00',
  57. },
  58. {
  59. __typename: 'CalendarIcsFileEvent',
  60. title: 'event 2',
  61. location: 'location 2',
  62. startDate: '2024-08-22T14:00:00:00+00:00',
  63. endDate: '2024-08-22T15:00:00:00+00:00',
  64. },
  65. {
  66. __typename: 'CalendarIcsFileEvent',
  67. title: 'event 3',
  68. location: 'location 3',
  69. startDate: '2024-08-22T16:00:00:00+00:00',
  70. endDate: '2024-08-22T17:00:00:00+00:00',
  71. },
  72. ],
  73. })
  74. })
  75. it('renders calendar preview', async () => {
  76. const wrapper = await renderCommonCalendarPreviewFlyout()
  77. expect(
  78. wrapper.getByRole('complementary', {
  79. name: 'Preview Calendar',
  80. }),
  81. ).toBeInTheDocument()
  82. expect(
  83. wrapper.getByRole('heading', { name: 'Preview Calendar' }),
  84. ).toBeInTheDocument()
  85. expect(wrapper.getByText('event 1')).toBeInTheDocument()
  86. expect(wrapper.getByText('location 1')).toBeInTheDocument()
  87. expect(wrapper.getByText('2024-08-22 12:00')).toBeInTheDocument()
  88. expect(wrapper.getByText('2024-08-22 13:00')).toBeInTheDocument()
  89. expect(wrapper.getByText('event 2')).toBeInTheDocument()
  90. expect(wrapper.getByText('location 2')).toBeInTheDocument()
  91. expect(wrapper.getByText('2024-08-22 14:00')).toBeInTheDocument()
  92. expect(wrapper.getByText('2024-08-22 15:00')).toBeInTheDocument()
  93. expect(wrapper.getByText('event 3')).toBeInTheDocument()
  94. expect(wrapper.getByText('location 3')).toBeInTheDocument()
  95. expect(wrapper.getByText('2024-08-22 16:00')).toBeInTheDocument()
  96. expect(wrapper.getByText('2024-08-22 17:00')).toBeInTheDocument()
  97. })
  98. it('supports downloading calendar', async () => {
  99. const wrapper = await renderCommonCalendarPreviewFlyout()
  100. await wrapper.events.click(
  101. wrapper.getByRole('button', {
  102. name: 'Download',
  103. }),
  104. )
  105. expect(openExternalLinkMock).toHaveBeenCalledWith(
  106. '/api/attachments/1?disposition=attachment',
  107. '_blank',
  108. 'calendar.ics',
  109. )
  110. })
  111. })