-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: restore lost PR #28 * fix: rebuild package lock * test: cache test * test: add tests for fhir client (stateless, axios, config) * test: config (stateless, fhir base, defaults) * test: fix config
- Loading branch information
Showing
4 changed files
with
261 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* © Copyright Outburn Ltd. 2022-2023 All Rights Reserved | ||
* Project name: FUME | ||
*/ | ||
|
||
import { test } from '@jest/globals'; | ||
|
||
import config from './config'; | ||
|
||
const serverDefaults = { | ||
EXCLUDE_FHIR_PACKAGES: '', | ||
FHIR_PACKAGES: '[email protected],[email protected],[email protected],laniado.test.fhir.r4', | ||
FHIR_SERVER_AUTH_TYPE: 'NONE', | ||
FHIR_SERVER_BASE: 'http://hapi-fhir.outburn.co.il/fhir', | ||
FHIR_SERVER_PW: '', | ||
FHIR_SERVER_TIMEOUT: 10000, | ||
FHIR_SERVER_UN: '', | ||
FHIR_VERSION: '4.0.1', | ||
SEARCH_BUNDLE_PAGE_SIZE: 20, | ||
SERVER_PORT: 42420, | ||
SERVER_STATELESS: false | ||
}; | ||
|
||
jest.mock('./serverConfig', () => (serverDefaults)); | ||
|
||
describe('setServerConfig', () => { | ||
test('Uses defaults', async () => { | ||
config.setServerConfig({}); | ||
expect(config.getServerConfig()).toEqual({ | ||
...serverDefaults, | ||
FHIR_SERVER_BASE: '', | ||
SERVER_STATELESS: true | ||
}); | ||
}); | ||
|
||
test('Uses defaults, with FHIR_SERVER_BASE, sets stateless to false', async () => { | ||
config.setServerConfig({ | ||
FHIR_SERVER_BASE: 'http://hapi-fhir.outburn.co.il/fhir-test ' | ||
}); | ||
expect(config.getServerConfig()).toEqual({ | ||
EXCLUDE_FHIR_PACKAGES: '', | ||
FHIR_PACKAGES: '[email protected],[email protected],[email protected],laniado.test.fhir.r4', | ||
FHIR_SERVER_AUTH_TYPE: 'NONE', | ||
FHIR_SERVER_BASE: 'http://hapi-fhir.outburn.co.il/fhir-test', | ||
FHIR_SERVER_PW: '', | ||
FHIR_SERVER_TIMEOUT: 10000, | ||
FHIR_SERVER_UN: '', | ||
FHIR_VERSION: '4.0.1', | ||
SEARCH_BUNDLE_PAGE_SIZE: 20, | ||
SERVER_PORT: 42420, | ||
SERVER_STATELESS: false | ||
}); | ||
}); | ||
|
||
test('Uses defaults, without FHIR_SERVER_BASE, sets stateless to true', async () => { | ||
config.setServerConfig({ | ||
SERVER_STATELESS: false | ||
}); | ||
expect(config.getServerConfig()).toEqual({ | ||
EXCLUDE_FHIR_PACKAGES: '', | ||
FHIR_PACKAGES: '[email protected],[email protected],[email protected],laniado.test.fhir.r4', | ||
FHIR_SERVER_AUTH_TYPE: 'NONE', | ||
FHIR_SERVER_BASE: '', | ||
FHIR_SERVER_PW: '', | ||
FHIR_SERVER_TIMEOUT: 10000, | ||
FHIR_SERVER_UN: '', | ||
FHIR_VERSION: '4.0.1', | ||
SEARCH_BUNDLE_PAGE_SIZE: 20, | ||
SERVER_PORT: 42420, | ||
SERVER_STATELESS: true | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
|
||
import { test } from '@jest/globals'; | ||
import mockAxios from 'jest-mock-axios'; | ||
|
||
import config from '../../config'; | ||
import { FhirClient } from './client'; | ||
|
||
const mockConfig = { | ||
FHIR_SERVER_BASE: 'test.com', | ||
FHIR_SERVER_TIMEOUT: 1000, | ||
SERVER_PORT: 111, | ||
FHIR_SERVER_AUTH_TYPE: '', | ||
FHIR_SERVER_UN: '', | ||
FHIR_SERVER_PW: '', | ||
SERVER_STATELESS: false, | ||
FHIR_VERSION: '4.0.1', | ||
SEARCH_BUNDLE_PAGE_SIZE: 200, | ||
FHIR_PACKAGES: '', | ||
EXCLUDE_FHIR_PACKAGES: '' | ||
}; | ||
|
||
describe('FhirClient', () => { | ||
let mockAxiosInstance: jest.Mocked<any>; | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
mockAxios.reset(); | ||
}); | ||
|
||
beforeEach(() => { | ||
mockAxiosInstance = { | ||
get: jest.fn(), | ||
post: jest.fn(), | ||
put: jest.fn(), | ||
delete: jest.fn() | ||
|
||
} as any; | ||
mockAxios.create.mockReturnValue(mockAxiosInstance); | ||
}); | ||
|
||
test('constructor uses config, no auth, stateless = false', async () => { | ||
config.setServerConfig(mockConfig); | ||
/* eslint-disable no-new */ | ||
new FhirClient(); | ||
|
||
expect(mockAxios.create).toHaveBeenCalledWith({ | ||
baseURL: mockConfig.FHIR_SERVER_BASE, | ||
timeout: mockConfig.FHIR_SERVER_TIMEOUT, | ||
headers: { | ||
Accept: 'application/fhir+json;fhirVersion=4.0', | ||
'Content-Type': 'application/fhir+json;fhirVersion=4.0', | ||
Authorization: undefined | ||
} | ||
}); | ||
}); | ||
|
||
test('constructor uses config, with BASIC auth, stateless = false', async () => { | ||
config.setServerConfig({ | ||
...mockConfig, | ||
FHIR_SERVER_AUTH_TYPE: 'BASIC', | ||
FHIR_SERVER_UN: 'user', | ||
FHIR_SERVER_PW: 'pw' | ||
}); | ||
/* eslint-disable no-new */ | ||
new FhirClient(); | ||
|
||
expect(mockAxios.create).toHaveBeenCalledWith({ | ||
baseURL: mockConfig.FHIR_SERVER_BASE, | ||
timeout: mockConfig.FHIR_SERVER_TIMEOUT, | ||
headers: { | ||
Accept: 'application/fhir+json;fhirVersion=4.0', | ||
'Content-Type': 'application/fhir+json;fhirVersion=4.0', | ||
Authorization: 'Basic dXNlcjpwdw==' | ||
} | ||
}); | ||
}); | ||
|
||
test('constructor uses config, no auth, stateless = false', async () => { | ||
config.setServerConfig({ | ||
...mockConfig, | ||
SERVER_STATELESS: true | ||
}); | ||
|
||
/* eslint-disable no-new */ | ||
new FhirClient(); | ||
|
||
expect(mockAxios.create).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('read returns empty in stateless mode', async () => { | ||
config.setServerConfig({ | ||
...mockConfig, | ||
SERVER_STATELESS: true | ||
}); | ||
const client = new FhirClient(); | ||
const url = 'test'; | ||
const res = await client.read(url); | ||
expect(res).toBeUndefined(); | ||
expect(mockAxios.get).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('read returns response data', async () => { | ||
config.setServerConfig(mockConfig); | ||
const client = new FhirClient(); | ||
const url = 'test-url'; | ||
mockAxiosInstance.get.mockResolvedValue({ data: 'test' }); | ||
const response = await client.read(url); | ||
expect(response).toBe('test'); | ||
expect(mockAxiosInstance.get).toHaveBeenCalledWith(url); | ||
}); | ||
|
||
test('search returns empty in stateless mode', async () => { | ||
config.setServerConfig({ | ||
...mockConfig, | ||
SERVER_STATELESS: true | ||
}); | ||
const client = new FhirClient(); | ||
const url = 'test'; | ||
const res = await client.search(url); | ||
expect(res).toBeUndefined(); | ||
expect(mockAxios.get).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('search returns response data', async () => { | ||
config.setServerConfig({ | ||
...mockConfig, | ||
SEARCH_BUNDLE_PAGE_SIZE: 45 | ||
}); | ||
const client = new FhirClient(); | ||
const url = 'test-url'; | ||
mockAxiosInstance.get.mockResolvedValue({ data: 'test' }); | ||
const response = await client.search(url, { test: 'test-param' }); | ||
expect(response).toBe('test'); | ||
|
||
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/test-url', { params: { _count: 45, test: 'test-param' } }); | ||
}); | ||
|
||
test('create throws error in community', async () => { | ||
config.setServerConfig({ | ||
...mockConfig, | ||
SERVER_STATELESS: true | ||
}); | ||
const client = new FhirClient(); | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
expect(client.create({}, 'test')).rejects.toThrow('Method not implemented.'); | ||
}); | ||
|
||
test('update throws error in community', async () => { | ||
config.setServerConfig({ | ||
...mockConfig, | ||
SERVER_STATELESS: true | ||
}); | ||
const client = new FhirClient(); | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
expect(client.update('test', 'test', {})).rejects.toThrow('Method not implemented.'); | ||
}); | ||
|
||
test('simpleDelete throws error in community', async () => { | ||
config.setServerConfig({ | ||
...mockConfig, | ||
SERVER_STATELESS: true | ||
}); | ||
const client = new FhirClient(); | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
expect(client.simpleDelete('test', 'test')).rejects.toThrow('Method not implemented.'); | ||
}); | ||
}); |