From 0a5307819884eac70559e757d62894787c1ec378 Mon Sep 17 00:00:00 2001 From: Olivier Lance Date: Fri, 4 Mar 2022 14:49:48 +0100 Subject: [PATCH 1/2] feat(payments): adds fetch payment endpoint --- src/endpoints/payments.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/endpoints/payments.ts b/src/endpoints/payments.ts index c8c64ac..4a8e20d 100644 --- a/src/endpoints/payments.ts +++ b/src/endpoints/payments.ts @@ -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 { + const response = await this.request(`${PAYMENTS_PATH}/${paymentId}`).get() + return response.data + } } export default PaymentsEndpoint From ec94538b2cfa3781176043ff0ead69d238bae858 Mon Sep 17 00:00:00 2001 From: Olivier Lance Date: Fri, 4 Mar 2022 15:17:06 +0100 Subject: [PATCH 2/2] test(payments): adds tests for fetch payment endpoint --- test/endpoints/payments.test.ts | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/endpoints/payments.test.ts b/test/endpoints/payments.test.ts index 0011332..f7fb5ab 100644 --- a/test/endpoints/payments.test.ts +++ b/test/endpoints/payments.test.ts @@ -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' +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', () => { @@ -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() + }) + }) })