@@ -7,6 +7,7 @@ import marketplaceController from '../server/controllers/marketplaceController';
7
7
import app from '../server/server' ;
8
8
import mockData from '../mockData' ;
9
9
import { profileEnd } from 'console' ;
10
+ import { Projects } from '../server/models/reactypeModels' ;
10
11
const request = require ( 'supertest' ) ;
11
12
const mongoose = require ( 'mongoose' ) ;
12
13
const mockNext = jest . fn ( ) ; // Mock nextFunction
@@ -22,6 +23,9 @@ beforeAll(async () => {
22
23
} ) ;
23
24
24
25
afterAll ( async ( ) => {
26
+
27
+ const result = await Projects . deleteMany ( { } ) ; //clear the projects collection after tests are done
28
+ console . log ( `${ result . deletedCount } documents deleted.` ) ;
25
29
await mongoose . connection . close ( ) ;
26
30
} ) ;
27
31
@@ -31,38 +35,42 @@ describe('Server endpoint tests', () => {
31
35
expect ( response . status ) . toBe ( 200 ) ;
32
36
expect ( response . text ) . toBe ( 'test request is working' ) ;
33
37
} ) ;
34
- describe ( 'Marketplace endpoint testing' , ( ) => {
35
- it ( 'get requests to /getMarketplaceProjects should return an array of projects' , async ( ) => {
36
- const response = await request ( app ) . get ( '/getMarketplaceProjects' ) ;
37
- expect ( response . status ) . toBe ( 200 ) ;
38
- expect ( Array . isArray ( response . body ) ) . toBe ( true ) ;
39
- } ) ;
40
- it ( 'the return array should be populated with project objects' , async ( ) => {
41
- const response = await request ( app ) . get ( '/getMarketplaceProjects' ) ;
42
- expect ( response . status ) . toBe ( 200 ) ;
43
- expect ( Array . isArray ( response . body ) ) . toBe ( true ) ;
38
+
39
+ // test saveProject endpoint
40
+ describe ( '/login' , ( ) => {
41
+ describe ( '/POST' , ( ) => {
42
+ it ( 'responds with a status of 200 and json object equal to project sent' , async ( ) => {
43
+ return request ( app )
44
+ . post ( '/login' )
45
+ . set ( 'Cookie' , [ `ssid=${ user . userId } ` ] )
46
+ . set ( 'Accept' , 'application/json' )
47
+ . send ( projectToSave )
48
+ . expect ( 200 )
49
+ . expect ( 'Content-Type' , / a p p l i c a t i o n \/ j s o n / )
50
+ . then ( ( res ) => expect ( res . body . name ) . toBe ( projectToSave . name ) ) ;
51
+ } ) ;
52
+ // });
44
53
} ) ;
45
54
} ) ;
55
+
46
56
// test saveProject endpoint
47
57
describe ( '/saveProject' , ( ) => {
48
58
describe ( '/POST' , ( ) => {
49
59
it ( 'responds with a status of 200 and json object equal to project sent' , async ( ) => {
50
- // const response = await request(app).post('/saveProject').set('Accept', 'application/json').send(projectToSave);
51
- // console.log(response);
52
- // console.log(response.body);
53
- // expect(response.status).toBe(200);
54
60
return request ( app )
55
61
. post ( '/saveProject' )
62
+ . set ( 'Cookie' , [ `ssid=${ user . userId } ` ] )
56
63
. set ( 'Accept' , 'application/json' )
57
64
. send ( projectToSave )
58
65
. expect ( 200 )
59
66
. expect ( 'Content-Type' , / a p p l i c a t i o n \/ j s o n / )
60
67
. then ( ( res ) => expect ( res . body . name ) . toBe ( projectToSave . name ) ) ;
61
68
} ) ;
62
69
// });
70
+ } ) ;
63
71
} ) ;
64
72
// test getProjects endpoint
65
- describe ( '/getProjects' , ( ) => {
73
+ xdescribe ( '/getProjects' , ( ) => {
66
74
describe ( 'POST' , ( ) => {
67
75
it ( 'responds with status of 200 and json object equal to an array of user projects' , ( ) => {
68
76
return request ( app )
@@ -79,13 +87,12 @@ describe('Server endpoint tests', () => {
79
87
} ) ;
80
88
} ) ;
81
89
// test deleteProject endpoint
82
- describe ( '/deleteProject' , ( ) => {
90
+ xdescribe ( '/deleteProject' , ( ) => {
83
91
describe ( 'DELETE' , ( ) => {
84
92
it ( 'responds with status of 200 and json object equal to deleted project' , async ( ) => {
85
93
const response : Response = await request ( app ) . post ( '/getProjects' ) . set ( 'Accept' , 'application/json' ) . send ( { userId : projectToSave . userId } ) ;
86
94
const _id : String = response . body [ 0 ] . _id ;
87
95
const userId : String = user . userId ;
88
- console . log ( _id , userId ) ;
89
96
return request ( app )
90
97
. delete ( '/deleteProject' )
91
98
. set ( 'Content-Type' , 'application/json' )
@@ -95,7 +102,104 @@ describe('Server endpoint tests', () => {
95
102
} ) ;
96
103
} ) ;
97
104
} ) ;
98
- } ) ;
105
+
106
+ //test publishProject endpoint
107
+ xdescribe ( '/publishProject' , ( ) => {
108
+ describe ( 'POST' , ( ) => {
109
+ it ( 'responds with status of 200 and json object equal to published project' , async ( ) => {
110
+
111
+ const projObj = await request ( app )
112
+ . post ( '/saveProject' )
113
+ . set ( 'Accept' , 'application/json' )
114
+ . send ( projectToSave )
115
+
116
+ const _id : String = projObj . body . _id ;
117
+ const project : String = projObj . body . project ;
118
+ const comments : String = projObj . body . comments ;
119
+ const username : String = projObj . body . username ;
120
+ const name : String = projObj . body . name ;
121
+ const userId : String = user . userId ;
122
+ return request ( app )
123
+ . post ( '/publishProject' )
124
+ . set ( 'Content-Type' , 'application/json' )
125
+ . send ( { _id, project, comments, userId, username, name } ) //_id, project, comments, userId, username, name
126
+ . expect ( 200 )
127
+ . then ( ( res ) => {
128
+ expect ( res . body . _id ) . toBe ( _id )
129
+ expect ( res . body . published ) . toBe ( true ) ;
130
+ } ) ;
131
+ } ) ;
132
+ } ) ;
133
+ } ) ;
134
+
135
+ //test getMarketplaceProjects endpoint
136
+ xdescribe ( '/getMarketplaceProjects' , ( ) => { //most recent project should be the one from publishProject
137
+ describe ( 'GET' , ( ) => {
138
+ it ( 'responds with status of 200 and json object equal to unpublished project' , async ( ) => {
139
+ return request ( app )
140
+ . get ( '/getMarketplaceProjects' )
141
+ . set ( 'Content-Type' , 'application/json' )
142
+ . expect ( 200 )
143
+ . then ( ( res ) => {
144
+
145
+ expect ( Array . isArray ( res . body ) ) . toBe ( true ) ;
146
+ expect ( res . body [ 0 ] . _id ) . toBeTruthy ;
147
+ } ) ;
148
+ } ) ;
149
+ } ) ;
150
+ } ) ;
151
+
152
+ //test cloneProject endpoint
153
+ xdescribe ( '/cloneProject/:docId' , ( ) => {
154
+ describe ( 'GET' , ( ) => {
155
+ it ( 'responds with status of 200 and json object equal to cloned project' , async ( ) => {
156
+
157
+ const projObj = await request ( app )
158
+ . get ( '/getMarketplaceProjects' )
159
+ . set ( 'Content-Type' , 'application/json' )
160
+ console . log ( projObj . body )
161
+ // const _id: String = projObj.body._id;
162
+ // const project: String = projObj.body.project;
163
+ // const comments: String = projObj.body.comments;
164
+ // const username: String = projObj.body.username;
165
+ // const name: String = projObj.body.name;
166
+ // const userId: String = user.userId;
167
+ return request ( app )
168
+ . get ( `/cloneProject/${ projObj . body [ 0 ] . _id } ` )
169
+ . set ( 'Cookie' , [ `ssid=${ user . userId } ` ] ) // Set the cookie
170
+ . query ( { username : user . username } )
171
+ . expect ( 200 )
172
+ . then ( ( res ) => {
173
+ expect ( res . body . forked ) . toBeTruthy ;
174
+ expect ( res . body . username ) . toBe ( user . username ) ;
175
+ } ) ;
176
+ } ) ;
177
+ } ) ;
178
+ } ) ;
179
+
180
+ //test unpublishProject endpoint
181
+ xdescribe ( '/unpublishProject' , ( ) => {
182
+ describe ( 'PATCH' , ( ) => {
183
+ it ( 'responds with status of 200 and json object equal to unpublished project' , async ( ) => {
184
+ const response : Response = await request ( app ) . post ( '/getProjects' ) . set ( 'Accept' , 'application/json' ) . send ( { userId : projectToSave . userId } ) ; //most recent project should be the one from publishProject
185
+ const _id : String = response . body [ 0 ] . _id ;
186
+ const userId : String = user . userId ;
187
+ return request ( app )
188
+ . patch ( '/unpublishProject' )
189
+ . set ( 'Content-Type' , 'application/json' )
190
+ . send ( { _id, userId } ) //_id, project, comments, userId, username, name
191
+ . expect ( 200 )
192
+ . then ( ( res ) => {
193
+ expect ( res . body . _id ) . toBe ( _id )
194
+ expect ( res . body . published ) . toBe ( false ) ;
195
+ } ) ; // @Denton might want to check more of these fields
196
+ } ) ;
197
+ } ) ;
198
+ } ) ;
199
+
200
+
201
+
202
+
99
203
} ) ;
100
204
101
205
0 commit comments