Skip to content

Commit

Permalink
tests: add missing server tests, remove callback from plotfunction
Browse files Browse the repository at this point in the history
  • Loading branch information
ngfelixl committed Feb 20, 2019
1 parent c07b3b8 commit 5cd15ee
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodeplotlib",
"version": "0.4.2",
"version": "0.5.0",
"description": "NodeJS frontend-less plotting lib using plotly.js inspired by matplotlib",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
8 changes: 2 additions & 6 deletions src/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function stack(data: Plot[], layout?: Layout): void {
* @param layout
* @param cb
*/
export function plot(data?: Plot[] | null, layout?: Layout, cb?: (id: number) => void): void {
export function plot(data?: Plot[] | null, layout?: Layout): void {
if (data) {
stack(data, layout);
}
Expand All @@ -45,9 +45,5 @@ export function plot(data?: Plot[] | null, layout?: Layout, cb?: (id: number) =>
};
plots = [];

server.spawn(plotContainer, () => {
if (cb) {
cb(id);
}
});
server.spawn(plotContainer);
}
8 changes: 2 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ export class Server {
* and opens a new browser window targetting the webservers
* data address.
*/
public spawn(plotsContainer: IPlotsContainer, cb?: () => void) {
public spawn(plotsContainer: IPlotsContainer) {
this.plotsContainer = plotsContainer;

if (!this.instance.address()) {
this.instance.listen(this.port);
}

this.openBrowserWindow();

if (cb) {
cb();
}
}

/**
* Closes the webserver and clears the plots container.
*/
public clean() {
if (this.instance) {
if (this.instance.address()) {
this.instance.close();
}
this.plotsContainer = {};
Expand Down
7 changes: 7 additions & 0 deletions test/clear.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ describe('clear', () => {

expect(lib.plots).toEqual([]);
});

it('should be clearable multiple times', () => {
lib.clear();
lib.clear();

expect(lib.plots).toEqual([]);
});
});
69 changes: 61 additions & 8 deletions test/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,67 @@ describe('Server', () => {
});
});

it('should serve the website and return 404 if html file not found', (done) => {
server.spawn({0: {
opened: false,
pending: false,
plots: [{data: [{ x: [1], y: [2]}]}]
}});

request(`http://localhost:8080/plots/0/index.html`, (err, response, body) => {
expect(response.statusCode).toBe(404);
done();
});
});

it('should serve the nodeplotlib script and return 404 if file not found', (done) => {
server.spawn({0: {
opened: false,
pending: false,
plots: [{data: [{ x: [1], y: [2]}]}]
}});

request(`http://localhost:8080/plots/0/nodeplotlib.min.js`, (err, response, body) => {
expect(response.statusCode).toBe(404);
done();
});
});

it('should serve the plotly.min.js script and return 404 if file not found', (done) => {
server.spawn({0: {
opened: false,
pending: false,
plots: [{data: [{ x: [1], y: [2]}]}]
}});

request(`http://localhost:8080/plots/0/plotly.min.js`, (err, response, body) => {
expect(response.statusCode).toBe(404);
done();
});
});

it('should not close the webserver, if one plot hasn\'t got its data', (done) => {
server.spawn({0: {
opened: false,
pending: false,
plots: [{data: [{ x: [1], y: [2]}]}]
},
1: {
opened: false,
pending: false,
plots: [{data: [{ x: [1], y: [3]}]}]
}});

request(`http://localhost:8080/data/0`, (err, response, body) => {
expect(JSON.parse(body)).toEqual([{data: [{ x: [1], y: [2]}]}]);

request(`http://localhost:8080/data/1`, (err1, response1, body1) => {
expect(JSON.parse(body1)).toEqual([{data: [{ x: [1], y: [3]}]}]);
done();
});
});
});

it('should return 404 if routes not matching', (done) => {
const data = {0: {
opened: false,
Expand All @@ -69,14 +130,6 @@ describe('Server', () => {
});
});

it('should work with callback', () => {
const mock = jest.fn().mockImplementation(() => null);

server.spawn({}, mock);

expect(mock).toHaveBeenCalledTimes(1);
});

afterEach(() => {
server.clean();
server = null;
Expand Down

0 comments on commit 5cd15ee

Please sign in to comment.