123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { ref } from 'vue'
- import { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
- import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
- import { convertToGraphQLId } from '#shared/graphql/utils.ts'
- import { useTicketArticleRetryMediaDownload } from '../composables/useTicketArticleRetryMediaDownload.ts'
- import {
- mockTicketArticleRetryMediaDownloadMutation,
- waitForTicketArticleRetryMediaDownloadMutationCalls,
- } from '../graphql/mutations/ticketArticleRetryMediaDownload.mocks.ts'
- describe('useTicketArticleRetryMediaDownload', () => {
- const testArticleId = ref(convertToGraphQLId('Ticket::Article', 1))
- const { loading, tryAgain } =
- useTicketArticleRetryMediaDownload(testArticleId)
- describe('tryAgain', () => {
- it('resolves on success', async () => {
- mockTicketArticleRetryMediaDownloadMutation({
- ticketArticleRetryMediaDownload: {
- success: true,
- },
- })
- expect(tryAgain()).resolves.toBeUndefined()
- const calls = await waitForTicketArticleRetryMediaDownloadMutationCalls()
- expect(calls.at(-1)?.variables).toEqual({
- articleId: testArticleId.value,
- })
- })
- it('rejects on error', async () => {
- mockTicketArticleRetryMediaDownloadMutation({
- ticketArticleRetryMediaDownload: {
- success: false,
- errors: [
- {
- message: 'Something went wrong',
- },
- ],
- },
- })
- expect(tryAgain()).rejects.toThrow()
- const calls = await waitForTicketArticleRetryMediaDownloadMutationCalls()
- expect(calls.at(-1)?.variables).toEqual({
- articleId: testArticleId.value,
- })
- })
- it('shows a notification on success', async () => {
- mockTicketArticleRetryMediaDownloadMutation({
- ticketArticleRetryMediaDownload: {
- success: true,
- },
- })
- await tryAgain()
- const { notify } = useNotifications()
- expect(notify).toHaveBeenCalledWith({
- id: 'media-download-success',
- message: 'Media download was successful.',
- type: NotificationTypes.Success,
- })
- })
- it('shows a notification on error', async () => {
- mockTicketArticleRetryMediaDownloadMutation({
- ticketArticleRetryMediaDownload: {
- success: false,
- errors: [
- {
- message: 'Something went wrong',
- },
- ],
- },
- })
- try {
- await tryAgain()
- } catch {
- // no-op
- }
- const { notify } = useNotifications()
- expect(notify).toHaveBeenCalledWith({
- id: 'media-download-failed',
- message: 'Media download failed. Please try again later.',
- type: NotificationTypes.Error,
- })
- })
- })
- describe('loading', () => {
- it('returns correct request state on success', async () => {
- expect(loading.value).toBe(false)
- mockTicketArticleRetryMediaDownloadMutation({
- ticketArticleRetryMediaDownload: {
- success: true,
- },
- })
- const promise = tryAgain()
- expect(loading.value).toBe(true)
- await promise
- expect(loading.value).toBe(false)
- })
- it('returns correct request state on error', async () => {
- expect(loading.value).toBe(false)
- mockTicketArticleRetryMediaDownloadMutation({
- ticketArticleRetryMediaDownload: {
- success: false,
- errors: [
- {
- message: 'Something went wrong',
- },
- ],
- },
- })
- const promise = tryAgain()
- expect(loading.value).toBe(true)
- try {
- await promise
- } catch {
- // no-op
- }
- expect(loading.value).toBe(false)
- })
- })
- })
|