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