11jest . mock ( 'shared/supabase/init' ) ;
2+ jest . mock ( 'shared/supabase/notifications' ) ;
3+ jest . mock ( 'email/functions/helpers' ) ;
4+ jest . mock ( 'common/supabase/comment' ) ;
5+ jest . mock ( 'shared/utils' ) ;
6+ jest . mock ( 'common/user-notification-preferences' ) ;
7+ jest . mock ( 'shared/websockets/helpers' ) ;
28
39import * as supabaseInit from "shared/supabase/init" ;
410import { AuthedUser } from "api/helpers/endpoint" ;
11+ import * as sharedUtils from "shared/utils" ;
12+ import { createComment } from "api/create-comment" ;
13+ import * as notificationPrefs from "common/user-notification-preferences" ;
14+ import * as supabaseNotifications from "shared/supabase/notifications" ;
15+ import * as emailHelpers from "email/functions/helpers" ;
16+ import * as websocketHelpers from "shared/websockets/helpers" ;
17+ import { convertComment } from "common/supabase/comment" ;
518
619describe ( 'createComment' , ( ) => {
720 let mockPg : any ;
@@ -14,6 +27,12 @@ describe('createComment', () => {
1427
1528 ( supabaseInit . createSupabaseDirectClient as jest . Mock )
1629 . mockReturnValue ( mockPg ) ;
30+ ( supabaseNotifications . insertNotificationToSupabase as jest . Mock )
31+ . mockResolvedValue ( null ) ;
32+ ( emailHelpers . sendNewEndorsementEmail as jest . Mock )
33+ . mockResolvedValue ( null ) ;
34+ ( convertComment as jest . Mock )
35+ . mockResolvedValue ( null ) ;
1736 } ) ;
1837
1938 afterEach ( ( ) => {
@@ -22,13 +41,17 @@ describe('createComment', () => {
2241
2342 describe ( 'should' , ( ) => {
2443 it ( 'successfully create a comment with information provided' , async ( ) => {
25- const mockUserId = { userId : '123' }
44+ const mockUserId = {
45+ userId : '123' ,
46+ blockedUserIds : [ '111' ]
47+ }
2648 const mockOnUser = { id : '123' }
2749 const mockCreator = {
28- id : '123 ' ,
50+ id : '1234 ' ,
2951 name : 'Mock Creator' ,
3052 username : 'mock.creator.username' ,
31- avatarUrl : 'mock.creator.avatarurl'
53+ avatarUrl : 'mock.creator.avatarurl' ,
54+ isBannedFromPosting : false
3255 }
3356 const mockContent = {
3457 content : {
@@ -48,9 +71,307 @@ describe('createComment', () => {
4871 userId : '123'
4972 } ;
5073 const mockAuth = { uid : '321' } as AuthedUser ;
74+ const mockReq = { } as any ;
75+ const mockReplyToCommentId = { } as any ;
76+ const mockComment = { id : 12 } ;
77+ const mockNotificationDestination = { } as any ;
78+ const mockProps = {
79+ userId : mockUserId . userId ,
80+ content : mockContent . content ,
81+ replyToCommentId : mockReplyToCommentId
82+ } ;
83+
84+ ( sharedUtils . getUser as jest . Mock )
85+ . mockResolvedValueOnce ( mockCreator )
86+ . mockResolvedValueOnce ( mockOnUser ) ;
87+ ( sharedUtils . getPrivateUser as jest . Mock )
88+ . mockResolvedValueOnce ( mockUserId )
89+ . mockResolvedValueOnce ( mockOnUser ) ;
90+ ( mockPg . one as jest . Mock ) . mockResolvedValue ( mockComment ) ;
91+ ( notificationPrefs . getNotificationDestinationsForUser as jest . Mock )
92+ . mockReturnValue ( mockNotificationDestination ) ;
93+
94+ const results = await createComment ( mockProps , mockAuth , mockReq ) ;
95+
96+ expect ( results . status ) . toBe ( 'success' ) ;
97+ expect ( sharedUtils . getUser ) . toBeCalledTimes ( 2 ) ;
98+ expect ( sharedUtils . getUser ) . toBeCalledWith ( mockUserId . userId ) ;
99+ expect ( sharedUtils . getUser ) . toBeCalledWith ( mockAuth . uid ) ;
100+ expect ( sharedUtils . getPrivateUser ) . toBeCalledTimes ( 2 ) ;
101+ expect ( mockPg . one ) . toBeCalledTimes ( 1 ) ;
102+ expect ( mockPg . one ) . toBeCalledWith (
103+ expect . stringContaining ( 'insert into profile_comments' ) ,
104+ expect . arrayContaining ( [ mockCreator . id ] )
105+ ) ;
106+ expect ( websocketHelpers . broadcastUpdatedComment ) . toBeCalledTimes ( 1 )
107+
108+ } ) ;
109+
110+ it ( 'throw an error if there is no user matching the userId' , async ( ) => {
111+ const mockAuth = { uid : '321' } as AuthedUser ;
112+ const mockReq = { } as any ;
113+ const mockReplyToCommentId = { } as any ;
114+ const mockUserId = {
115+ userId : '123' ,
116+ blockedUserIds : [ '111' ]
117+ } ;
118+ const mockCreator = {
119+ id : '1234' ,
120+ name : 'Mock Creator' ,
121+ username : 'mock.creator.username' ,
122+ avatarUrl : 'mock.creator.avatarurl' ,
123+ isBannedFromPosting : false
124+ } ;
125+ const mockContent = {
126+ content : {
127+ type : 'doc' ,
128+ content : [
129+ {
130+ type : 'paragraph' ,
131+ content : [
132+ {
133+ type : 'text' ,
134+ text : 'This is the comment text'
135+ }
136+ ]
137+ }
138+ ]
139+ } ,
140+ userId : '123'
141+ } ;
142+ const mockProps = {
143+ userId : mockUserId . userId ,
144+ content : mockContent . content ,
145+ replyToCommentId : mockReplyToCommentId
146+ } ;
147+
148+ ( sharedUtils . getUser as jest . Mock )
149+ . mockResolvedValueOnce ( mockCreator )
150+ . mockResolvedValueOnce ( null ) ;
151+ ( sharedUtils . getPrivateUser as jest . Mock )
152+ . mockResolvedValue ( mockUserId ) ;
153+
154+ expect ( createComment ( mockProps , mockAuth , mockReq ) ) . rejects . toThrowError ( 'User not found' ) ;
155+ } ) ;
156+
157+ it ( 'throw an error if there is no account associated with the authId' , async ( ) => {
158+ const mockAuth = { uid : '321' } as AuthedUser ;
159+ const mockReq = { } as any ;
51160 const mockReplyToCommentId = { } as any ;
161+ const mockUserId = {
162+ userId : '123' ,
163+ blockedUserIds : [ '111' ]
164+ } ;
165+ const mockContent = {
166+ content : {
167+ type : 'doc' ,
168+ content : [
169+ {
170+ type : 'paragraph' ,
171+ content : [
172+ {
173+ type : 'text' ,
174+ text : 'This is the comment text'
175+ }
176+ ]
177+ }
178+ ]
179+ } ,
180+ userId : '123'
181+ } ;
182+ const mockProps = {
183+ userId : mockUserId . userId ,
184+ content : mockContent . content ,
185+ replyToCommentId : mockReplyToCommentId
186+ } ;
187+
188+ ( sharedUtils . getUser as jest . Mock )
189+ . mockResolvedValueOnce ( null ) ;
190+
191+ expect ( createComment ( mockProps , mockAuth , mockReq ) ) . rejects . toThrowError ( 'Your account was not found' ) ;
192+ } ) ;
193+
194+ it ( 'throw an error if the account is banned from posting' , async ( ) => {
195+ const mockAuth = { uid : '321' } as AuthedUser ;
196+ const mockReq = { } as any ;
197+ const mockReplyToCommentId = { } as any ;
198+ const mockUserId = {
199+ userId : '123' ,
200+ blockedUserIds : [ '111' ]
201+ } ;
202+ const mockCreator = {
203+ id : '1234' ,
204+ name : 'Mock Creator' ,
205+ username : 'mock.creator.username' ,
206+ avatarUrl : 'mock.creator.avatarurl' ,
207+ isBannedFromPosting : true
208+ } ;
209+ const mockContent = {
210+ content : {
211+ type : 'doc' ,
212+ content : [
213+ {
214+ type : 'paragraph' ,
215+ content : [
216+ {
217+ type : 'text' ,
218+ text : 'This is the comment text'
219+ }
220+ ]
221+ }
222+ ]
223+ } ,
224+ userId : '123'
225+ } ;
226+ const mockProps = {
227+ userId : mockUserId . userId ,
228+ content : mockContent . content ,
229+ replyToCommentId : mockReplyToCommentId
230+ } ;
231+
232+ ( sharedUtils . getUser as jest . Mock )
233+ . mockResolvedValueOnce ( mockCreator ) ;
234+
235+ expect ( createComment ( mockProps , mockAuth , mockReq ) ) . rejects . toThrowError ( 'You are banned' ) ;
236+ } ) ;
237+
238+ it ( 'throw an error if the other user is not found' , async ( ) => {
239+ const mockAuth = { uid : '321' } as AuthedUser ;
240+ const mockReq = { } as any ;
241+ const mockReplyToCommentId = { } as any ;
242+ const mockUserId = {
243+ userId : '123' ,
244+ blockedUserIds : [ '111' ]
245+ } ;
246+ const mockCreator = {
247+ id : '1234' ,
248+ name : 'Mock Creator' ,
249+ username : 'mock.creator.username' ,
250+ avatarUrl : 'mock.creator.avatarurl' ,
251+ isBannedFromPosting : false
252+ } ;
253+ const mockContent = {
254+ content : {
255+ type : 'doc' ,
256+ content : [
257+ {
258+ type : 'paragraph' ,
259+ content : [
260+ {
261+ type : 'text' ,
262+ text : 'This is the comment text'
263+ }
264+ ]
265+ }
266+ ]
267+ } ,
268+ userId : '123'
269+ } ;
270+ const mockProps = {
271+ userId : mockUserId . userId ,
272+ content : mockContent . content ,
273+ replyToCommentId : mockReplyToCommentId
274+ } ;
275+
276+ ( sharedUtils . getUser as jest . Mock )
277+ . mockResolvedValueOnce ( mockCreator ) ;
278+ ( sharedUtils . getPrivateUser as jest . Mock )
279+ . mockResolvedValue ( null ) ;
280+
281+ expect ( createComment ( mockProps , mockAuth , mockReq ) ) . rejects . toThrowError ( 'Other user not found' ) ;
282+ } ) ;
283+
284+ it ( 'throw an error if the user has blocked you' , async ( ) => {
285+ const mockAuth = { uid : '321' } as AuthedUser ;
286+ const mockReq = { } as any ;
287+ const mockReplyToCommentId = { } as any ;
288+ const mockUserId = {
289+ userId : '123' ,
290+ blockedUserIds : [ '321' ]
291+ } ;
292+ const mockCreator = {
293+ id : '1234' ,
294+ name : 'Mock Creator' ,
295+ username : 'mock.creator.username' ,
296+ avatarUrl : 'mock.creator.avatarurl' ,
297+ isBannedFromPosting : false
298+ } ;
299+ const mockContent = {
300+ content : {
301+ type : 'doc' ,
302+ content : [
303+ {
304+ type : 'paragraph' ,
305+ content : [
306+ {
307+ type : 'text' ,
308+ text : 'This is the comment text'
309+ }
310+ ]
311+ }
312+ ]
313+ } ,
314+ userId : '123'
315+ } ;
316+ const mockProps = {
317+ userId : mockUserId . userId ,
318+ content : mockContent . content ,
319+ replyToCommentId : mockReplyToCommentId
320+ } ;
321+
322+ ( sharedUtils . getUser as jest . Mock )
323+ . mockResolvedValueOnce ( mockCreator ) ;
324+ ( sharedUtils . getPrivateUser as jest . Mock )
325+ . mockResolvedValue ( mockUserId ) ;
326+
327+ expect ( createComment ( mockProps , mockAuth , mockReq ) ) . rejects . toThrowError ( 'User has blocked you' ) ;
328+ } ) ;
329+
330+ it ( 'throw an error if the comment is too long' , async ( ) => {
331+ const mockAuth = { uid : '321' } as AuthedUser ;
332+ const mockReq = { } as any ;
333+ const mockReplyToCommentId = { } as any ;
334+ const mockUserId = {
335+ userId : '123' ,
336+ blockedUserIds : [ '111' ]
337+ } ;
338+ const mockCreator = {
339+ id : '1234' ,
340+ name : 'Mock Creator' ,
341+ username : 'mock.creator.username' ,
342+ avatarUrl : 'mock.creator.avatarurl' ,
343+ isBannedFromPosting : false
344+ } ;
345+ const mockContent = {
346+ content : {
347+ type : 'doc' ,
348+ content : [
349+ {
350+ type : 'paragraph' ,
351+ content : [
352+ {
353+ type : 'text' ,
354+ text : 'This ' . repeat ( 30000 ) ,
355+ }
356+ ]
357+ }
358+ ]
359+ } ,
360+ userId : '123'
361+ } ;
362+ const mockProps = {
363+ userId : mockUserId . userId ,
364+ content : mockContent . content ,
365+ replyToCommentId : mockReplyToCommentId
366+ } ;
52367
368+ ( sharedUtils . getUser as jest . Mock )
369+ . mockResolvedValueOnce ( mockCreator ) ;
370+ ( sharedUtils . getPrivateUser as jest . Mock )
371+ . mockResolvedValue ( mockUserId ) ;
372+ console . log ( JSON . stringify ( mockContent . content ) . length ) ;
53373
374+ expect ( createComment ( mockProps , mockAuth , mockReq ) ) . rejects . toThrowError ( 'Comment is too long' ) ;
54375 } ) ;
55376 } ) ;
56377} ) ;
0 commit comments