// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors
// SPDX-License-Identifier: Apache-2.0
import { createReference } from '@medplum/core';
import { HomerServiceRequest, MockClient } from '@medplum/mock';
import { MedplumProvider } from '@medplum/react-hooks';
import { MemoryRouter } from 'react-router';
import { act, fireEvent, render, screen, waitFor } from '../test-utils/render';
import type { ServiceRequestTimelineProps } from './ServiceRequestTimeline';
import { ServiceRequestTimeline } from './ServiceRequestTimeline';
const medplum = new MockClient();
describe('ServiceRequestTimeline', () => {
async function setup(args: ServiceRequestTimelineProps): Promise<void> {
await act(async () => {
render(
<MemoryRouter>
<MedplumProvider medplum={medplum} navigate={jest.fn()}>
<ServiceRequestTimeline {...args} />
</MedplumProvider>
</MemoryRouter>
);
});
}
test('Renders reference', async () => {
await setup({ serviceRequest: createReference(HomerServiceRequest) });
await waitFor(() => screen.getAllByTestId('timeline-item'));
const items = screen.getAllByTestId('timeline-item');
expect(items).toBeDefined();
});
test('Renders resource', async () => {
await setup({ serviceRequest: HomerServiceRequest });
await waitFor(() => screen.getAllByTestId('timeline-item'));
const items = screen.getAllByTestId('timeline-item');
expect(items).toBeDefined();
});
test('Create comment', async () => {
await setup({ serviceRequest: HomerServiceRequest });
// Wait for initial load
await waitFor(() => screen.getAllByTestId('timeline-item'));
// Enter the comment text
await act(async () => {
fireEvent.change(screen.getByPlaceholderText('Add comment'), {
target: { value: 'Test comment' },
});
});
// Submit the form
await act(async () => {
fireEvent.submit(screen.getByTestId('timeline-form'), {
target: { text: 'Test comment' },
});
});
// Wait for new comment
await waitFor(() => screen.getAllByTestId('timeline-item'));
const items = screen.getAllByTestId('timeline-item');
expect(items).toBeDefined();
});
test('Upload media', async () => {
await setup({ serviceRequest: HomerServiceRequest });
// Wait for initial load
await waitFor(() => screen.getAllByTestId('timeline-item'));
// Upload the file
await act(async () => {
const files = [new File(['hello'], 'hello.txt', { type: 'text/plain' })];
fireEvent.change(screen.getByTestId('upload-file-input'), {
target: { files },
});
});
// Wait for new comment
await waitFor(() => screen.getAllByTestId('timeline-item'));
const items = screen.getAllByTestId('timeline-item');
expect(items).toBeDefined();
});
});