3
3
*/
4
4
import { Request , Response } from 'jest-express' ;
5
5
6
- import { createMock } from '../../../models/user' ;
6
+ import User from '../../../models/user' ;
7
7
import getProjectsForUser , {
8
8
apiGetProjectsForUser
9
9
} from '../../project.controller/getProjectsForUser' ;
@@ -12,14 +12,8 @@ jest.mock('../../../models/user');
12
12
jest . mock ( '../../aws.controller' ) ;
13
13
14
14
describe ( 'project.controller' , ( ) => {
15
- let UserMock ;
16
-
17
15
beforeEach ( ( ) => {
18
- UserMock = createMock ( ) ;
19
- } ) ;
20
-
21
- afterEach ( ( ) => {
22
- UserMock . restore ( ) ;
16
+ User . findByUsername = jest . fn ( ) ;
23
17
} ) ;
24
18
25
19
describe ( 'getProjectsForUser()' , ( ) => {
@@ -42,117 +36,79 @@ describe('project.controller', () => {
42
36
promise . then ( expectations , expectations ) . catch ( expectations ) ;
43
37
} ) ;
44
38
45
- it ( 'returns 404 if user does not exist' , ( done ) => {
39
+ it ( 'returns 404 if user does not exist' , async ( ) => {
46
40
const request = new Request ( ) ;
47
41
request . setParams ( { username : 'abc123' } ) ;
48
42
const response = new Response ( ) ;
49
43
50
- UserMock . expects ( 'findOne' )
51
- . withArgs ( { username : 'abc123' } )
52
- . resolves ( null ) ;
53
-
54
- const promise = getProjectsForUser ( request , response ) ;
55
-
56
- function expectations ( ) {
57
- expect ( response . status ) . toHaveBeenCalledWith ( 404 ) ;
58
- expect ( response . json ) . toHaveBeenCalledWith ( {
59
- message : 'User with that username does not exist.'
60
- } ) ;
44
+ await User . findByUsername . mockResolvedValue ( null ) ;
61
45
62
- done ( ) ;
63
- }
46
+ await getProjectsForUser ( request , response ) ;
64
47
65
- promise . then ( expectations , expectations ) . catch ( expectations ) ;
48
+ expect ( response . status ) . toHaveBeenCalledWith ( 404 ) ;
49
+ expect ( response . json ) . toHaveBeenCalledWith ( {
50
+ message : 'User with that username does not exist.'
51
+ } ) ;
66
52
} ) ;
67
53
68
- it ( 'returns 500 on other errors' , ( done ) => {
54
+ it ( 'returns 500 on other errors' , async ( ) => {
69
55
const request = new Request ( ) ;
70
56
request . setParams ( { username : 'abc123' } ) ;
71
57
const response = new Response ( ) ;
72
58
73
- UserMock . expects ( 'findOne' )
74
- . withArgs ( { username : 'abc123' } )
75
- . rejects ( new Error ( ) ) ;
59
+ await User . findByUsername . mockRejectedValue ( new Error ( ) ) ;
76
60
77
- const promise = getProjectsForUser ( request , response ) ;
78
-
79
- function expectations ( ) {
80
- expect ( response . json ) . toHaveBeenCalledWith ( {
81
- message : 'Error fetching projects'
82
- } ) ;
83
- expect ( response . status ) . toHaveBeenCalledWith ( 500 ) ;
84
-
85
- done ( ) ;
86
- }
61
+ await getProjectsForUser ( request , response ) ;
87
62
88
- promise . then ( expectations , expectations ) . catch ( expectations ) ;
63
+ expect ( response . json ) . toHaveBeenCalledWith ( {
64
+ message : 'Error fetching projects'
65
+ } ) ;
66
+ expect ( response . status ) . toHaveBeenCalledWith ( 500 ) ;
89
67
} ) ;
90
68
} ) ;
91
69
92
70
describe ( 'apiGetProjectsForUser()' , ( ) => {
93
- it ( 'returns validation error if username not provided' , ( done ) => {
71
+ it ( 'returns validation error if username not provided' , async ( ) => {
94
72
const request = new Request ( ) ;
95
73
request . setParams ( { } ) ;
96
74
const response = new Response ( ) ;
97
75
98
- const promise = apiGetProjectsForUser ( request , response ) ;
99
-
100
- function expectations ( ) {
101
- expect ( response . status ) . toHaveBeenCalledWith ( 422 ) ;
102
- expect ( response . json ) . toHaveBeenCalledWith ( {
103
- message : 'Username not provided'
104
- } ) ;
105
-
106
- done ( ) ;
107
- }
76
+ await apiGetProjectsForUser ( request , response ) ;
108
77
109
- promise . then ( expectations , expectations ) . catch ( expectations ) ;
78
+ expect ( response . status ) . toHaveBeenCalledWith ( 422 ) ;
79
+ expect ( response . json ) . toHaveBeenCalledWith ( {
80
+ message : 'Username not provided'
81
+ } ) ;
110
82
} ) ;
111
83
112
- it ( 'returns 404 if user does not exist' , ( done ) => {
84
+ it ( 'returns 404 if user does not exist' , async ( ) => {
113
85
const request = new Request ( ) ;
114
86
request . setParams ( { username : 'abc123' } ) ;
115
87
const response = new Response ( ) ;
116
88
117
- UserMock . expects ( 'findOne' )
118
- . withArgs ( { username : 'abc123' } )
119
- . resolves ( null ) ;
120
-
121
- const promise = apiGetProjectsForUser ( request , response ) ;
122
-
123
- function expectations ( ) {
124
- expect ( response . status ) . toHaveBeenCalledWith ( 404 ) ;
125
- expect ( response . json ) . toHaveBeenCalledWith ( {
126
- message : 'User with that username does not exist.'
127
- } ) ;
89
+ await User . findByUsername . mockResolvedValue ( null ) ;
128
90
129
- done ( ) ;
130
- }
91
+ await apiGetProjectsForUser ( request , response ) ;
131
92
132
- promise . then ( expectations , expectations ) . catch ( expectations ) ;
93
+ expect ( response . status ) . toHaveBeenCalledWith ( 404 ) ;
94
+ expect ( response . json ) . toHaveBeenCalledWith ( {
95
+ message : 'User with that username does not exist.'
96
+ } ) ;
133
97
} ) ;
134
98
135
- it ( 'returns 500 on other errors' , ( done ) => {
99
+ it ( 'returns 500 on other errors' , async ( ) => {
136
100
const request = new Request ( ) ;
137
101
request . setParams ( { username : 'abc123' } ) ;
138
102
const response = new Response ( ) ;
139
103
140
- UserMock . expects ( 'findOne' )
141
- . withArgs ( { username : 'abc123' } )
142
- . rejects ( new Error ( ) ) ;
143
-
144
- const promise = apiGetProjectsForUser ( request , response ) ;
145
-
146
- function expectations ( ) {
147
- expect ( response . status ) . toHaveBeenCalledWith ( 500 ) ;
148
- expect ( response . json ) . toHaveBeenCalledWith ( {
149
- message : 'Error fetching projects'
150
- } ) ;
104
+ await User . findByUsername . mockRejectedValue ( new Error ( ) ) ;
151
105
152
- done ( ) ;
153
- }
106
+ await apiGetProjectsForUser ( request , response ) ;
154
107
155
- promise . then ( expectations , expectations ) . catch ( expectations ) ;
108
+ expect ( response . status ) . toHaveBeenCalledWith ( 500 ) ;
109
+ expect ( response . json ) . toHaveBeenCalledWith ( {
110
+ message : 'Error fetching projects'
111
+ } ) ;
156
112
} ) ;
157
113
} ) ;
158
114
} ) ;
0 commit comments