Skip to content

Commit

Permalink
Example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codyebberson committed May 13, 2022
1 parent a0fc6e9 commit 516185b
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 15 deletions.
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
"description": "Medplum Demo Bots",
"scripts": {
"build": "tsc",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"deploy:hello-world": "medplum deploy-bot dist/hello-world.js e54fa800-02ab-41be-8d48-8c027dd85f0c"
},
"author": "Medplum <[email protected]>",
"license": "Apache-2.0",
"devDependencies": {
"@medplum/cli": "^0.9.4",
"@medplum/core": "^0.9.4",
"@medplum/fhirtypes": "^0.9.4",
"@types/node": "^17.0.31",
"eslint": "8.14.0",
"@medplum/cli": "0.9.5",
"@medplum/core": "0.9.5",
"@medplum/fhirtypes": "0.9.5",
"@medplum/mock": "0.9.5",
"@types/node": "17.0.33",
"c8": "7.11.2",
"eslint": "8.15.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.0.0",
"node-fetch": "^3.2.4",
"node-fetch": "3.2.4",
"prettier": "2.6.2",
"typescript": "^4.6.4"
"typescript": "4.6.4",
"vitest": "0.12.6"
},
"eslintConfig": {
"parser": "@typescript-eslint/parser",
Expand Down
11 changes: 11 additions & 0 deletions src/hello-world.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MockClient } from '@medplum/mock';
import { expect, test } from 'vitest';
import { handler } from './hello-world';

const medplum = new MockClient();

test('Hello world', () => {
const input = 'Hello';
const contentType = 'text/plain';
expect(handler(medplum, { input, contentType })).resolves.toBe(true);
});
8 changes: 4 additions & 4 deletions src/hello-world.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MedplumClient } from '@medplum/core';
import { Resource } from '@medplum/fhirtypes';
import { BotEvent, MedplumClient } from '@medplum/core';

export async function handler(medplum: MedplumClient, input: Resource): Promise<any> {
export async function handler(medplum: MedplumClient, event: BotEvent): Promise<any> {
console.log('Hello world');
console.log(JSON.stringify(input, undefined, 2));
console.log(JSON.stringify(event, undefined, 2));
return true;
}
48 changes: 48 additions & 0 deletions src/patient-intake.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { QuestionnaireResponse } from '@medplum/fhirtypes';
import { MockClient } from '@medplum/mock';
import { expect, test, vi } from 'vitest';
import { handler } from './patient-intake';

const medplum = new MockClient();
const contentType = 'application/fhir+json';

test('Success', async () => {
const input: QuestionnaireResponse = {
resourceType: 'QuestionnaireResponse',
item: [
{ linkId: 'firstName', answer: [{ valueString: 'John' }] },
{ linkId: 'lastName', answer: [{ valueString: 'Smith' }] },
{ linkId: 'comment', answer: [{ valueString: 'Please review urgently' }] },
],
};
const result = await handler(medplum, { input, contentType });
expect(result).toBe(true);
});

test('Missing first name', async () => {
console.log = vi.fn();
const input: QuestionnaireResponse = {
resourceType: 'QuestionnaireResponse',
item: [
{ linkId: 'firstName', answer: [{ valueString: '' }] },
{ linkId: 'lastName', answer: [{ valueString: 'Smith' }] },
],
};
const result = await handler(medplum, { input, contentType });
expect(result).toBe(false);
expect(console.log).toBeCalledWith('Missing first name');
});

test('Missing last name', async () => {
console.log = vi.fn();
const input: QuestionnaireResponse = {
resourceType: 'QuestionnaireResponse',
item: [
{ linkId: 'firstName', answer: [{ valueString: 'John' }] },
{ linkId: 'lastName', answer: [{ valueString: '' }] },
],
};
const result = await handler(medplum, { input, contentType });
expect(result).toBe(false);
expect(console.log).toBeCalledWith('Missing last name');
});
11 changes: 7 additions & 4 deletions src/patient-intake.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { createReference, getQuestionnaireAnswers, MedplumClient } from '@medplum/core';
import { BotEvent, createReference, getQuestionnaireAnswers, MedplumClient } from '@medplum/core';
import { Communication, Patient, QuestionnaireResponse, ServiceRequest } from '@medplum/fhirtypes';

export async function handler(medplum: MedplumClient, input: QuestionnaireResponse): Promise<any> {
export async function handler(medplum: MedplumClient, event: BotEvent): Promise<any> {
// Get all of the answers from the questionnaire response
const answers = getQuestionnaireAnswers(input);
const answers = getQuestionnaireAnswers(event.input as QuestionnaireResponse);

// Some quick data validation
const firstName = answers['firstName']?.valueString;
if (!firstName) {
console.log('Missing first name');
return;
return false;
}

const lastName = answers['lastName']?.valueString;
if (!lastName) {
console.log('Missing last name');
return false;
}

// Create a patient (FHIR Patient)
Expand Down Expand Up @@ -50,4 +51,6 @@ export async function handler(medplum: MedplumClient, input: QuestionnaireRespon
],
});
}

return true;
}
7 changes: 7 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
globals: true,
},
});

0 comments on commit 516185b

Please sign in to comment.