mail/back/test/mail/saveMessage-test.js
2023-04-01 16:32:29 +02:00

91 lines
3.5 KiB
JavaScript

const { getAddresseId, getUserIdOfMailbox } from ";
const { generateAttrs, generateUsers } = require("../test-utils/test-attrsUtils");
const { registerMessageInApp, roomType } from ";
jest.mock("../../db/mail");
// todo mock db
// new message from us
// to multiple people -> room
// if response has same member => group
// if response is dm => channel
// to one person => dm
// new message from other
// to only me -> room
// if no reply to multiple message => channel
// else => dm
// to multiple people -> room
// // make it better
// if multiple members reply -> group
// if only me reply -> channel
describe("saveMessage", () => {
describe("new first message", () => {
const users = generateUsers(5);
const ownUser = users[0];
const messageId = 1;
const boxId = 1;
getUserIdOfMailbox.mockReturnValue([{ user_id: ownUser.id }]);
getAddresseId.mockImplementation((email) => {
const match = users.find((user) => user.user.mailbox + "@" + user.user.host == email);
return match.id;
});
describe("from us", () => {
it("new first message from us to one recipient should create a DM", async () => {
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const createOrRegisterOnExistence = jest
.spyOn(register, "createOrRegisterOnExistence")
.mockImplementation(() => undefined);
await register.save();
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM);
});
it("new first message from us to multiple recipients should create a room", async () => {
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user, users[2].user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const initiateRoom = jest
.spyOn(register, "initiateRoom")
.mockImplementation(() => undefined);
await register.save();
expect(initiateRoom).toHaveBeenCalledWith(ownUser.id, roomType.ROOM);
});
it("response to new first message to multiple recipients with same members should change room type to GROUP", () => {
});
it("response to new first message to multiple recipients with different members should change room type to CHANNEL", () => {
});
});
describe("from other", () => {
it("new first message from other to me only should create a room", async () => {
const attrs = generateAttrs({ from: [users[1].user], to: [ownUser.user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const createOrRegisterOnExistence = jest
.spyOn(register, "createOrRegisterOnExistence")
.mockImplementation(() => undefined);
await register.save();
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.ROOM);
});
});
});
describe("replies", () => {
it("", () => {});
});
describe("", () => {});
});