1- import { get , Server } from 'http' ;
1+ // tslint:disable-next-line:no-implicit-dependencies
2+ import axios from 'axios' ;
3+ // tslint:disable-next-line:no-submodule-imports
4+ import * as simpleGit from 'simple-git/promise' ;
25import { startServer } from '../src/server' ;
36
7+ jest . mock ( 'simple-git/promise' ) ;
8+
49describe ( 'main' , ( ) => {
5- const port = 15523 ;
6- const baseUrl = `http://localhost:${ port } ` ;
7- let server : Server ;
10+ let port ;
11+ let baseUrl ;
12+ let server ;
13+ let sg ;
814
9- beforeEach ( ( ) => {
15+ beforeEach ( async ( ) => {
1016 try {
11- server = startServer ( port ) ;
17+ // tslint:disable
18+ port = 3000 + Math . ceil ( Math . random ( ) * 1000 ) ;
19+ // tslint:enable
20+ process . env . PORT = port ;
21+ baseUrl = `http://localhost:${ port } ` ;
22+ server = await startServer ( ) ;
23+ sg = simpleGit ( ) ;
24+
25+ return server ;
1226 } catch ( error ) {
1327 console . log ( error , `can't start the server` ) ;
1428 }
1529 } ) ;
1630
17- afterEach ( ( ) => {
18- server . close ( ( ) => {
19- console . log ( 'server closed' ) ;
20- } ) ;
31+ afterEach ( async ( ) => {
32+ server . close ( ) ;
33+ sg . _dispose ( ) ;
2134 } )
2235
23- it ( 'GET /' , done => {
24- get ( baseUrl , res => {
25- expect ( res . statusCode ) . toBe ( 200 ) ;
36+ // tslint:disable:mocha-unneeded-done
37+ it ( 'GET /' , async ( done ) => {
38+ const res = await axios ( baseUrl ) ;
39+ expect ( res . status ) . toBe ( 200 ) ;
40+ expect ( res . data ) . toBe ( 'Hello World!' ) ;
41+ done ( ) ;
42+ } ) ;
43+
44+ describe ( 'POST /commit' , ( ) => {
45+ // tslint:disable-next-line:mocha-unneeded-done
46+ it ( 'complete payload' , async ( done ) => {
47+ const res = await axios ( {
48+ url : `http://localhost:${ port } /commit` ,
49+ method : 'POST' ,
50+ data : payload
51+ } ) ;
52+ expect ( res . status ) . toBe ( 200 ) ;
53+ expect ( res . data ) . toBe ( 'done' ) ;
54+ const gitStatus = await sg . status ( ) ;
55+ expect (
56+ gitStatus . files . map ( file => file . path )
57+ ) . toEqual (
58+ payload . files . map ( file => file . path )
59+ ) ;
2660 done ( ) ;
2761 } ) ;
2862 } ) ;
2963
30- // it('POST /commit', done => {
31- // // try {
32-
33- // const req = request({host: 'localhost', port: port, method: 'POST', path: '/commit'}, res => {
34- // console.log('res!!', res);
35- // res.on('data', chunk => {
36- // console.log('chunk', chunk);
37- // expect(chunk).toBe('done');
38- // done();
39- // });
40- // });
41-
42- // // console.log('req', req);
43-
44- // req.write(JSON.stringify({
45- // "remoteRepo": "moshfeu/commit-bot-test",
46- // "token": "7cb950575b914d7c36a8de37d9db056671e92788",
47- // "commitMessage": "yet another commit",
48- // "files": [
49- // {
50- // "path": "folder1/folder2/file3.js",
51- // "content": "alert('blabla123')"
52- // },
53- // {
54- // "path": "folder1/folder3/file4.ts",
55- // "content": "import * as path from 'path';"
56- // }
57- // ]
58- // }));
64+ it ( 'not complete payload' , async ( done ) => {
65+ const res = await axios ( {
66+ url : `http://localhost:${ port } /commit` ,
67+ method : 'POST' ,
68+ validateStatus : ( ) => true
69+ } ) ;
5970
60- // // console.log('req done' );
61- // / / } catch (error) {
62- // // console.log('error', error );
63- // / / }
71+ expect ( res . status ) . toBe ( 500 ) ;
72+ done ( ) ;
73+ } ) ;
74+ } ) ;
6475
65- // // expect(1).toBe(1);
66- // // done();
67- // });
68- } ) ;
76+ const payload = {
77+ "remoteRepo" : "moshfeu/commit-bot-test" ,
78+ "token" : "7cb950575b914d7c36a8de37d9db056671e92788" ,
79+ "commitMessage" : "yet another commit" ,
80+ "files" : [
81+ {
82+ "path" : "folder1/folder2/file3.js" ,
83+ "content" : "alert('blabla123')"
84+ } ,
85+ {
86+ "path" : "folder1/folder3/file4.ts" ,
87+ "content" : "import * as path from 'path';"
88+ }
89+ ]
90+ } ;
0 commit comments