Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.
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
46 changes: 46 additions & 0 deletions __tests__/charges/createCharge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TEST_API_KEY } from "../../src/constants";
import { zbd } from "../../src/zbd";
import {
ChargeDataResponseType,
ChargeOptionsType,
isChargeResponseType,
} from "../../src/types/charges";

const ZBD = new zbd(TEST_API_KEY);

describe("createCharge", () => {
it("should successfully create a charge", async () => {
const payload: ChargeOptionsType = {
expiresIn: 300,
amount: "10000",
description: "My Charge Test Zapier",
internalId: "internalId",
callbackUrl: "https://my-website.com/zbd-callback",
};

const response = await ZBD.createCharge(payload);

expect(isChargeResponseType(response)).toBeTruthy();
expect(response.success).toBe(true);
expect(response.message).toBe("Charge created.");
expect(response.data.amount).toBe(payload.amount);
expect(response.data.description).toBe(payload.description);
});

describe("createCharge error scenarios", () => {
it("should throw an error given an erroneous payload (amount = 0)", async () => {
const erroneousPayload: ChargeOptionsType = {
expiresIn: 100,
amount: "0",
description: "My Charge Test Zapier",
internalId: "internalId",
callbackUrl: "https://my-website.com/zbd-callback",
};

await expect(ZBD.createCharge(erroneousPayload)).rejects.toMatchObject({
message: "Request has missing or mismatch params.",
status: 400,
});
});
});
});
32 changes: 32 additions & 0 deletions __tests__/charges/getCharge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { zbd } from "@zbd/node";
import { TEST_API_KEY } from "../../src/constants";
import {
ChargeDataResponseType,
isChargeResponseType,
} from "../../src/types/charges";

const ZBD = new zbd(TEST_API_KEY);

describe("getCharge", () => {
const VALID_CHARGE_ID = "4f0fa38f-efbe-485f-9293-95e697f6fbd4";
const INVALID_CHARGE_ID = "invalid";
const INVALID_API_KEY = "INVALID_API_KEY";

// Success
it("should fetch charge details for a valid charge ID", async () => {
const data = await ZBD.getCharge(VALID_CHARGE_ID);
expect(data.success).toBe(true);
expect(data.message).toBe("Fetched Charge.");

// Data Validation
expect(isChargeResponseType(data)).toBeTruthy();
});

// Charge not found
it("should return a 404 for a non-existent charge ID", async () => {
await expect(ZBD.getCharge(INVALID_CHARGE_ID)).rejects.toMatchObject({
message: "No Charge records found with this ID.",
status: 404,
});
});
});
35 changes: 0 additions & 35 deletions __tests__/create-charge.test.ts

This file was deleted.

99 changes: 99 additions & 0 deletions __tests__/fetch-access-token.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { zbd } from "../src/zbd";
import { API, API_URL, TEST_API_KEY } from "../src/constants";
import { FetchAccessTokenRes, FetchTokenParam } from "../src/types";

const postData = jest.fn();
describe("fetchAccessToken", () => {
let ZBD: zbd;

beforeEach(() => {
ZBD = new zbd(TEST_API_KEY);
ZBD.setClientId("client_id");
ZBD.setClientSecret("secret123");
});

it("should fetch access token successfully", async () => {
await ZBD.generatePKCECodes();
const code_verifier = ZBD.getCodeChallenge();

console.log("Code verifier: ", code_verifier);
// Arrange
const param: FetchTokenParam = {
code: "xxx11xx1-xxxx-xxxx-xxx1-1xx11xx111xx",
redirect_uri: "http://redirect-uri.com",
};

const mockAccessTokenResponse: FetchAccessTokenRes = {
access_token: "mockAccessToken",
token_type: "Bearer",
expires_in: 3600,
refresh_token: "mockRefreshToken",
refresh_token_expires_in: 7200,
scope: "read write",
};

postData.mockResolvedValue(mockAccessTokenResponse);

// Act
const result = await ZBD.fetchAccessToken(param);
console.log("Client id: ", ZBD.clientId);

// Assert
// expect(result).toEqual(mockAccessTokenResponse);
// expect(postData).toHaveBeenCalledWith({
// url: `${API_URL}${API.FETCH_ACCESS_TOKEN_ENDPOINT}`,
// headers: { ...ZBD.apiCoreHeaders },
// body: {
// client_id: ZBD.clientId,
// client_secret: ZBD.clientSecret,
// grant_type: "authorization_code",
// code: param.code,
// redirect_uri: param.redirect_uri,
// code_verifier: ZBD.codeVerifier,
// },
// });
});

it("should throw an error if authorization code is missing", async () => {
// Arrange
const param: FetchTokenParam = {
code: "",
redirect_uri: "http://redirect-uri.com",
};

// Act and Assert
await expect(ZBD.fetchAccessToken(param)).rejects.toThrow(
"Authorization code is required"
);
});

it("should throw an error if redirect URI is missing", async () => {
// Arrange
const param: FetchTokenParam = {
code: "authorization_code",
redirect_uri: "",
};

// Act and Assert
await expect(ZBD.fetchAccessToken(param)).rejects.toThrow(
"Redirect URI is required"
);
});

// Add more test cases for other validation checks

it("should throw an error if fetching access token fails", async () => {
// Arrange
const param: FetchTokenParam = {
code: "authorization_code",
redirect_uri: "http://redirect-uri.com",
};

postData.mockRejectedValue(new Error("Network error"));

// Act and Assert
await expect(ZBD.fetchAccessToken(param)).rejects.toThrow(
"A code challenge is required. Generate one using .generatePKCE()."
);
});
});
46 changes: 46 additions & 0 deletions __tests__/gamertag/fetchChargeFromGamertag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TEST_API_KEY } from "../../src/constants";
import {
FetchChargeFromGamertagDataResponseType,
FetchChargeFromGamertagOptionsType,
isFetchChargeFromGamertagDataResponseType,
} from "../../src/types";
import { zbd } from "../../src/zbd";

