Skip to content

Commit 0952efa

Browse files
committed
added controller tests
1 parent 185093b commit 0952efa

File tree

2 files changed

+135
-5
lines changed

2 files changed

+135
-5
lines changed

src/controllers/note.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class NoteController {
1313
let result = await this.noteService.findAll();
1414
res.status( httpStatus.OK).json( result );
1515
} catch (ex) {
16-
console.log( ex );
16+
// console.log( ex );
1717
res.status(httpStatus.INTERNAL_SERVER_ERROR).json( { message: ex.message } );
1818
}
1919
}
@@ -24,7 +24,7 @@ class NoteController {
2424
let status = result ? httpStatus.OK : httpStatus.NO_CONTENT;
2525
res.status( status ).json( result );
2626
} catch (ex) {
27-
console.log( ex );
27+
// console.log( ex );
2828
res.status(httpStatus.INTERNAL_SERVER_ERROR).json( { message: ex.message } );
2929
}
3030
}
@@ -34,7 +34,7 @@ class NoteController {
3434
let result = await this.noteService.save( req.body );
3535
res.status( httpStatus.CREATED ).json( result );
3636
} catch (ex) {
37-
console.log( ex );
37+
// console.log( ex );
3838
res.status(httpStatus.INTERNAL_SERVER_ERROR).json( { message: ex.message } );
3939
}
4040
}
@@ -44,7 +44,7 @@ class NoteController {
4444
await this.noteService.delete( req.params.id );
4545
res.status( httpStatus.ACCEPTED ).end();
4646
} catch (ex) {
47-
console.log( ex );
47+
// console.log( ex );
4848
res.status(httpStatus.INTERNAL_SERVER_ERROR).json( { message: ex.message } );
4949
}
5050
}
@@ -55,7 +55,7 @@ class NoteController {
5555
let result = await this.noteService.update( req.body );
5656
res.status( httpStatus.OK ).json( result );
5757
} catch (ex) {
58-
console.log( ex );
58+
// console.log( ex );
5959
res.status(httpStatus.INTERNAL_SERVER_ERROR).json( { message: ex.message } );
6060
}
6161
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import "mocha";
2+
import { expect } from "chai";
3+
import * as sinon from "sinon";
4+
import NoteController from "../../../src/controllers/note";
5+
import { INoteService } from "../../../src/services/INoteService";
6+
import { Note } from "../../../src/models/note";
7+
import { newNotes } from "../../seed";
8+
9+
10+
11+
let mockNoteService: INoteService = {
12+
findAll (): Promise<Note[]> { return Promise.resolve(<Note[]>newNotes); },
13+
findById(id): Promise<Note> { return Promise.resolve(<Note>newNotes[0]); },
14+
save(obj): Promise<Note> { return Promise.resolve(<Note>newNotes[0]); },
15+
update(obj): Promise<Note> { return Promise.resolve(<Note>newNotes[0]); },
16+
delete(id): void { }
17+
};
18+
19+
20+
describe("Note controller", function() {
21+
22+
it("Should get all notes", async function() {
23+
let res = { status (stat) { return this; },
24+
json (result) {
25+
expect(result).to.be.instanceof(Array);
26+
(<Note>result[0]).noteTitle.should.equal("test123");
27+
}
28+
};
29+
30+
let findAllSpy = sinon.spy(mockNoteService, "findAll");
31+
const noteController = new NoteController(mockNoteService);
32+
33+
await noteController.getNotes(<any>{}, <any>res, <any>{});
34+
35+
findAllSpy.calledOnce.should.be.true;
36+
findAllSpy.restore();
37+
});
38+
39+
it("Should return http status 500 when error occurs on getting all notes", async function() {
40+
let res = { status (stat) {
41+
stat.should.be.equal(500);
42+
return this; },
43+
json () { }
44+
};
45+
46+
let findAllStub = sinon.stub(mockNoteService, "findAll")
47+
.throws("something failed");
48+
const noteController = new NoteController(mockNoteService);
49+
50+
await noteController.getNotes(<any>{}, <any>res, <any>{});
51+
52+
findAllStub.calledOnce.should.be.true;
53+
findAllStub.restore();
54+
});
55+
56+
it("Should find a note", async function() {
57+
let req = { params: { id: "000ABC" }};
58+
let res = { status (stat) { return this; },
59+
json (result) {
60+
(<Note>result).noteTitle.should.equal("test123");
61+
}
62+
};
63+
64+
let findByIdSpy = sinon.spy(mockNoteService, "findById");
65+
const noteController = new NoteController(mockNoteService);
66+
67+
await noteController.findNote(<any>req, <any>res, <any>{});
68+
69+
findByIdSpy.calledOnce.should.be.true;
70+
findByIdSpy.firstCall.args[0].should.equal("000ABC");
71+
findByIdSpy.restore();
72+
});
73+
74+
it("Should save a note", async function() {
75+
let req = { body: newNotes[0] };
76+
let res = { status (stat) { return this; },
77+
json (result) {
78+
(<Note>result).noteTitle.should.equal("test123");
79+
}
80+
};
81+
82+
let saveSpy = sinon.spy(mockNoteService, "save");
83+
const noteController = new NoteController(mockNoteService);
84+
85+
await noteController.createNote(<any>req, <any>res, <any>{});
86+
87+
saveSpy.calledOnce.should.be.true;
88+
saveSpy.firstCall.args[0].noteTitle.should.equal("test123");
89+
saveSpy.restore();
90+
});
91+
92+
it("Should save a note", async function() {
93+
let req = { params: { id: "000ABC" } , body: newNotes[0] };
94+
let res = { status (stat) { return this; },
95+
json (result) {
96+
(<Note>result).noteTitle.should.equal("test123");
97+
}
98+
};
99+
100+
let updateSpy = sinon.spy(mockNoteService, "update");
101+
const noteController = new NoteController(mockNoteService);
102+
103+
await noteController.updateNote(<any>req, <any>res, <any>{});
104+
105+
updateSpy.calledOnce.should.be.true;
106+
updateSpy.firstCall.args[0].noteTitle.should.equal("test123");
107+
updateSpy.restore();
108+
});
109+
110+
111+
112+
it("Should delete a note", async function() {
113+
let req = { params: { id: "000ABC" }};
114+
let res = { status (stat) { stat.should.be.equal(202);
115+
return this; },
116+
json (result) {},
117+
end () {}
118+
};
119+
120+
let deleteSpy = sinon.spy(mockNoteService, "delete");
121+
const noteController = new NoteController(mockNoteService);
122+
123+
await noteController.deleteNote(<any>req, <any>res, <any>{});
124+
125+
deleteSpy.calledOnce.should.be.true;
126+
deleteSpy.firstCall.args[0].should.equal("000ABC");
127+
deleteSpy.restore();
128+
});
129+
130+
});

0 commit comments

Comments
 (0)