Skip to content

Commit

Permalink
fix: unit tests for client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
barbmarcio committed Aug 12, 2024
1 parent 69617cf commit d80b5b9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 70 deletions.
65 changes: 31 additions & 34 deletions test/auctions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { afterEach, beforeAll, describe, expect, it } from "bun:test";
import { createAuction } from "../src";
import {
afterEach,
beforeEach,
beforeAll,
describe,
expect,
it,
} from "bun:test";
import { TopsortClient } from "../src";
import { apis, baseURL } from "../src/constants/apis.constant";
import {
mswServer,
Expand All @@ -11,12 +18,16 @@ import AppError from "../src/lib/app-error";
import { TopsortAuction } from "../src/types/auctions";

describe("createAuction", () => {
let topsortClient: TopsortClient;
beforeAll(() => mswServer.listen());
afterEach(() => mswServer.resetHandlers());
beforeEach(() => {
topsortClient = new TopsortClient({ apiKey: "apiKey" });
});

it("should handle authentication error", async () => {
returnStatus(401, `${baseURL}/${apis.auctions}`);
expect(createAuction({ apiKey: "apiKey" }, {} as TopsortAuction)).rejects.toEqual({
returnStatus(401, `${baseURL}${apis.auctions}`);
expect(topsortClient.createAuction({} as TopsortAuction)).rejects.toEqual({
status: 401,
retry: false,
statusText: "Unauthorized",
Expand All @@ -25,8 +36,8 @@ describe("createAuction", () => {
});

it("should handle retryable error", async () => {
returnStatus(429, `${baseURL}/${apis.auctions}`);
expect(createAuction({ apiKey: "apiKey" }, {} as TopsortAuction)).rejects.toEqual({
returnStatus(429, `${baseURL}${apis.auctions}`);
expect(topsortClient.createAuction({} as TopsortAuction)).rejects.toEqual({
status: 429,
retry: true,
statusText: "Too Many Requests",
Expand All @@ -35,8 +46,8 @@ describe("createAuction", () => {
});

it("should handle server error", async () => {
returnStatus(500, `${baseURL}/${apis.auctions}`);
expect(createAuction({ apiKey: "apiKey" }, {} as TopsortAuction)).rejects.toEqual({
returnStatus(500, `${baseURL}${apis.auctions}`);
expect(topsortClient.createAuction({} as TopsortAuction)).rejects.toEqual({
status: 500,
retry: true,
statusText: "Internal Server Error",
Expand All @@ -45,16 +56,13 @@ describe("createAuction", () => {
});

it("should handle custom url", async () => {
returnAuctionSuccess(`https://demo.api.topsort.com/${apis.auctions}`);
expect(
createAuction(
{
apiKey: "apiKey",
host: "https://demo.api.topsort.com",
},
{} as TopsortAuction,
),
).resolves.toEqual({
returnAuctionSuccess(`https://demo.api.topsort.com${apis.auctions}`);
topsortClient = new TopsortClient({
apiKey: "apiKey",
host: "https://demo.api.topsort.com",
});

expect(topsortClient.createAuction({} as TopsortAuction)).resolves.toEqual({
results: [
{
resultType: "listings",
Expand All @@ -71,28 +79,17 @@ describe("createAuction", () => {
});

it("should handle fetch error", async () => {
returnError(`${baseURL}/${apis.auctions}`);
expect(
async () =>
await createAuction(
{
apiKey: "apiKey",
},
{} as TopsortAuction,
),
returnError(`${baseURL}${apis.auctions}`);
expect(async () =>
topsortClient.createAuction({} as TopsortAuction)
).toThrow(AppError);
});

it("should handle invalid URL error", async () => {
const invalidHost = "invalid-url";
topsortClient = new TopsortClient({ apiKey: "apiKey", host: invalidHost });
expect(async () =>
createAuction(
{
apiKey: "apiKey",
host: invalidHost,
},
{} as TopsortAuction,
),
topsortClient.createAuction({} as TopsortAuction)
).toThrow(AppError);
});
});
75 changes: 39 additions & 36 deletions test/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { afterEach, beforeAll, describe, expect, it } from "bun:test";
import { TopsortEvent, reportEvent } from "../src";
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
} from "bun:test";
import { TopsortClient, TopsortEvent } from "../src";
import { apis, baseURL } from "../src/constants/apis.constant";
import { mswServer, returnError, returnStatus } from "../src/constants/handlers.constant";
import {
mswServer,
returnError,
returnStatus,
} from "../src/constants/handlers.constant";
import AppError from "../src/lib/app-error";

describe("reportEvent", () => {
let topsortClient: TopsortClient;
beforeAll(() => mswServer.listen());
afterEach(() => mswServer.resetHandlers());
beforeEach(() => {
topsortClient = new TopsortClient({ apiKey: "apiKey" });
});

it("should handle authentication error", async () => {
returnStatus(401, `${baseURL}/${apis.events}`);
expect(reportEvent({ apiKey: "apiKey" }, {} as TopsortEvent)).rejects.toEqual({
returnStatus(401, `${baseURL}${apis.events}`);
expect(topsortClient.reportEvent({} as TopsortEvent)).rejects.toEqual({
status: 401,
retry: false,
statusText: "Unauthorized",
Expand All @@ -19,57 +34,45 @@ describe("reportEvent", () => {
});

it("should handle retryable error", async () => {
returnStatus(429, `${baseURL}/${apis.events}`);
expect(reportEvent({ apiKey: "apiKey" }, {} as TopsortEvent)).resolves.toEqual({
returnStatus(429, `${baseURL}${apis.events}`);
expect(topsortClient.reportEvent({} as TopsortEvent)).resolves.toEqual({
ok: false,
retry: true,
});
});

it("should handle server error", async () => {
returnStatus(500, `${baseURL}/${apis.events}`);
expect(reportEvent({ apiKey: "apiKey" }, {} as TopsortEvent)).resolves.toEqual({
returnStatus(500, `${baseURL}${apis.events}`);
expect(topsortClient.reportEvent({} as TopsortEvent)).resolves.toEqual({
ok: false,
retry: true,
});
});

it("should handle custom url", async () => {
returnStatus(200, `https://demo.api.topsort.com/${apis.events}`);
expect(
reportEvent(
{
apiKey: "apiKey",
host: "https://demo.api.topsort.com",
},
{} as TopsortEvent,
),
).resolves.toEqual({ ok: true, retry: false });
returnStatus(200, `https://demo.api.topsort.com${apis.events}`);
topsortClient = new TopsortClient({
apiKey: "apiKey",
host: "https://demo.api.topsort.com/",
});
expect(topsortClient.reportEvent({} as TopsortEvent)).resolves.toEqual({
ok: true,
retry: false,
});
});

it("should handle fetch error", async () => {
returnError(`${baseURL}/${apis.events}`);
returnError(`${baseURL}${apis.events}`);
expect(
async () =>
await reportEvent(
{
apiKey: "apiKey",
},
{} as TopsortEvent,
),
async () => await topsortClient.reportEvent({} as TopsortEvent)
).toThrow(AppError);
});

it("should handle invalid URL error", async () => {
const invalidHost = "invalid-url";
expect(async () =>
reportEvent(
{
apiKey: "apiKey",
host: invalidHost,
},
{} as TopsortEvent,
),
).toThrow(AppError);
topsortClient = new TopsortClient({ apiKey: "apiKey", host: invalidHost });
expect(async () => topsortClient.reportEvent({} as TopsortEvent)).toThrow(
AppError
);
});
});

0 comments on commit d80b5b9

Please sign in to comment.