|
| 1 | +import { toaster } from 'baseui/toast'; |
| 2 | +import { HttpResponse } from 'msw'; |
| 3 | + |
| 4 | +import { render, fireEvent, screen, act, waitFor } from '@/test-utils/rtl'; |
| 5 | + |
| 6 | +import { type HistoryEvent } from '@/__generated__/proto-ts/uber/cadence/api/v1/HistoryEvent'; |
| 7 | +import { type GetWorkflowHistoryResponse } from '@/route-handlers/get-workflow-history/get-workflow-history.types'; |
| 8 | + |
| 9 | +import type { Props as MSWMocksHandlersProps } from '../../../../test-utils/msw-mock-handlers/msw-mock-handlers.types'; |
| 10 | +import { completedActivityTaskEvents } from '../../__fixtures__/workflow-history-activity-events'; |
| 11 | +import WorkflowHistoryExportJsonButton from '../workflow-history-export-json-button'; |
| 12 | +import { type Props } from '../workflow-history-export-json-button.types'; |
| 13 | + |
| 14 | +jest.mock('@/utils/logger'); |
| 15 | +jest.mock('baseui/toast', () => ({ |
| 16 | + ...jest.requireActual('baseui/toast'), |
| 17 | + toaster: { |
| 18 | + negative: jest.fn(), |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +describe('WorkflowHistoryExportJsonButton', () => { |
| 23 | + const originalCreateObjectURL = window.URL.createObjectURL; |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + window.URL.createObjectURL = originalCreateObjectURL; |
| 28 | + }); |
| 29 | + |
| 30 | + it('should render the button with "Export JSON"', () => { |
| 31 | + setup({}); |
| 32 | + expect(screen.getByText('Export JSON')).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should show spinner when loading', async () => { |
| 36 | + const { getRequestResolver } = setup({ wait: true }); |
| 37 | + fireEvent.click(screen.getByText('Export JSON')); |
| 38 | + |
| 39 | + expect(screen.queryByRole('progressbar')).toBeInTheDocument(); |
| 40 | + await act(() => { |
| 41 | + const resolver = getRequestResolver(); |
| 42 | + resolver(); |
| 43 | + }); |
| 44 | + expect(screen.queryByRole('progressbar')).not.toBeInTheDocument(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should call request API and download JSON file', async () => { |
| 48 | + const createObjectURLMock: jest.Mock = jest.fn(); |
| 49 | + window.URL.createObjectURL = createObjectURLMock; |
| 50 | + |
| 51 | + setup({}); |
| 52 | + |
| 53 | + fireEvent.click(screen.getByText('Export JSON')); |
| 54 | + |
| 55 | + await waitFor(() => { |
| 56 | + expect(createObjectURLMock).toHaveBeenCalledWith(expect.any(Blob)); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle error and show toast', async () => { |
| 61 | + setup({ error: true }); |
| 62 | + fireEvent.click(screen.getByText('Export JSON')); |
| 63 | + |
| 64 | + await waitFor(() => { |
| 65 | + expect(toaster.negative).toHaveBeenCalledWith( |
| 66 | + 'Failed to export workflow history' |
| 67 | + ); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +function setup({ |
| 73 | + error, |
| 74 | + wait, |
| 75 | + ...overrides |
| 76 | +}: Partial<Props> & { error?: boolean; loading?: boolean; wait?: boolean }) { |
| 77 | + const defaultProps: Props = { |
| 78 | + domain: 'test-domain', |
| 79 | + cluster: 'test-cluster', |
| 80 | + workflowId: 'test-workflowId', |
| 81 | + runId: 'test-runId', |
| 82 | + }; |
| 83 | + const mockEvents: HistoryEvent[] = completedActivityTaskEvents; |
| 84 | + const totalEventsCount = mockEvents.length; |
| 85 | + let currentEventIndex = 0; |
| 86 | + let requestResolver = () => {}; |
| 87 | + const getRequestResolver = () => requestResolver; |
| 88 | + |
| 89 | + render(<WorkflowHistoryExportJsonButton {...defaultProps} {...overrides} />, { |
| 90 | + endpointsMocks: [ |
| 91 | + { |
| 92 | + path: '/api/domains/:domain/:cluster/workflows/:workflowId/:runId/history', |
| 93 | + httpMethod: 'GET', |
| 94 | + httpResolver: async () => { |
| 95 | + const index = currentEventIndex; |
| 96 | + currentEventIndex = currentEventIndex + 1; |
| 97 | + if (wait && currentEventIndex === 0) { |
| 98 | + await new Promise<void>((resolve) => { |
| 99 | + requestResolver = () => { |
| 100 | + resolve(); |
| 101 | + }; |
| 102 | + }); |
| 103 | + } |
| 104 | + if (error) |
| 105 | + return HttpResponse.json( |
| 106 | + { message: 'Failed to fetch workflow history' }, |
| 107 | + { status: 500 } |
| 108 | + ); |
| 109 | + |
| 110 | + return HttpResponse.json( |
| 111 | + { |
| 112 | + history: { |
| 113 | + events: [mockEvents[index]], |
| 114 | + }, |
| 115 | + archived: false, |
| 116 | + nextPageToken: index < totalEventsCount - 1 ? '' : `${index + 1}`, |
| 117 | + rawHistory: [], |
| 118 | + } satisfies GetWorkflowHistoryResponse, |
| 119 | + { status: 200 } |
| 120 | + ); |
| 121 | + }, |
| 122 | + }, |
| 123 | + ] as MSWMocksHandlersProps['endpointsMocks'], |
| 124 | + }); |
| 125 | + |
| 126 | + return { getRequestResolver }; |
| 127 | +} |
0 commit comments