Skip to content

Commit

Permalink
Added tests for mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
mclintprojects committed Aug 16, 2024
1 parent 847d1f4 commit 3803bad
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ class Cashramp {
query: INITIATE_HOSTED_PAYMENT,
variables: {
amount,
currency,
currency: currency || "usd",
countryCode,
email,
paymentType,
reference,
redirectUrl,
firstName,
lastName,
redirectUrl,
email,
},
});
}
Expand Down Expand Up @@ -243,7 +243,7 @@ class Cashramp {
* Withdraw from your balance to an onchain wallet address
* @param {object} options
* @param {string} options.address The wallet address to withdraw to
* @param {string} options.amountUsd The amount to withdraw to the address provided
* @param {number} options.amountUsd The amount to withdraw to the address provided
* @returns {CashrampResponse}
*/
async withdrawOnchain({ address, amountUsd }) {
Expand Down
143 changes: 143 additions & 0 deletions tests/mutation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
jest.mock("node-fetch");

const Cashramp = require("../src/index");
const { createGraphQLJSONResponse } = require("./index");

describe("Cashramp mutations", () => {
let client = null;
let fetch = null;
const secretKey = "CSHRMP-SECK-123456789";

beforeEach(() => {
client = new Cashramp({ secretKey, env: "test" });
fetch = require("node-fetch");
});

test("it should confirm a transaction", async () => {
const name = "confirmTransaction";
const result = true;
const payload = { paymentRequest: "1", transactionHash: "0xanwu" };
fetch.mockReturnValue(createGraphQLJSONResponse({ name, result }));

const response = await client.confirmTransaction(payload);
expect(response.success).toBeTruthy();
expect(response.result).toBe(result);

const mockCallIndex = fetch.mock.calls.length - 1;
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(name);
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(
JSON.stringify(payload)
);
});

test("it should initiate a payment request", async () => {
const name = "initiateHostedPayment";
const result = {
id: "1",
hostedLink: "https://useaccrue.com",
status: "completed",
};
const payload = {
amount: 100,
currency: "usd",
countryCode: "GH",
paymentType: "withdrawal",
reference: "test_ref_1",
redirectUrl: "https://google.com",
firstName: "Clinton",
lastName: "Mbah",
email: "[email protected]",
};
fetch.mockReturnValue(createGraphQLJSONResponse({ name, result }));

const response = await client.initiateHostedPayment(payload);
expect(response.success).toBeTruthy();
expect(response.result).toBe(result);

const mockCallIndex = fetch.mock.calls.length - 1;
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(name);
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(
JSON.stringify(payload)
);
});

test("it should cancel a payment request", async () => {
const name = "cancelHostedPayment";
const result = true;
const payload = { paymentRequest: "1" };
fetch.mockReturnValue(createGraphQLJSONResponse({ name, result }));

const response = await client.cancelHostedPayment(payload);
expect(response.success).toBeTruthy();
expect(response.result).toBe(result);

const mockCallIndex = fetch.mock.calls.length - 1;
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(name);
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(
JSON.stringify(payload)
);
});

test("it should create a customer", async () => {
const name = "createCustomer";
const result = { id: "1" };
const payload = {
firstName: "Clinton",
lastName: "Mbah",
email: "[email protected]",
country: "1",
};
fetch.mockReturnValue(createGraphQLJSONResponse({ name, result }));

const response = await client.createCustomer(payload);
expect(response.success).toBeTruthy();
expect(response.result).toBe(result);

const mockCallIndex = fetch.mock.calls.length - 1;
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(name);
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(
JSON.stringify(payload)
);
});

test("it should create a payment method for a customer", async () => {
const name = "addPaymentMethod";
const result = { id: "1" };
const payload = {
customer: "1",
paymentMethodType: "1",
fields: [{ identifier: "name", value: "Clinton Mbah" }],
};
fetch.mockReturnValue(createGraphQLJSONResponse({ name, result }));

const response = await client.addPaymentMethod(payload);
expect(response.success).toBeTruthy();
expect(response.result).toBe(result);

const mockCallIndex = fetch.mock.calls.length - 1;
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(name);
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(
JSON.stringify(payload)
);
});

test("it should withdraw onchain", async () => {
const name = "withdrawOnchain";
const result = { id: "1" };
const payload = {
address: "0xanwu",
amountUsd: 100,
};
fetch.mockReturnValue(createGraphQLJSONResponse({ name, result }));

const response = await client.withdrawOnchain(payload);
expect(response.success).toBeTruthy();
expect(response.result).toBe(result);

const mockCallIndex = fetch.mock.calls.length - 1;
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(name);
expect(fetch.mock.calls[mockCallIndex][1].body).toContain(
JSON.stringify(payload)
);
});
});

0 comments on commit 3803bad

Please sign in to comment.