33 */
44import { Request , Response } from 'jest-express' ;
55
6- import { createMock } from '../../../models/user' ;
6+ import User from '../../../models/user' ;
77import getProjectsForUser , {
88 apiGetProjectsForUser
99} from '../../project.controller/getProjectsForUser' ;
@@ -12,14 +12,8 @@ jest.mock('../../../models/user');
1212jest . mock ( '../../aws.controller' ) ;
1313
1414describe ( 'project.controller' , ( ) => {
15- let UserMock ;
16-
1715 beforeEach ( ( ) => {
18- UserMock = createMock ( ) ;
19- } ) ;
20-
21- afterEach ( ( ) => {
22- UserMock . restore ( ) ;
16+ User . findByUsername = jest . fn ( ) ;
2317 } ) ;
2418
2519 describe ( 'getProjectsForUser()' , ( ) => {
@@ -42,117 +36,79 @@ describe('project.controller', () => {
4236 promise . then ( expectations , expectations ) . catch ( expectations ) ;
4337 } ) ;
4438
45- it ( 'returns 404 if user does not exist' , ( done ) => {
39+ it ( 'returns 404 if user does not exist' , async ( ) => {
4640 const request = new Request ( ) ;
4741 request . setParams ( { username : 'abc123' } ) ;
4842 const response = new Response ( ) ;
4943
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 ) ;
6145
62- done ( ) ;
63- }
46+ await getProjectsForUser ( request , response ) ;
6447
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+ } ) ;
6652 } ) ;
6753
68- it ( 'returns 500 on other errors' , ( done ) => {
54+ it ( 'returns 500 on other errors' , async ( ) => {
6955 const request = new Request ( ) ;
7056 request . setParams ( { username : 'abc123' } ) ;
7157 const response = new Response ( ) ;
7258
73- UserMock . expects ( 'findOne' )
74- . withArgs ( { username : 'abc123' } )
75- . rejects ( new Error ( ) ) ;
59+ await User . findByUsername . mockRejectedValue ( new Error ( ) ) ;
7660
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 ) ;
8762
88- promise . then ( expectations , expectations ) . catch ( expectations ) ;
63+ expect ( response . json ) . toHaveBeenCalledWith ( {
64+ message : 'Error fetching projects'
65+ } ) ;
66+ expect ( response . status ) . toHaveBeenCalledWith ( 500 ) ;
8967 } ) ;
9068 } ) ;
9169
9270 describe ( 'apiGetProjectsForUser()' , ( ) => {
93- it ( 'returns validation error if username not provided' , ( done ) => {
71+ it ( 'returns validation error if username not provided' , async ( ) => {
9472 const request = new Request ( ) ;
9573 request . setParams ( { } ) ;
9674 const response = new Response ( ) ;
9775
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 ) ;
10877
109- promise . then ( expectations , expectations ) . catch ( expectations ) ;
78+ expect ( response . status ) . toHaveBeenCalledWith ( 422 ) ;
79+ expect ( response . json ) . toHaveBeenCalledWith ( {
80+ message : 'Username not provided'
81+ } ) ;
11082 } ) ;
11183
112- it ( 'returns 404 if user does not exist' , ( done ) => {
84+ it ( 'returns 404 if user does not exist' , async ( ) => {
11385 const request = new Request ( ) ;
11486 request . setParams ( { username : 'abc123' } ) ;
11587 const response = new Response ( ) ;
11688
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 ) ;
12890
129- done ( ) ;
130- }
91+ await apiGetProjectsForUser ( request , response ) ;
13192
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+ } ) ;
13397 } ) ;
13498
135- it ( 'returns 500 on other errors' , ( done ) => {
99+ it ( 'returns 500 on other errors' , async ( ) => {
136100 const request = new Request ( ) ;
137101 request . setParams ( { username : 'abc123' } ) ;
138102 const response = new Response ( ) ;
139103
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 ( ) ) ;
151105
152- done ( ) ;
153- }
106+ await apiGetProjectsForUser ( request , response ) ;
154107
155- promise . then ( expectations , expectations ) . catch ( expectations ) ;
108+ expect ( response . status ) . toHaveBeenCalledWith ( 500 ) ;
109+ expect ( response . json ) . toHaveBeenCalledWith ( {
110+ message : 'Error fetching projects'
111+ } ) ;
156112 } ) ;
157113 } ) ;
158114} ) ;
0 commit comments