Skip to content

Commit

Permalink
Merge 6f94d00 into 441c123
Browse files Browse the repository at this point in the history
  • Loading branch information
totallynotvaishnav committed Sep 2, 2022
2 parents 441c123 + 6f94d00 commit 2121e50
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dist/netjsongraph.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/netjsonmap-nodeTiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@

if (typeof tilesMap[curZoom] === "string") {
// json update function
this.utils
.JSONParamParse(folderName + tilesMap[curZoom])
this.utils.paginatedDataParse
.call(this, folderName + tilesMap[curZoom])
.then((JSONData) => {
// store the data.
tilesMap[curZoom] = JSONData;
Expand Down
1 change: 1 addition & 0 deletions src/js/netjsongraph.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const NetJSONGraphDefaultConfig = {
metadata: true,
svgRender: false,
switchMode: false,
maxPointsFetched: 10000,
showMetaOnNarrowScreens: false,
echartsOption: {
aria: {
Expand Down
5 changes: 2 additions & 3 deletions src/js/netjsongraph.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ class NetJSONGraph {
this.config.onRender.call(this);
this.event.once("onReady", this.config.onReady.bind(this));
this.event.once("onLoad", this.config.onLoad.bind(this));

this.utils
.JSONParamParse(JSONParam)
this.utils.paginatedDataParse
.call(this, JSONParam)
.then((JSONData) => {
if (this.utils.isNetJSON(JSONData)) {
this.type = "netjson";
Expand Down
4 changes: 2 additions & 2 deletions src/js/netjsongraph.update.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class NetJSONGraphUpdate extends NetJSONGraphUtil {
const self = this;
self.config.onUpdate.call(self);

return self.utils
.JSONParamParse(Data)
return self.utils.paginatedDataParse
.call(self, Data)
.then((JSONData) => {
function update() {
// override data.
Expand Down
33 changes: 27 additions & 6 deletions src/js/netjsongraph.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,40 @@ class NetJSONGraphUtil {
Accept: "application/json",
},
})
.then((response) => {
if (response.json) {
return response.json();
}
return response;
})
.then((response) => response)
.catch((msg) => {
console.error(msg);
});
}
return Promise.resolve(JSONParam);
}

async paginatedDataParse(JSONParam) {
let res;
let data;
try {
let paginatedResponse = await this.utils.JSONParamParse(JSONParam);
if (paginatedResponse.json) {
res = await paginatedResponse.json();
data = res.results ? res.results : res;
while (res.next && data.nodes.length <= this.config.maxPointsFetched) {
// eslint-disable-next-line no-await-in-loop
paginatedResponse = await this.utils.JSONParamParse(res.next);
// eslint-disable-next-line no-await-in-loop
res = await paginatedResponse.json();
data.nodes = data.nodes.concat(res.results.nodes);
data.links = data.links.concat(res.results.links);
}
} else {
data = paginatedResponse;
}
} catch (e) {
console.error(e);
}

return data;
}

/**
* @function
* @name dateParse
Expand Down
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 2121e50

Please sign in to comment.