Skip to content

Commit 7dbb9f6

Browse files
authored
Merge pull request #10 from ngfelixl/missing-server-tests
tests: add missing server tests, remove callback from plotfunction
2 parents c07b3b8 + 5cd15ee commit 7dbb9f6

File tree

5 files changed

+73
-21
lines changed

5 files changed

+73
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodeplotlib",
3-
"version": "0.4.2",
3+
"version": "0.5.0",
44
"description": "NodeJS frontend-less plotting lib using plotly.js inspired by matplotlib",
55
"main": "dist/lib/index.js",
66
"types": "dist/lib/index.d.ts",

src/plot.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function stack(data: Plot[], layout?: Layout): void {
3232
* @param layout
3333
* @param cb
3434
*/
35-
export function plot(data?: Plot[] | null, layout?: Layout, cb?: (id: number) => void): void {
35+
export function plot(data?: Plot[] | null, layout?: Layout): void {
3636
if (data) {
3737
stack(data, layout);
3838
}
@@ -45,9 +45,5 @@ export function plot(data?: Plot[] | null, layout?: Layout, cb?: (id: number) =>
4545
};
4646
plots = [];
4747

48-
server.spawn(plotContainer, () => {
49-
if (cb) {
50-
cb(id);
51-
}
52-
});
48+
server.spawn(plotContainer);
5349
}

src/server.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,21 @@ export class Server {
1919
* and opens a new browser window targetting the webservers
2020
* data address.
2121
*/
22-
public spawn(plotsContainer: IPlotsContainer, cb?: () => void) {
22+
public spawn(plotsContainer: IPlotsContainer) {
2323
this.plotsContainer = plotsContainer;
2424

2525
if (!this.instance.address()) {
2626
this.instance.listen(this.port);
2727
}
2828

2929
this.openBrowserWindow();
30-
31-
if (cb) {
32-
cb();
33-
}
3430
}
3531

3632
/**
3733
* Closes the webserver and clears the plots container.
3834
*/
3935
public clean() {
40-
if (this.instance) {
36+
if (this.instance.address()) {
4137
this.instance.close();
4238
}
4339
this.plotsContainer = {};

test/clear.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ describe('clear', () => {
1010

1111
expect(lib.plots).toEqual([]);
1212
});
13+
14+
it('should be clearable multiple times', () => {
15+
lib.clear();
16+
lib.clear();
17+
18+
expect(lib.plots).toEqual([]);
19+
});
1320
});

test/server.spec.ts

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,67 @@ describe('Server', () => {
5353
});
5454
});
5555

56+
it('should serve the website and return 404 if html file not found', (done) => {
57+
server.spawn({0: {
58+
opened: false,
59+
pending: false,
60+
plots: [{data: [{ x: [1], y: [2]}]}]
61+
}});
62+
63+
request(`http://localhost:8080/plots/0/index.html`, (err, response, body) => {
64+
expect(response.statusCode).toBe(404);
65+
done();
66+
});
67+
});
68+
69+
it('should serve the nodeplotlib script and return 404 if file not found', (done) => {
70+
server.spawn({0: {
71+
opened: false,
72+
pending: false,
73+
plots: [{data: [{ x: [1], y: [2]}]}]
74+
}});
75+
76+
request(`http://localhost:8080/plots/0/nodeplotlib.min.js`, (err, response, body) => {
77+
expect(response.statusCode).toBe(404);
78+
done();
79+
});
80+
});
81+
82+
it('should serve the plotly.min.js script and return 404 if file not found', (done) => {
83+
server.spawn({0: {
84+
opened: false,
85+
pending: false,
86+
plots: [{data: [{ x: [1], y: [2]}]}]
87+
}});
88+
89+
request(`http://localhost:8080/plots/0/plotly.min.js`, (err, response, body) => {
90+
expect(response.statusCode).toBe(404);
91+
done();
92+
});
93+
});
94+
95+
it('should not close the webserver, if one plot hasn\'t got its data', (done) => {
96+
server.spawn({0: {
97+
opened: false,
98+
pending: false,
99+
plots: [{data: [{ x: [1], y: [2]}]}]
100+
},
101+
1: {
102+
opened: false,
103+
pending: false,
104+
plots: [{data: [{ x: [1], y: [3]}]}]
105+
}});
106+
107+
request(`http://localhost:8080/data/0`, (err, response, body) => {
108+
expect(JSON.parse(body)).toEqual([{data: [{ x: [1], y: [2]}]}]);
109+
110+
request(`http://localhost:8080/data/1`, (err1, response1, body1) => {
111+
expect(JSON.parse(body1)).toEqual([{data: [{ x: [1], y: [3]}]}]);
112+
done();
113+
});
114+
});
115+
});
116+
56117
it('should return 404 if routes not matching', (done) => {
57118
const data = {0: {
58119
opened: false,
@@ -69,14 +130,6 @@ describe('Server', () => {
69130
});
70131
});
71132

72-
it('should work with callback', () => {
73-
const mock = jest.fn().mockImplementation(() => null);
74-
75-
server.spawn({}, mock);
76-
77-
expect(mock).toHaveBeenCalledTimes(1);
78-
});
79-
80133
afterEach(() => {
81134
server.clean();
82135
server = null;

0 commit comments

Comments
 (0)