Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): correct loading behavior, add MedplumClient lifecycle events #4701

Merged
merged 4 commits into from
Jun 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test(mock): test mockFetchOverride
  • Loading branch information
ThatOneBro committed Jun 20, 2024
commit 828e62dcaf08801d3ed9e5c217c256c09ba32db2
63 changes: 62 additions & 1 deletion packages/mock/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ClientStorage,
ContentType,
LoginState,
MemoryStorage,
MockAsyncClientStorage,
NewPatientRequest,
NewProjectRequest,
Expand All @@ -12,8 +13,10 @@ import {
getReferenceString,
indexSearchParameterBundle,
indexStructureDefinitionBundle,
sleep,
} from '@medplum/core';
import { readJson } from '@medplum/definitions';
import { FhirRouter, MemoryRepository } from '@medplum/fhir-router';
import {
Agent,
Bot,
Expand All @@ -26,7 +29,7 @@ import {
} from '@medplum/fhirtypes';
import { randomUUID, webcrypto } from 'node:crypto';
import { TextEncoder } from 'node:util';
import { MockClient } from './client';
import { MockClient, MockFetchClient } from './client';
import { DrAliceSmith, DrAliceSmithSchedule, HomerSimpson } from './mocks';
import { MockSubscriptionManager } from './subscription-manager';

Expand Down Expand Up @@ -268,6 +271,64 @@ describe('MockClient', () => {
expect(console.log).toHaveBeenCalled();
});

test('mockFetchOverride -- Missing one of router, repo, or client throws', () => {
const router = new FhirRouter();
const repo = new MemoryRepository();
const client = new MockFetchClient(router, repo, 'https://example.com/');
expect(
() =>
new MockClient({
// @ts-expect-error Missing router
mockFetchOverride: { repo, client },
})
).toThrow('mockFetchOverride must specify all fields: client, repo, router');
expect(
() =>
new MockClient({
// @ts-expect-error Missing repo
mockFetchOverride: { repo, client },
})
).toThrow('mockFetchOverride must specify all fields: client, repo, router');
expect(
() =>
new MockClient({
// @ts-expect-error Missing client
mockFetchOverride: { router, repo },
})
).toThrow('mockFetchOverride must specify all fields: client, repo, router');
});

test('mockFetchOverride -- Can spy on passed-in fetch', async () => {
const baseUrl = 'https://example.com/';

const router = new FhirRouter();
const repo = new MemoryRepository();
const client = new MockFetchClient(router, repo, baseUrl);
const fetchClientSpy = jest.spyOn(client, 'mockFetch');

const storage = new ClientStorage(new MemoryStorage());
storage.setObject('activeLogin', {
accessToken:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJsb2dpbl9pZCI6InRlc3RpbmcxMjMifQ.lJGCbp2taTarRbamxaKFsTR_VRVgzvttKMmI5uFQSM0',
refreshToken: '456',
profile: {
reference: 'Practitioner/123',
},
project: {
reference: 'Project/123',
},
});

const mockClient = new MockClient({
storage,
mockFetchOverride: { router, repo, client },
});

await sleep(0);
expect(mockClient).toBeDefined();
expect(fetchClientSpy).toHaveBeenCalledWith(`${baseUrl}auth/me`, expect.objectContaining({ method: 'GET' }));
});

test('Search', async () => {
const client = new MockClient();
const result = await client.search('Patient', 'name=Simpson');
Expand Down
Loading