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

663/mk/standardize op client requests #971

Merged
merged 10 commits into from
Jan 13, 2023
49 changes: 41 additions & 8 deletions packages/open-payments/src/client/ilp-stream-connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { createILPStreamConnectionRoutes } from './ilp-stream-connection'
import { OpenAPI, HttpMethod, createOpenAPI } from 'openapi'
import path from 'path'
import { defaultAxiosInstance, silentLogger } from '../test/helpers'
import {
defaultAxiosInstance,
mockILPStreamConnection,
silentLogger
} from '../test/helpers'
import * as requestors from './requests'

jest.mock('./requests', () => {
return {
// https://jestjs.io/docs/jest-object#jestmockmodulename-factory-options
__esModule: true,
...jest.requireActual('./requests')
}
})

describe('ilp-stream-connection', (): void => {
let openApi: OpenAPI
Expand All @@ -15,14 +28,34 @@ describe('ilp-stream-connection', (): void => {
const axiosInstance = defaultAxiosInstance
const logger = silentLogger

describe('createILPStreamConnectionRoutes', (): void => {
test('calls createResponseValidator properly', async (): Promise<void> => {
jest.spyOn(openApi, 'createResponseValidator')
describe('routes', (): void => {
const ilpStreamConnection = mockILPStreamConnection()

describe('get', (): void => {
test('calls get method with correct validator', async (): Promise<void> => {
const mockResponseValidator = ({ path, method }) =>
path === '/connections/{id}' && method === HttpMethod.GET

jest
.spyOn(openApi, 'createResponseValidator')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.mockImplementation(mockResponseValidator as any)
Comment on lines +39 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check that the spy is called?

Copy link
Contributor Author

@mkurapov mkurapov Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already testing this (albeit indirectly): if the createResponseValidator doesn't get the proper path and method when its called, the test will fail based on the expectation of true on L57

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the true! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the easiest to see when first reading it, but that was the best solution I could come up with to test the correct validator was passed in


const getSpy = jest
.spyOn(requestors, 'get')
.mockResolvedValueOnce(ilpStreamConnection)

await createILPStreamConnectionRoutes({
openApi,
axiosInstance,
logger
}).get({ url: ilpStreamConnection.id })

createILPStreamConnectionRoutes({ axiosInstance, openApi, logger })
expect(openApi.createResponseValidator).toHaveBeenCalledWith({
path: '/connections/{id}',
method: HttpMethod.GET
expect(getSpy).toHaveBeenCalledWith(
{ axiosInstance, logger },
{ url: ilpStreamConnection.id },
true
)
})
})
})
Expand Down
Loading