Skip to content

Commit

Permalink
[test] Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
totallynotvaishnav committed Sep 2, 2022
1 parent d804a94 commit 6f94d00
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions test/netjsongraph.render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ const JSONData = {
nodes: [],
links: [],
};
const param = "data1.json";
const nextParam = "data2.json";
const data1 = {
results: {
nodes: [{id: "1"}, {id: "2"}],
links: [],
},
next: "data2.json",
prev: null,
};
const data2 = {
results: {
nodes: [{id: "3"}, {id: "4"}],
links: [],
},
next: null,
};

const graph = new NetJSONGraph([JSONFILE, JSONFILE]);
graph.event = graph.utils.createEvent();
Expand All @@ -29,11 +46,22 @@ graph.setConfig({
});
graph.setUtils();

window.fetch = jest.fn((url) =>
url === JSONFILE
? Promise.resolve(JSONData)
: Promise.reject(new Error("Fetch json file wrong!")),
);
window.fetch = jest.fn((url) => {
if (url === JSONFILE) {
return Promise.resolve(JSONData);
}
if (url === param) {
return Promise.resolve({
json: () => Promise.resolve(data1),
});
}
if (url === nextParam) {
return Promise.resolve({
json: () => Promise.resolve(data2),
});
}
return Promise.reject(new Error("Fetch json file wrong!"));
});

describe("Test netjsongraph render", () => {
beforeAll(() => {
Expand Down Expand Up @@ -244,6 +272,18 @@ describe("Test netjsongraph JSONParamParse", () => {
});
});

describe("Test paginatedDataParse", () => {
const {paginatedDataParse} = graph.utils;
test("Should return the data", () => {
paginatedDataParse.call(graph, param).then((data) => {
expect(data).toEqual({
nodes: [{id: "1"}, {id: "2"}, {id: "3"}, {id: "4"}],
links: [],
});
});
});
});

describe("Test netjsongraph searchElements", () => {
test("Add search function for new elements.", () => {
const searchFunc = graph.utils.searchElements.call(graph, "test");
Expand Down

0 comments on commit 6f94d00

Please sign in to comment.