useTicketArticleRetryMediaDownload.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ref } from 'vue'
  3. import { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
  4. import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
  5. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  6. import { useTicketArticleRetryMediaDownload } from '../composables/useTicketArticleRetryMediaDownload.ts'
  7. import {
  8. mockTicketArticleRetryMediaDownloadMutation,
  9. waitForTicketArticleRetryMediaDownloadMutationCalls,
  10. } from '../graphql/mutations/ticketArticleRetryMediaDownload.mocks.ts'
  11. describe('useTicketArticleRetryMediaDownload', () => {
  12. const testArticleId = ref(convertToGraphQLId('Ticket::Article', 1))
  13. const { loading, tryAgain } =
  14. useTicketArticleRetryMediaDownload(testArticleId)
  15. describe('tryAgain', () => {
  16. it('resolves on success', async () => {
  17. mockTicketArticleRetryMediaDownloadMutation({
  18. ticketArticleRetryMediaDownload: {
  19. success: true,
  20. },
  21. })
  22. expect(tryAgain()).resolves.toBeUndefined()
  23. const calls = await waitForTicketArticleRetryMediaDownloadMutationCalls()
  24. expect(calls.at(-1)?.variables).toEqual({
  25. articleId: testArticleId.value,
  26. })
  27. })
  28. it('rejects on error', async () => {
  29. mockTicketArticleRetryMediaDownloadMutation({
  30. ticketArticleRetryMediaDownload: {
  31. success: false,
  32. errors: [
  33. {
  34. message: 'Something went wrong',
  35. },
  36. ],
  37. },
  38. })
  39. expect(tryAgain()).rejects.toThrow()
  40. const calls = await waitForTicketArticleRetryMediaDownloadMutationCalls()
  41. expect(calls.at(-1)?.variables).toEqual({
  42. articleId: testArticleId.value,
  43. })
  44. })
  45. it('shows a notification on success', async () => {
  46. mockTicketArticleRetryMediaDownloadMutation({
  47. ticketArticleRetryMediaDownload: {
  48. success: true,
  49. },
  50. })
  51. await tryAgain()
  52. const { notify } = useNotifications()
  53. expect(notify).toHaveBeenCalledWith({
  54. id: 'media-download-success',
  55. message: 'Media download was successful.',
  56. type: NotificationTypes.Success,
  57. })
  58. })
  59. it('shows a notification on error', async () => {
  60. mockTicketArticleRetryMediaDownloadMutation({
  61. ticketArticleRetryMediaDownload: {
  62. success: false,
  63. errors: [
  64. {
  65. message: 'Something went wrong',
  66. },
  67. ],
  68. },
  69. })
  70. try {
  71. await tryAgain()
  72. } catch {
  73. // no-op
  74. }
  75. const { notify } = useNotifications()
  76. expect(notify).toHaveBeenCalledWith({
  77. id: 'media-download-failed',
  78. message: 'Media download failed. Please try again later.',
  79. type: NotificationTypes.Error,
  80. })
  81. })
  82. })
  83. describe('loading', () => {
  84. it('returns correct request state on success', async () => {
  85. expect(loading.value).toBe(false)
  86. mockTicketArticleRetryMediaDownloadMutation({
  87. ticketArticleRetryMediaDownload: {
  88. success: true,
  89. },
  90. })
  91. const promise = tryAgain()
  92. expect(loading.value).toBe(true)
  93. await promise
  94. expect(loading.value).toBe(false)
  95. })
  96. it('returns correct request state on error', async () => {
  97. expect(loading.value).toBe(false)
  98. mockTicketArticleRetryMediaDownloadMutation({
  99. ticketArticleRetryMediaDownload: {
  100. success: false,
  101. errors: [
  102. {
  103. message: 'Something went wrong',
  104. },
  105. ],
  106. },
  107. })
  108. const promise = tryAgain()
  109. expect(loading.value).toBe(true)
  110. try {
  111. await promise
  112. } catch {
  113. // no-op
  114. }
  115. expect(loading.value).toBe(false)
  116. })
  117. })
  118. })