Skip to content

Commit

Permalink
Test dataloader
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Nov 3, 2024
1 parent 3aec9c1 commit 6fdd0c9
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/test/dataLoader.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { waitFor } from "@testing-library/dom";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";

import DataLoader from "../dataLoader";

const server = setupServer();

afterAll(() => {
server.close();
});

afterEach(() => {
server.resetHandlers();
});

beforeAll(() => {
server.listen();
});

describe("loadFromUrl", () => {
it("does nothing when urlInfo is empty", () => {
const loadData = () => null;
Expand All @@ -15,4 +33,34 @@ describe("loadFromUrl", () => {
dataLoader.loadFromUrl(null, null, null);
expect(triggerEvent).not.toHaveBeenCalled();
});

it("parses json when the response is a string", async () => {
server.use(
http.get(
"/test",
() =>
new HttpResponse('{ "key1": "value1" }', {
headers: {
"Content-Type": "text/plain",
},
}),
{},
),
);

const loadData = jest.fn();
const treeElement = document.createElement("div");
const triggerEvent = jest.fn();

const dataLoader = new DataLoader({
loadData,
treeElement,
triggerEvent,
});
dataLoader.loadFromUrl({ dataType: "text", url: "/test" }, null, null);

await waitFor(() => {
expect(loadData).toHaveBeenCalledWith({ key1: "value1" }, null);
});
});
});

0 comments on commit 6fdd0c9

Please sign in to comment.