// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors
// SPDX-License-Identifier: Apache-2.0
import { createReference, locationUtils } from '@medplum/core';
import { HomerEncounter, HomerSimpson, MockClient } from '@medplum/mock';
import { MedplumProvider } from '@medplum/react-hooks';
import type { ReactNode } from 'react';
import { MemoryRouter } from 'react-router';
import { act, fireEvent, render, screen, waitFor } from '../test-utils/render';
import { SmartAppLaunchLink } from './SmartAppLaunchLink';
const medplum = new MockClient();
describe('SmartAppLaunchLink', () => {
function setup(children: ReactNode): void {
render(
<MemoryRouter>
<MedplumProvider medplum={medplum}>{children}</MedplumProvider>
</MemoryRouter>
);
}
test('Happy path', async () => {
// const assignSpy = jest.spyOn(locationUtils, 'assign').mockImplementation(() => undefined);
const mockAssign = jest.fn();
locationUtils.assign = mockAssign;
setup(
<SmartAppLaunchLink
client={{ resourceType: 'ClientApplication', launchUri: 'https://example.com' }}
patient={createReference(HomerSimpson)}
encounter={createReference(HomerEncounter)}
>
My SmartAppLaunchLink
</SmartAppLaunchLink>
);
expect(screen.getByText('My SmartAppLaunchLink')).toBeInTheDocument();
await act(async () => {
fireEvent.click(screen.getByText('My SmartAppLaunchLink'));
});
await waitFor(() => expect(mockAssign).toHaveBeenCalled());
expect(mockAssign).toHaveBeenCalled();
const url = mockAssign.mock.calls[0][0];
expect(url).toContain('https://example.com');
expect(url).toContain('launch=');
expect(url).toContain('iss=');
});
});