|
1 | 1 | /* eslint-disable @typescript-eslint/no-non-null-assertion */ |
2 | 2 |
|
3 | 3 | import * as uuid from "uuid"; |
4 | | -import { CreatePaymentResponse } from "../../src/model/domain/payment"; |
| 4 | +import { SdkResponse } from "../../src/model"; |
| 5 | +import { CreatePaymentResponse, PaymentErrorResponse } from "../../src/model/domain/payment"; |
| 6 | +import { CreatePaymentResult } from "../../src/model/domain/payment/definitions"; |
5 | 7 | import connectSdk, { config } from "./init"; |
6 | 8 |
|
7 | 9 | /** |
@@ -38,29 +40,39 @@ describe("Idempotence", () => { |
38 | 40 | } |
39 | 41 | }; |
40 | 42 |
|
| 43 | + function extractPaymentResult(response: SdkResponse): CreatePaymentResult { |
| 44 | + // For this test it doesn't matter if the response is successful or declined, |
| 45 | + // as long as idempotence is handled correctly |
| 46 | + if (response.status === 201) { |
| 47 | + return response.body as CreatePaymentResponse; |
| 48 | + } else { |
| 49 | + const responseBody = response.body as PaymentErrorResponse; |
| 50 | + return responseBody.paymentResult!; |
| 51 | + } |
| 52 | + } |
| 53 | + |
41 | 54 | connectSdk.payments.create(config.merchantId, body, paymentContext, (error, response) => { |
42 | 55 | expect(error).toBeNull(); |
43 | 56 |
|
44 | 57 | expect(response).not.toBeNull(); |
45 | | - expect(response!.status).toBe(201); |
46 | | - expect(response!.body).not.toBe(null); |
| 58 | + expect(response!.body).toBeTruthy(); |
47 | 59 |
|
48 | | - const responseBody = response!.body as CreatePaymentResponse; |
49 | | - expect(responseBody.payment).not.toBe(null); |
50 | | - expect(responseBody.payment!.id).not.toBe(null); |
| 60 | + const paymentResult = extractPaymentResult(response!); |
| 61 | + expect(paymentResult.payment).toBeTruthy(); |
| 62 | + expect(paymentResult.payment!.id).toBeTruthy(); |
51 | 63 | expect(paymentContext.idemPotence.key).toBe(idemPotenceKey); |
52 | 64 | expect(connectSdk.context.getIdempotenceRequestTimestamp()).toBeUndefined(); |
53 | 65 |
|
54 | 66 | connectSdk.payments.create(config.merchantId, body, paymentContext, (error2, response2) => { |
55 | 67 | expect(error2).toBeNull(); |
56 | 68 |
|
57 | 69 | expect(response2).not.toBeNull(); |
58 | | - expect(response2!.status).toBe(201); |
59 | | - expect(response2!.body).not.toBe(null); |
| 70 | + expect(response2!.status).toBe(response!.status); |
| 71 | + expect(response2!.body).toBeTruthy(); |
60 | 72 |
|
61 | | - const responseBody2 = response2!.body as CreatePaymentResponse; |
62 | | - expect(responseBody2.payment).not.toBe(null); |
63 | | - expect(responseBody2.payment!.id).toBe(responseBody.payment!.id); |
| 73 | + const paymentResult2 = extractPaymentResult(response2!); |
| 74 | + expect(paymentResult2.payment).toBeTruthy(); |
| 75 | + expect(paymentResult2.payment!.id).toBe(paymentResult.payment!.id); |
64 | 76 | expect(paymentContext.idemPotence.key).toBe(idemPotenceKey); |
65 | 77 | expect(connectSdk.context.getIdempotenceRequestTimestamp()).not.toBeUndefined(); |
66 | 78 | expect(connectSdk.context.getIdempotenceRequestTimestamp()).not.toBeNull(); |
|
0 commit comments