-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1233 from nickgros/PORTALS-2923b
- Loading branch information
Showing
13 changed files
with
314 additions
and
47 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
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,102 @@ | ||
import { http, HttpResponse } from 'msw' | ||
import { ErrorResponse } from './generated/index' | ||
import { FileEntity } from './generated/models/FileEntity' | ||
import { server } from './mocks/node' | ||
import { SynapseClient } from './SynapseClient' | ||
import { fetchResponseWithExponentialTimeout } from './util/fetchWithExponentialTimeout' | ||
import { SynapseClientError } from './util/SynapseClientError' | ||
|
||
vi.mock('./util/fetchWithExponentialTimeout', async importOriginal => { | ||
const original = await importOriginal< | ||
typeof import('./util/fetchWithExponentialTimeout') | ||
>() | ||
return { | ||
...original, | ||
fetchResponseWithExponentialTimeout: vi | ||
.fn() | ||
.mockImplementation(original.fetchResponseWithExponentialTimeout), | ||
} | ||
}) | ||
|
||
const fetchResponseWithExponentialTimeoutSpy = vi.mocked( | ||
fetchResponseWithExponentialTimeout, | ||
) | ||
|
||
describe('SynapseClient', () => { | ||
it('Should use fetchWithExponentialTimeout as the default fetchApi', async () => { | ||
// Set up mock service worker | ||
const expectedResponse: FileEntity = { | ||
id: 'syn123', | ||
concreteType: 'org.sagebionetworks.repo.model.FileEntity', | ||
} | ||
server.use( | ||
http.get('https://repo-prod.prod.sagebase.org/repo/v1/entity/:id', () => { | ||
return HttpResponse.json(expectedResponse) | ||
}), | ||
) | ||
|
||
const client = new SynapseClient() | ||
const actual = await client.entityServicesClient.getRepoV1EntityId({ | ||
id: 'syn123', | ||
}) | ||
|
||
expect(actual).toEqual(expectedResponse) | ||
|
||
// verify fetchApi is used | ||
expect(fetchResponseWithExponentialTimeoutSpy).toHaveBeenCalled() | ||
}) | ||
|
||
it('allows overriding the base path', async () => { | ||
const mockBasePath = 'https://repo-mock.mock.sagebase.org' | ||
// Set up mock service worker | ||
const expectedResponse: FileEntity = { | ||
id: 'syn456', | ||
concreteType: 'org.sagebionetworks.repo.model.FileEntity', | ||
} | ||
server.use( | ||
http.get(`${mockBasePath}/repo/v1/entity/:id`, () => { | ||
return HttpResponse.json(expectedResponse) | ||
}), | ||
) | ||
|
||
const client = new SynapseClient({ basePath: mockBasePath }) | ||
const actual = await client.entityServicesClient.getRepoV1EntityId({ | ||
id: 'syn456', | ||
}) | ||
|
||
expect(actual).toEqual(expectedResponse) | ||
|
||
// verify fetchApi is used | ||
expect(fetchResponseWithExponentialTimeoutSpy).toHaveBeenCalled() | ||
}) | ||
|
||
it('Should throw SynapseClientError on a 400-level error', async () => { | ||
const response: ErrorResponse = { | ||
reason: 'Not found!', | ||
concreteType: 'org.sagebionetworks.repo.model.ErrorResponse', | ||
} | ||
server.use( | ||
http.get('https://repo-prod.prod.sagebase.org/repo/v1/entity/:id', () => { | ||
return HttpResponse.json(response, { status: 404 }) | ||
}), | ||
) | ||
const client = new SynapseClient() | ||
|
||
let error: SynapseClientError = new SynapseClientError(500, '', '') | ||
try { | ||
await client.entityServicesClient.getRepoV1EntityId({ | ||
id: 'syn123', | ||
}) | ||
} catch (e) { | ||
error = e as SynapseClientError | ||
} | ||
|
||
expect(error).toBeInstanceOf(SynapseClientError) | ||
expect(error.status).toBe(404) | ||
expect(error.reason).toBe('Not found!') | ||
expect(error.errorResponse).toEqual(response) | ||
expect(error.url).toBe( | ||
'https://repo-prod.prod.sagebase.org/repo/v1/entity/syn123', | ||
) | ||
}) | ||
}) |
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,3 @@ | ||
export const handlers = [ | ||
// shared handlers go here | ||
] |
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,4 @@ | ||
import { setupServer } from 'msw/node' | ||
import { handlers } from './handlers' | ||
|
||
export const server = setupServer(...handlers) |
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,6 @@ | ||
import { beforeAll, afterEach, afterAll } from 'vitest' | ||
import { server } from './mocks/node' | ||
|
||
beforeAll(() => server.listen()) | ||
afterEach(() => server.resetHandlers()) | ||
afterAll(() => server.close()) |
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import { ConfigBuilder } from 'vite-config' | ||
|
||
const config = new ConfigBuilder().setIncludeVitestConfig(true).build() | ||
const config = new ConfigBuilder() | ||
.setIncludeVitestConfig(true) | ||
.setConfigOverrides({ | ||
test: { | ||
setupFiles: ['./src/vitest.setup.ts'], | ||
}, | ||
}) | ||
.build() | ||
|
||
export default config |
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
Oops, something went wrong.