Files
Compass/backend/api/tests/unit/create-private-user-message.unit.test.ts
Okechi Jones-Williams 28ce878b34 Add API unit tests for create-* endpoints (#23)
* setting up test structure

* .

* added playwright config file, deleted original playwright folder and moved "some.test" file

* continued test structure setup

* Updating test folder structure

* Added database seeding script and backend testing folder structure

* removed the database test

* Replaced db seeding script

* Updated userInformation.ts to use values from choices.tsx

* merge prep

* removing extra unit test, moving api test to correct folder

* Pushing to get help with sql Unit test

* Updating get-profiles unit tests

* Added more unit tests

* .

* Added more unit tests

* Added getSupabaseToken unit test

* .

* excluding supabase token test so ci can pass

* .

* Seperated the seedDatabase func into its own file so it can be accessed seperatly

* Fixed failing test

* .

* .

* Fix tests

* Fix lint

* Clean

* Fixed module paths in compute-score unit test

* Updated root tsconfig to recognise backend/shared

* Added create comment unit test

* Added some unit tests

* Working on createProfile return issue

* .

* Fixes

---------

Co-authored-by: MartinBraquet <martin.braquet@gmail.com>
2025-12-12 10:34:07 +02:00

100 lines
3.5 KiB
TypeScript

jest.mock('shared/utils');
jest.mock('shared/supabase/init');
jest.mock('api/helpers/private-messages');
import { createPrivateUserMessage } from "api/create-private-user-message";
import * as sharedUtils from "shared/utils";
import * as supabaseInit from "shared/supabase/init";
import * as helpersPrivateMessagesModules from "api/helpers/private-messages";
import { AuthedUser } from "api/helpers/endpoint";
import { MAX_COMMENT_JSON_LENGTH } from "api/create-comment";
describe('createPrivateUserMessage', () => {
beforeEach(() => {
jest.resetAllMocks();
const mockPg = {} as any;
(supabaseInit.createSupabaseDirectClient as jest.Mock)
.mockReturnValue(mockPg);
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('should', () => {
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 = {
isBannedFromPosting: false
};
(sharedUtils.getUser as jest.Mock).mockResolvedValue(mockCreator);
(helpersPrivateMessagesModules.createPrivateUserMessageMain as jest.Mock)
.mockResolvedValue(null);
await createPrivateUserMessage(mockBody, mockAuth, mockReq);
expect(helpersPrivateMessagesModules.createPrivateUserMessageMain).toBeCalledWith(
mockCreator,
mockBody.channelId,
mockBody.content,
expect.any(Object),
'private'
);
});
it('throw an error 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('throw an error 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(null);
expect(createPrivateUserMessage(mockBody, mockAuth, mockReq))
.rejects
.toThrowError(`Your account was not found`);
});
it('throw an error 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`);
});
});
});