@@ -6,13 +6,15 @@ const port = Number(process.env.PORT) || 8080;
66const validData = {
77 opened : false ,
88 pending : false ,
9- plots : [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ]
9+ plots : [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] ,
1010} ;
1111
1212jest . mock ( 'child_process' ) ;
13- jest . mock ( 'fs' , ( ) => ( { readFile : ( path : any , options : any , callback : ( err : any , data : any ) => void ) => {
14- callback ( 'Error' , null ) ;
15- } } ) ) ;
13+ jest . mock ( 'fs' , ( ) => ( {
14+ readFile : ( path : any , options : any , callback : ( err : any , data : any ) => void ) => {
15+ callback ( 'Error' , null ) ;
16+ } ,
17+ } ) ) ;
1618
1719describe ( 'Server' , ( ) => {
1820 let server : any ;
@@ -26,81 +28,96 @@ describe('Server', () => {
2628 } ) ;
2729
2830 it ( 'should call opn once when spawning a plot' , ( ) => {
29- server . spawn ( { 0 : {
30- opened : false ,
31- pending : false ,
32- plots : [ ]
33- } } ) ;
31+ server . spawn ( {
32+ 0 : {
33+ opened : false ,
34+ pending : false ,
35+ plots : [ ] ,
36+ } ,
37+ } ) ;
3438
3539 expect ( exec ) . toHaveBeenCalledTimes ( 1 ) ;
3640 } ) ;
3741
38- it ( 'should serve the data' , ( done ) => {
39- server . spawn ( { 0 : validData } ) ;
42+ it ( 'should serve the data' , done => {
43+ server . spawn ( { 0 : validData } ) ;
4044
4145 request ( `http://localhost:${ port } /data/0` , ( err , response , body ) => {
42- expect ( JSON . parse ( body ) ) . toEqual ( [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] ) ;
46+ expect ( JSON . parse ( body ) ) . toEqual ( [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] ) ;
4347 done ( ) ;
4448 } ) ;
4549 } ) ;
4650
47- it ( 'should spawn two times but listen just once' , ( done ) => {
48- const data = { 0 : validData } ;
51+ it ( 'should spawn two times but listen just once' , done => {
52+ const data = { 0 : validData } ;
4953
5054 server . spawn ( data ) ;
5155 server . spawn ( data ) ;
5256
5357 request ( `http://localhost:${ port } /data/0` , ( err , response , body ) => {
54- expect ( JSON . parse ( body ) ) . toEqual ( [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] ) ;
58+ expect ( JSON . parse ( body ) ) . toEqual ( [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] ) ;
5559 done ( ) ;
5660 } ) ;
5761 } ) ;
5862
59- it ( 'should serve the website and return 404 if html file not found' , ( done ) => {
60- server . spawn ( { 0 : validData } ) ;
63+ it ( 'should serve the website and return 404 if html file not found' , done => {
64+ server . spawn ( { 0 : validData } ) ;
6165
6266 request ( `http://localhost:${ port } /plots/0/index.html` , ( err , response , body ) => {
6367 expect ( response . statusCode ) . toBe ( 404 ) ;
6468 done ( ) ;
6569 } ) ;
6670 } ) ;
6771
68- it ( 'should serve the nodeplotlib script and return 404 if file not found' , ( done ) => {
69- server . spawn ( { 0 : validData } ) ;
72+ it ( 'should serve the nodeplotlib script and return 404 if file not found' , done => {
73+ server . spawn ( { 0 : validData } ) ;
7074
7175 request ( `http://localhost:${ port } /plots/0/nodeplotlib.min.js` , ( err , response , body ) => {
7276 expect ( response . statusCode ) . toBe ( 404 ) ;
7377 done ( ) ;
7478 } ) ;
7579 } ) ;
7680
77- it ( 'should serve the plotly.min.js script and return 404 if file not found' , ( done ) => {
78- server . spawn ( { 0 : validData } ) ;
81+ it ( 'should serve the plotly.min.js script and return 404 if file not found' , done => {
82+ server . spawn ( { 0 : validData } ) ;
7983
8084 request ( `http://localhost:${ port } /plots/0/plotly.min.js` , ( err , response , body ) => {
8185 expect ( response . statusCode ) . toBe ( 404 ) ;
8286 done ( ) ;
8387 } ) ;
8488 } ) ;
8589
86- it ( ' should not close the webserver, if one plot hasn\ 't got its data' , ( done ) => {
90+ it ( " should not close the webserver, if one plot hasn't got its data" , done => {
8791 server . spawn ( {
88- 0 : { pending : false , opened : false , plots : [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] } ,
89- 1 : { pending : false , opened : false , plots : [ { data : [ { x : [ 1 ] , y : [ 3 ] } ] } ] }
92+ 0 : { pending : false , opened : false , plots : [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] } ,
93+ 1 : { pending : false , opened : false , plots : [ { data : [ { x : [ 1 ] , y : [ 3 ] } ] } ] } ,
9094 } ) ;
9195
9296 request ( `http://localhost:${ port } /data/0` , ( err , response , body ) => {
93- expect ( JSON . parse ( body ) ) . toEqual ( [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] ) ;
97+ expect ( JSON . parse ( body ) ) . toEqual ( [ { data : [ { x : [ 1 ] , y : [ 2 ] } ] } ] ) ;
9498
9599 request ( `http://localhost:${ port } /data/1` , ( err1 , response1 , body1 ) => {
96- expect ( JSON . parse ( body1 ) ) . toEqual ( [ { data : [ { x : [ 1 ] , y : [ 3 ] } ] } ] ) ;
100+ expect ( JSON . parse ( body1 ) ) . toEqual ( [ { data : [ { x : [ 1 ] , y : [ 3 ] } ] } ] ) ;
97101 done ( ) ;
98102 } ) ;
99103 } ) ;
100104 } ) ;
101105
102- it ( 'should return 404 if routes not matching' , ( done ) => {
103- const data = { 0 : validData } ;
106+ it ( 'should not close webserver until plot data is entirely transferred' , done => {
107+ const elements = 100000 ;
108+ const plot = [ { data : [ { x : new Array ( elements ) . fill ( 1 ) , y : new Array ( elements ) . fill ( 1 ) } ] } ] ;
109+ server . spawn ( {
110+ 0 : { pending : false , opened : false , plots : plot } ,
111+ } ) ;
112+
113+ request ( `http://localhost:${ port } /data/0` , ( err , response , body ) => {
114+ expect ( JSON . parse ( body ) ) . toEqual ( plot ) ;
115+ done ( ) ;
116+ } ) ;
117+ } ) ;
118+
119+ it ( 'should return 404 if routes not matching' , done => {
120+ const data = { 0 : validData } ;
104121
105122 server . spawn ( data ) ;
106123
0 commit comments