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

Feat/fetch payment #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/endpoints/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class PaymentsEndpoint extends Endpoint {
const response = await this.request(PAYMENTS_PATH).setBody(data).post()
return response.data
}

async fetch(paymentId: string): Promise<Payment> {
const response = await this.request(`${PAYMENTS_PATH}/${paymentId}`).get()
return response.data
}
}

export default PaymentsEndpoint
61 changes: 61 additions & 0 deletions test/endpoints/payments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import { expectTypeOf } from 'expect-type'
import { PaymentsEndpoint } from '@/endpoints/payments'
import Context from '@/context'
import Eligibility from '@/entities/eligibility'
import Request from '../../src/http/request'
import * as RequestModule from '../../src/http/request'
import Response from '../../src/http/response'
import MerchantIdCredentials from '../../src/credentials/MerchantIdCredentials'
import { ApiMode } from '../../src'
Comment on lines +6 to +10
Copy link
Member

Choose a reason for hiding this comment

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

you could use absolute imports

import { AxiosResponse } from 'axios'

const FAKE_CONTEXT = new Context(new MerchantIdCredentials('fake-id'), {
apiRoot: { test: 'https://api.sandbox.getalma.eu', live: '' },
mode: ApiMode.TEST,
logger: console,
})

describe('Payments endpoint', () => {
describe('eligibility method', () => {
Expand Down Expand Up @@ -45,4 +57,53 @@ describe('Payments endpoint', () => {
spy.mockRestore()
})
})

describe('fetch method', () => {
it('exists', () => {
expect(PaymentsEndpoint.prototype.fetch).toBeDefined()
})

it('calls the API with provided payment ID', async () => {
const paymentId = 'A_PAYMENT_ID'

const newRequest = jest.spyOn(PaymentsEndpoint.prototype, 'request')
const get = jest
.spyOn(Request.prototype, 'get')
.mockImplementation(async () => new Response({} as AxiosResponse))

const endpoint = new PaymentsEndpoint(FAKE_CONTEXT)
await endpoint.fetch(paymentId)

expect(newRequest).toHaveBeenCalledWith(`/v1/payments/${paymentId}`)
expect(get).toHaveBeenCalled()

get.mockRestore()
newRequest.mockRestore()
})

it('returns the payment data', async () => {
const paymentData = {
id: 'A_PAYMENT_ID',
url: 'https://checkout.sandbox.getalma.eu/A_PAYMENT_ID',
}

const get = jest.spyOn(Request.prototype, 'get').mockImplementation(
async () =>
new Response({
data: paymentData,
status: 200,
statusText: 'OK',
headers: { 'Content-type': 'application/json' },
config: {},
})
)

const endpoint = new PaymentsEndpoint(FAKE_CONTEXT)
const receivedData = await endpoint.fetch(paymentData.id)

expect(receivedData).toEqual(paymentData)

get.mockRestore()
})
})
})