Files
Compass/backend/api/tests/unit/delete-message.unit.test.ts
Okechi Jones-Williams 7b61c70f6d Add more API endoints unit tests (#25)
* 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

* Updated Create profile unit test

* Updating create user unit test

* Add create-user unit tests

* .

* Added more unit tests

* Added more unit tests

* .

* Apply suggestion from @MartinBraquet

* .

* Added unit tests

* Added unit tests

---------

Co-authored-by: MartinBraquet <martin.braquet@gmail.com>
2026-01-08 12:52:49 +02:00

99 lines
3.6 KiB
TypeScript

jest.mock('shared/supabase/init');
jest.mock('api/helpers/private-messages');
import { deleteMessage } from "api/delete-message";
import * as supabaseInit from "shared/supabase/init";
import * as messageHelpers from "api/helpers/private-messages";
import { AuthedUser } from "api/helpers/endpoint";
describe('deleteMessage', () => {
let mockPg = {} as any;
beforeEach(() => {
jest.resetAllMocks();
mockPg = {
oneOrNone: jest.fn(),
none: jest.fn()
};
(supabaseInit.createSupabaseDirectClient as jest.Mock)
.mockReturnValue(mockPg);
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('when given valid input', () => {
it('should delete a message', async () => {
const mockMessageId = {
messageId: 123
};
const mockMessage = {
channel_id: "mockChannelId"
};
const mockAuth = { uid: '321' } as AuthedUser;
const mockReq = {} as any;
(mockPg.oneOrNone as jest.Mock).mockResolvedValue(mockMessage);
(mockPg.none as jest.Mock).mockResolvedValue(null);
(messageHelpers.broadcastPrivateMessages as jest.Mock).mockResolvedValue(null);
const results = await deleteMessage(mockMessageId, mockAuth, mockReq);
expect(results.success).toBeTruthy();
expect(mockPg.oneOrNone).toBeCalledTimes(1);
expect(mockPg.oneOrNone).toBeCalledWith(
expect.stringContaining('SELECT *'),
[mockMessageId.messageId, mockAuth.uid]
);
expect(mockPg.none).toBeCalledTimes(1);
expect(mockPg.none).toBeCalledWith(
expect.stringContaining('DELETE'),
[mockMessageId.messageId, mockAuth.uid]
);
expect(messageHelpers.broadcastPrivateMessages).toBeCalledTimes(1);
expect(messageHelpers.broadcastPrivateMessages).toBeCalledWith(
expect.any(Object),
mockMessage.channel_id,
mockAuth.uid
);
});
});
describe('when an error occurs', () => {
it('should throw if the message was not found', async () => {
const mockMessageId = {
messageId: 123
};
const mockAuth = { uid: '321' } as AuthedUser;
const mockReq = {} as any;
(mockPg.oneOrNone as jest.Mock).mockResolvedValue(null);
expect(deleteMessage(mockMessageId, mockAuth, mockReq))
.rejects
.toThrow('Message not found');
});
it('should throw if the message was not broadcasted', async () => {
const mockMessageId = {
messageId: 123
};
const mockMessage = {
channel_id: "mockChannelId"
};
const mockAuth = { uid: '321' } as AuthedUser;
const mockReq = {} as any;
(mockPg.oneOrNone as jest.Mock).mockResolvedValue(mockMessage);
(mockPg.none as jest.Mock).mockResolvedValue(null);
(messageHelpers.broadcastPrivateMessages as jest.Mock).mockRejectedValue(new Error('Broadcast Error'));
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
await deleteMessage(mockMessageId, mockAuth, mockReq);
expect(errorSpy).toBeCalledTimes(1);
expect(errorSpy).toBeCalledWith(
expect.stringContaining('broadcastPrivateMessages failed'),
expect.any(Error)
);
});
});
});