@@ -3,69 +3,73 @@ const { listen } = require('./utils');
33
44const db = require ( '../src/db' ) ;
55const service = require ( '../src/handler' ) ;
6- let url ;
6+ let server ;
77
8- beforeAll ( ( ) => {
8+ beforeAll ( async ( ) => {
99 db . initDbAdapter ( { adapter : 'memory' } ) ;
10+ server = await listen ( service ( { adapter : 'memory' } ) ) ;
1011} ) ;
1112
12- beforeEach ( async ( ) => {
13+ afterAll ( ( ) => {
14+ server . close ( ) ;
15+ } ) ;
16+
17+ beforeEach ( ( ) => {
1318 db . clear ( ) ;
14- url = await listen ( service ( { adapter : 'memory' } ) ) ;
1519} ) ;
1620
1721describe ( 'single' , ( ) => {
1822 it ( 'should set the views of a non-existant path to one' , async ( ) => {
19- const body = JSON . parse ( await request ( `${ url } /nonexistant` ) ) ;
23+ const body = JSON . parse ( await request ( `${ server . url } /nonexistant` ) ) ;
2024 expect ( body . views ) . toEqual ( 1 ) ;
2125 } ) ;
2226
2327 it ( 'should increment the views of an existant path' , async ( ) => {
24- await request ( `${ url } /existant` ) ;
25- const body = JSON . parse ( await request ( `${ url } /existant` ) ) ;
28+ await request ( `${ server . url } /existant` ) ;
29+ const body = JSON . parse ( await request ( `${ server . url } /existant` ) ) ;
2630 expect ( body . views ) . toEqual ( 2 ) ;
2731 } ) ;
2832
2933 it ( 'should return 0 views on a non-existant path if inc is set to false' , async ( ) => {
30- const body = JSON . parse ( await request ( `${ url } /path?inc=false` ) ) ;
34+ const body = JSON . parse ( await request ( `${ server . url } /path?inc=false` ) ) ;
3135 expect ( body . views ) . toEqual ( 0 ) ;
3236 } ) ;
3337
3438 it ( 'should not increment the views of an existant path if inc is set to false' , async ( ) => {
35- await request ( `${ url } /existant` ) ;
36- const body = JSON . parse ( await request ( `${ url } /existant?inc=false` ) ) ;
39+ await request ( `${ server . url } /existant` ) ;
40+ const body = JSON . parse ( await request ( `${ server . url } /existant?inc=false` ) ) ;
3741 expect ( body . views ) . toEqual ( 1 ) ;
3842 } ) ;
3943
4044 it ( 'should not return anything for a POST request' , async ( ) => {
41- const body = await request . post ( `${ url } /existant` ) ;
45+ const body = await request . post ( `${ server . url } /existant` ) ;
4246 expect ( body ) . toEqual ( '' ) ;
4347 } ) ;
4448} ) ;
4549
4650describe ( 'all' , ( ) => {
4751 it ( 'should return an empty array if no previous views exist' , async ( ) => {
48- const body = JSON . parse ( await request ( `${ url } /?all=true` ) ) ;
52+ const body = JSON . parse ( await request ( `${ server . url } /?all=true` ) ) ;
4953 expect ( body . data ) . toEqual ( { } ) ;
5054 expect ( body . time ) . toBeDefined ( ) ;
5155 } ) ;
5256
5357 it ( 'should return previous views of one route' , async ( ) => {
54- await request ( `${ url } /route` ) ;
55- await request ( `${ url } /route` ) ;
56- const body = JSON . parse ( await request ( `${ url } /?all=true` ) ) ;
58+ await request ( `${ server . url } /route` ) ;
59+ await request ( `${ server . url } /route` ) ;
60+ const body = JSON . parse ( await request ( `${ server . url } /?all=true` ) ) ;
5761 expect ( Object . keys ( body . data ) . length ) . toBe ( 1 ) ;
5862 expect ( body . data [ '/route' ] . views ) . toBeDefined ( ) ;
5963 expect ( body . data [ '/route' ] . views . length ) . toBe ( 2 ) ;
6064 } ) ;
6165
6266 it ( 'should return previous views of all routes' , async ( ) => {
63- await request ( `${ url } /route` ) ;
64- await request ( `${ url } /route` ) ;
65- await request ( `${ url } /route2` ) ;
66- await request ( `${ url } /route2` ) ;
67- await request ( `${ url } /route2` ) ;
68- const body = JSON . parse ( await request ( `${ url } /?all=true` ) ) ;
67+ await request ( `${ server . url } /route` ) ;
68+ await request ( `${ server . url } /route` ) ;
69+ await request ( `${ server . url } /route2` ) ;
70+ await request ( `${ server . url } /route2` ) ;
71+ await request ( `${ server . url } /route2` ) ;
72+ const body = JSON . parse ( await request ( `${ server . url } /?all=true` ) ) ;
6973 expect ( Object . keys ( body . data ) . length ) . toBe ( 2 ) ;
7074 expect ( body . data [ '/route' ] . views ) . toBeDefined ( ) ;
7175 expect ( body . data [ '/route' ] . views . length ) . toBe ( 2 ) ;
@@ -75,19 +79,19 @@ describe('all', () => {
7579
7680 describe ( 'filtering' , ( ) => {
7781 it ( 'should filter based on pathname' , async ( ) => {
78- await request ( `${ url } /rover` ) ;
79- await request ( `${ url } /route` ) ;
80- const body = JSON . parse ( await request ( `${ url } /rover?all=true` ) ) ;
82+ await request ( `${ server . url } /rover` ) ;
83+ await request ( `${ server . url } /route` ) ;
84+ const body = JSON . parse ( await request ( `${ server . url } /rover?all=true` ) ) ;
8185 expect ( Object . keys ( body . data ) . length ) . toBe ( 1 ) ;
8286 expect ( body . data [ '/rover' ] . views ) . toBeDefined ( ) ;
8387 expect ( body . data [ '/rover' ] . views . length ) . toBe ( 1 ) ;
8488 } ) ;
8589
8690 it ( 'should filter based on starting with pathname' , async ( ) => {
87- await request ( `${ url } /rover` ) ;
88- await request ( `${ url } /rover2` ) ;
89- await request ( `${ url } /route` ) ;
90- const body = JSON . parse ( await request ( `${ url } /rover?all=true` ) ) ;
91+ await request ( `${ server . url } /rover` ) ;
92+ await request ( `${ server . url } /rover2` ) ;
93+ await request ( `${ server . url } /route` ) ;
94+ const body = JSON . parse ( await request ( `${ server . url } /rover?all=true` ) ) ;
9195 expect ( Object . keys ( body . data ) . length ) . toBe ( 2 ) ;
9296 expect ( body . data [ '/rover' ] . views ) . toBeDefined ( ) ;
9397 expect ( body . data [ '/rover' ] . views . length ) . toBe ( 1 ) ;
@@ -112,7 +116,7 @@ describe('all', () => {
112116
113117 const mapToIsoString = view => new Date ( view . time ) . toISOString ( ) ;
114118 const body = JSON . parse (
115- await request ( `${ url } /rover?all=true&before=${ before } &after=${ after } ` )
119+ await request ( `${ server . url } /rover?all=true&before=${ before } &after=${ after } ` )
116120 ) ;
117121 expect ( body . data [ '/rover' ] . views . map ( mapToIsoString ) ) . toEqual ( [
118122 '2017-01-01T09:20:00.000Z' ,
0 commit comments