2023-07-07 13:56:08 +03:00
|
|
|
/* eslint-disable max-lines */
|
2023-07-06 20:01:23 +03:00
|
|
|
import { renderHook } from "@testing-library/react";
|
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
import { useBrainApi } from "../useBrainApi";
|
|
|
|
|
|
|
|
const axiosGetMock = vi.fn(() => ({
|
|
|
|
data: {
|
|
|
|
documents: [],
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2023-07-07 13:56:08 +03:00
|
|
|
const axiosPostMock = vi.fn(() => ({
|
|
|
|
data: {
|
|
|
|
id: "123",
|
|
|
|
name: "Test Brain",
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
const axiosDeleteMock = vi.fn(() => ({}));
|
|
|
|
|
2023-07-06 20:01:23 +03:00
|
|
|
vi.mock("@/lib/hooks", () => ({
|
|
|
|
useAxios: vi.fn(() => ({
|
|
|
|
axiosInstance: {
|
|
|
|
get: axiosGetMock,
|
2023-07-07 13:56:08 +03:00
|
|
|
post: axiosPostMock,
|
|
|
|
delete: axiosDeleteMock,
|
2023-07-06 20:01:23 +03:00
|
|
|
},
|
|
|
|
})),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("useBrainApi", () => {
|
|
|
|
afterEach(() => {
|
2023-07-07 13:56:08 +03:00
|
|
|
vi.restoreAllMocks();
|
2023-07-06 20:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call getBrainDocuments with the correct parameters", async () => {
|
|
|
|
const {
|
|
|
|
result: {
|
|
|
|
current: { getBrainDocuments },
|
|
|
|
},
|
|
|
|
} = renderHook(() => useBrainApi());
|
|
|
|
const brainId = "123";
|
|
|
|
await getBrainDocuments(brainId);
|
|
|
|
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledWith(`/explore/?brain_id=${brainId}`);
|
|
|
|
});
|
2023-07-07 13:56:08 +03:00
|
|
|
|
|
|
|
it("should call createBrain with the correct parameters", async () => {
|
|
|
|
const {
|
|
|
|
result: {
|
|
|
|
current: { createBrain },
|
|
|
|
},
|
|
|
|
} = renderHook(() => useBrainApi());
|
|
|
|
const name = "Test Brain";
|
|
|
|
await createBrain(name);
|
|
|
|
|
|
|
|
expect(axiosPostMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(axiosPostMock).toHaveBeenCalledWith("/brains/", { name });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call deleteBrain with the correct parameters", async () => {
|
|
|
|
const {
|
|
|
|
result: {
|
|
|
|
current: { deleteBrain },
|
|
|
|
},
|
|
|
|
} = renderHook(() => useBrainApi());
|
|
|
|
const id = "123";
|
|
|
|
await deleteBrain(id);
|
|
|
|
|
|
|
|
expect(axiosDeleteMock).toHaveBeenCalledTimes(1);
|
2023-07-18 10:47:59 +03:00
|
|
|
expect(axiosDeleteMock).toHaveBeenCalledWith(`/brains/${id}/subscription`);
|
2023-07-07 13:56:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call getDefaultBrain with the correct parameters", async () => {
|
|
|
|
const {
|
|
|
|
result: {
|
|
|
|
current: { getDefaultBrain },
|
|
|
|
},
|
|
|
|
} = renderHook(() => useBrainApi());
|
|
|
|
await getDefaultBrain();
|
|
|
|
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledWith("/brains/default/");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call getBrains with the correct parameters", async () => {
|
|
|
|
const {
|
|
|
|
result: {
|
|
|
|
current: { getBrains },
|
|
|
|
},
|
|
|
|
} = renderHook(() => useBrainApi());
|
|
|
|
await getBrains();
|
|
|
|
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledWith("/brains/");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call getBrain with the correct parameters", async () => {
|
|
|
|
const {
|
|
|
|
result: {
|
|
|
|
current: { getBrain },
|
|
|
|
},
|
|
|
|
} = renderHook(() => useBrainApi());
|
|
|
|
const id = "123";
|
|
|
|
await getBrain(id);
|
|
|
|
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledWith(`/brains/${id}/`);
|
|
|
|
});
|
2023-07-12 16:45:45 +03:00
|
|
|
|
|
|
|
it("should call addBrainSubscription with the correct parameters", async () => {
|
|
|
|
const {
|
|
|
|
result: {
|
|
|
|
current: { addBrainSubscriptions },
|
|
|
|
},
|
|
|
|
} = renderHook(() => useBrainApi());
|
|
|
|
const id = "123";
|
|
|
|
const subscriptions = [
|
|
|
|
{
|
|
|
|
email: "user@quivr.app",
|
|
|
|
rights: "viewer",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
await addBrainSubscriptions(id, subscriptions);
|
|
|
|
|
|
|
|
expect(axiosPostMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(axiosPostMock).toHaveBeenCalledWith(
|
2023-07-18 10:47:59 +03:00
|
|
|
`/brains/${id}/subscription`,
|
2023-07-12 16:45:45 +03:00
|
|
|
subscriptions
|
|
|
|
);
|
|
|
|
});
|
2023-07-17 16:45:18 +03:00
|
|
|
|
|
|
|
it("should call getBrainUsers with the correct parameters", async () => {
|
|
|
|
const {
|
|
|
|
result: {
|
|
|
|
current: { getBrainUsers },
|
|
|
|
},
|
|
|
|
} = renderHook(() => useBrainApi());
|
|
|
|
const id = "123";
|
|
|
|
await getBrainUsers(id);
|
|
|
|
|
|
|
|
expect(axiosGetMock).toHaveBeenCalledTimes(1);
|
2023-07-18 10:47:59 +03:00
|
|
|
expect(axiosGetMock).toHaveBeenCalledWith(`/brains/${id}/users`);
|
2023-07-17 16:45:18 +03:00
|
|
|
});
|
2023-07-06 20:01:23 +03:00
|
|
|
});
|