diff --git a/package.json b/package.json index 7d9078b..6b834a3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/plot.ts b/src/plot.ts index 2c9d9fb..1f40455 100644 --- a/src/plot.ts +++ b/src/plot.ts @@ -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); } @@ -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); } diff --git a/src/server.ts b/src/server.ts index 0980e6c..d4354d4 100644 --- a/src/server.ts +++ b/src/server.ts @@ -19,7 +19,7 @@ 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()) { @@ -27,17 +27,13 @@ export class Server { } 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 = {}; diff --git a/test/clear.spec.ts b/test/clear.spec.ts index 68b9d0b..10429d5 100644 --- a/test/clear.spec.ts +++ b/test/clear.spec.ts @@ -10,4 +10,11 @@ describe('clear', () => { expect(lib.plots).toEqual([]); }); + + it('should be clearable multiple times', () => { + lib.clear(); + lib.clear(); + + expect(lib.plots).toEqual([]); + }); }); diff --git a/test/server.spec.ts b/test/server.spec.ts index 6487c5e..af7cecf 100644 --- a/test/server.spec.ts +++ b/test/server.spec.ts @@ -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, @@ -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;