Skip to content

Commit

Permalink
test: add unit tests for Folksonomy wrapper (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
bengeois authored Oct 31, 2024
1 parent eefaaad commit da4b330
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions tests/folksonomy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { Folksonomy, FolksonomyTag, FolksonomyKey } from "../src/folksonomy";
import { ApiError } from "../src/error";

describe("Folksonomy Wrapper", () => {
let fetchMock: jest.Mock;
let client: Folksonomy;

beforeEach(() => {
fetchMock = jest.fn();
global.fetch = fetchMock;
client = new Folksonomy(fetchMock, "test-token");
});

afterEach(() => {
jest.resetAllMocks();
});

afterAll(() => {
jest.clearAllMocks();
});

const mockResponse = (data: any, ok = true, status = 200) => {
return {
ok,
status,
headers: {
get: (header: string) => {
const headers: { [key: string]: string } = {
"Content-Type": "application/json",
};
return headers[header];
},
},
json: async () => data,
clone: () => ({ json: async () => data }),
};
};

describe("Keys", () => {
it("should fetch keys successfully", async () => {
const data = [{ k: "test-key", count: 1, values: 1 }];
fetchMock.mockResolvedValue(mockResponse(data));

const result = await client.getKeys();
expect(result).toEqual(data);
});

it("should handle error when fetching keys", async () => {
fetchMock.mockResolvedValue(mockResponse(null, false, 500));

expect(await client.getKeys()).toBeNull;
});
});

describe("Products", () => {
it("should fetch products successfully", async () => {
const data = [{ product: 1, k: "Test Product", v: "Test Value" }];
fetchMock.mockResolvedValue(mockResponse(data));

const result = await client.getProducts("test-key", "test-value");
expect(result).toEqual(data);
});

it("should handle error when fetching products", async () => {
fetchMock.mockResolvedValue(mockResponse(null, false, 500));

expect(await client.getKeys()).toBeNull;
});
});

describe("Tags", () => {
it("should put tag successfully", async () => {
const tagData: FolksonomyTag = { k: "test-key", v: "test-value", product: "12345" };
fetchMock.mockResolvedValue(mockResponse("ok", true, 200));

const result = await client.putTag(tagData);
expect(result).toBe(true);
});

it("should handle error when putting tag", async () => {
const tagData: FolksonomyTag = { k: "test-key", v: "test-value", product: "12345" };
fetchMock.mockResolvedValue(mockResponse(null, false, 500));

const result = await client.putTag(tagData);
expect(result).toBe(false);
});

it("should get product tags successfully", async () => {
const data = [{ k: "test-key", v: "test-value", product: "12345" }];
fetchMock.mockResolvedValue(mockResponse(data));

const result = await client.getProduct("12345");
expect(result).toBeDefined();
});

it("should handle error when getting product tags", async () => {
fetchMock.mockResolvedValue(mockResponse(null, false, 500));

expect(await client.getProduct("12345")).toBeNull;
});

it("should add tag successfully", async () => {
const tagData: FolksonomyTag = { k: "test-key", v: "test-value", product: "12345" };
fetchMock.mockResolvedValue(mockResponse("ok", true, 200));

const result = await client.addTag(tagData);
expect(result).toBe(true);
});

it("should handle error when adding tag", async () => {
const tagData: FolksonomyTag = { k: "test-key", v: "test-value", product: "12345" };
fetchMock.mockResolvedValue(mockResponse(null, false, 500));

const result = await client.addTag(tagData);
expect(result).toBe(false);
});

it("should remove tag successfully", async () => {
const tagData: FolksonomyTag & { version: number } = { k: "test-key", v: "test-value", product: "12345", version: 1 };
fetchMock.mockResolvedValue(mockResponse("ok", true, 200));

const result = await client.removeTag(tagData);
expect(result).toBeDefined();
});

it("should handle error when removing tag", async () => {
const tagData: FolksonomyTag & { version: number } = { k: "test-key", v: "test-value", product: "12345", version: 1 };
fetchMock.mockResolvedValue(mockResponse(null, false, 500));

const result = await client.removeTag(tagData);
expect(result).toBeDefined();
});
});

describe("Authentication", () => {
it("should login successfully", async () => {
const data = { access_token: "test-token", token_type: "Bearer" };
fetchMock.mockResolvedValue(mockResponse(data));

const result = await client.login("test-user", "test-password");
expect(result).toEqual({ token: data });
});

it("should handle error when logging in", async () => {
fetchMock.mockResolvedValue(mockResponse(null, false, 401));

const result = await client.login("test-user", "test-password");
expect(result).toEqual({ error: { detail: [{ msg: 'Status code 401', type: 'error', loc: [] }] } });
});
});
});

0 comments on commit da4b330

Please sign in to comment.