const ZBD = new zbd(TEST_API_KEY);

describe("Fetch Charge from Gamertag", () => {
const requestBody: FetchChargeFromGamertagOptionsType = {
amount: "1000",
gamertag: "andre",
description: "Requesting Charge for Gamertag",
callbackUrl: "https://your-website.com/zbd-callback",
internalId: "test-internal-id",
};

it("should successfully create a charge for a gamertag", async () => {
const response = await ZBD.createGamertagCharge(requestBody);

expect(response).toBeDefined();
expect(response.success).toBe(true);
expect(isFetchChargeFromGamertagDataResponseType(response)).toBeTruthy();
});

describe("fetchGamertagByUserID error scenarios", () => {
it("should throw an error given a non-existent user ID", async () => {
const errorRequestBody: FetchChargeFromGamertagOptionsType = {
amount: "1000",
gamertag: "fakeGamerTagĆ",
description: "Requesting Charge for fake Gamertag",
callbackUrl: "https://your-website.com/zbd-callback",
internalId: "test-internal-id",
};

await expect(
ZBD.createGamertagCharge(errorRequestBody)
).rejects.toMatchObject({
message: "API request failed",
status: 500,
});
});
});
});
33 changes: 33 additions & 0 deletions __tests__/gamertag/fetchGamertagByUserID.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TEST_API_KEY } from "../../src/constants";
import {
FetchGamertagByUserIdDataResponseType,
isFetchGamertagByUserIDDataResponseType,
} from "../../src/types";
import { zbd } from "../../src/zbd";

const ZBD = new zbd(TEST_API_KEY);

describe("Fetch Gamertag By User ID", () => {
const testUserID = "ec9b38d5-b126-4307-9d1e-8aa0dfab5d7e";
const fakeUserID = "202020";

it("should successfully fetch a gamertag by user ID", async () => {
const response = await ZBD.getGamertagByUserId(testUserID);

expect(response).toBeDefined();
expect(response.success).toBe(true);
expect(response.message).toBe("Fetched gamertag from uuid");

// Data Validation
expect(isFetchGamertagByUserIDDataResponseType(response)).toBeTruthy();
});

describe("fetchGamertagByUserID error scenarios", () => {
it("should throw an error given a non-existent user ID", async () => {
await expect(ZBD.getGamertagByUserId(fakeUserID)).rejects.toMatchObject({
message: "No gamertag found with this uuid",
status: 400,
});
});
});
});
34 changes: 34 additions & 0 deletions __tests__/gamertag/fetchGamertagTransactionDetailsByID.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { TEST_API_KEY } from "../../src/constants";
import {
GamertagTransactionDataResponseType,
isGamertagTransactionDataResponseType,
} from "../../src/types";
import { zbd } from "../../src/zbd";

const ZBD = new zbd(TEST_API_KEY);

describe("Fetch Gamertag Transaction Details By ID", () => {
const TEST_TRANSACTION_ID = "3418e871-05f6-4745-b12b-ccd53da9c4d1";
const NON_EXISTENT_TRANSACTION_ID = "903883f2-67d9-4707-a21b-ddff004fe041";

it("should successfully fetch transaction details by ID", async () => {
const response = await ZBD.getGamertagTransaction(TEST_TRANSACTION_ID);

expect(response).toBeDefined();
expect(response.success).toBe(true);

expect(isGamertagTransactionDataResponseType(response)).toBeTruthy();
});

describe("fetchGamertagTransactionDetailsByID error scenarios", () => {
it("should fail to fetch transaction details given false ID", async () => {
const response = await ZBD.getGamertagTransaction(
NON_EXISTENT_TRANSACTION_ID
);

expect(response).toBeDefined();
expect(response.success).toBe(true);
expect(response.message).toBe("Transaction not found");
});
});
});
31 changes: 31 additions & 0 deletions __tests__/gamertag/fetchUserIDFromGamertag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TEST_API_KEY } from "../../src/constants";
import {
FetchUserIdByGamertagDataResponseType,
isFetchUserIdByGamertagDataResponseType,
} from "../../src/types";
import { zbd } from "../../src/zbd";

const ZBD = new zbd(TEST_API_KEY);

describe("Fetch User ID By Gamertag", () => {
const testGamertag = "foxp2";

it("should successfully fetch user ID for a valid gamertag", async () => {
const response = await ZBD.getUserIdByGamertag(testGamertag);

expect(response).toBeDefined();
expect(response.success).toBe(true);
expect(isFetchUserIdByGamertagDataResponseType(response)).toBeTruthy();
});

it("should return an error for an invalid gamertag", async () => {
const invalidGamertag = "nonExistentTagč";

await expect(
ZBD.getUserIdByGamertag(invalidGamertag)
).rejects.toMatchObject({
message: "No user found with this gamertag",
status: 400,
});
});
});
Loading