Files
Compass/backend/api/tests/unit/create-private-user-message.unit.test.ts
MartinBraquet f4d8822dbe Fix CI
2026-03-07 00:17:38 +01:00

101 lines
3.1 KiB
TypeScript

jest.mock('shared/utils')
jest.mock('shared/supabase/init')
jest.mock('api/helpers/private-messages')
import {MAX_COMMENT_JSON_LENGTH} from 'api/create-comment'
import {createPrivateUserMessage} from 'api/create-private-user-message'
import {AuthedUser} from 'api/helpers/endpoint'
import * as helpersPrivateMessagesModules from 'api/helpers/private-messages'
import * as supabaseInit from 'shared/supabase/init'
import * as sharedUtils from 'shared/utils'
describe('createPrivateUserMessage', () => {
beforeEach(() => {
jest.resetAllMocks()
const mockPg = {} as any
;(supabaseInit.createSupabaseDirectClient as jest.Mock).mockReturnValue(mockPg)
})
afterEach(() => {
jest.restoreAllMocks()
})
describe('when given valid input', () => {
it('successfully create a private user message', async () => {
const mockBody = {
content: {'': 'x'.repeat(MAX_COMMENT_JSON_LENGTH - 8)},
channelId: 123,
}
const mockAuth = {uid: '321'} as AuthedUser
const mockReq = {} as any
const mockCreator = {
is_banned_from_posting: false,
}
;(sharedUtils.getUser as jest.Mock).mockResolvedValue(mockCreator)
await createPrivateUserMessage(mockBody, mockAuth, mockReq)
expect(sharedUtils.getUser).toBeCalledTimes(1)
expect(sharedUtils.getUser).toBeCalledWith(mockAuth.uid)
expect(helpersPrivateMessagesModules.createPrivateUserMessageMain).toBeCalledTimes(1)
expect(helpersPrivateMessagesModules.createPrivateUserMessageMain).toBeCalledWith(
mockCreator,
mockBody.channelId,
mockBody.content,
expect.any(Object),
'private',
)
})
})
describe('when an error occurs', () => {
it('should throw if the content is too long', async () => {
const mockBody = {
content: {'': 'x'.repeat(MAX_COMMENT_JSON_LENGTH)},
channelId: 123,
}
const mockAuth = {uid: '321'} as AuthedUser
const mockReq = {} as any
expect(createPrivateUserMessage(mockBody, mockAuth, mockReq)).rejects.toThrowError(
`Message JSON should be less than ${MAX_COMMENT_JSON_LENGTH}`,
)
})
it('should throw if the user does not exist', async () => {
const mockBody = {
content: {mockJson: 'mockJsonContent'},
channelId: 123,
}
const mockAuth = {uid: '321'} as AuthedUser
const mockReq = {} as any
;(sharedUtils.getUser as jest.Mock).mockResolvedValue(false)
expect(createPrivateUserMessage(mockBody, mockAuth, mockReq)).rejects.toThrowError(
`Your account was not found`,
)
})
it('should throw if the user does not exist', async () => {
const mockBody = {
content: {mockJson: 'mockJsonContent'},
channelId: 123,
}
const mockAuth = {uid: '321'} as AuthedUser
const mockReq = {} as any
const mockCreator = {
isBannedFromPosting: true,
}
;(sharedUtils.getUser as jest.Mock).mockResolvedValue(mockCreator)
expect(createPrivateUserMessage(mockBody, mockAuth, mockReq)).rejects.toThrowError(
`You are banned`,
)
})
})
})