improve tests saveMessage

This commit is contained in:
grimhilt 2023-03-30 09:16:10 +02:00
parent 1a63b3154a
commit e2bd0bafea
3 changed files with 215 additions and 214 deletions

View File

@ -46,14 +46,14 @@ class registerMessageInApp {
} }
async init() { async init() {
this.ownerId = await getAddresseId(createAddress(this.envelope.sender[0])); // todo use sender or from ? this.ownerId = await getAddresseId(createAddress(this.envelope.from[0])); // todo use sender or from ?
} }
isDm = () => nbMembers(this.envelope) == 2; isDm = () => nbMembers(this.envelope) == 2;
async isFromUs() { async isFromUs() {
if (!this.userId) this.userId = (await getUserIdOfMailbox(boxId))[0]?.user_id; if (!this.userId) this.userId = (await getUserIdOfMailbox(this.boxId))[0]?.user_id;
return this.ownerId = this.userId; return this.ownerId == this.userId;
} }
async initiateRoom(owner, roomType) { async initiateRoom(owner, roomType) {

View File

@ -1,4 +1,4 @@
const { getAddresseId } = require("../../db/mail"); const { getAddresseId, getUserIdOfMailbox } = require("../../db/mail");
const { generateAttrs, generateUsers } = require("../test-utils/test-attrsUtils"); const { generateAttrs, generateUsers } = require("../test-utils/test-attrsUtils");
const { registerMessageInApp, roomType } = require("../../mails/saveMessage"); const { registerMessageInApp, roomType } = require("../../mails/saveMessage");
@ -8,17 +8,17 @@ jest.mock("../../db/mail");
describe("saveMessage", () => { describe("saveMessage", () => {
describe("is not a reply", () => { describe("is not a reply", () => {
it("first DM from user should create a DM room", async () => { it("DM from user should be assigned to other user", async () => {
const users = generateUsers(2); const users = generateUsers(2);
const attrs = generateAttrs({ from: [users[0].user], to: [users[1].user] }); const attrs = generateAttrs({ from: [users[0].user], to: [users[1].user] });
getAddresseId.mockReturnValue(users[1].id); getUserIdOfMailbox.mockReturnValue([{ user_id: users[0].id }]);
getAddresseId.mockImplementation((email) => {
const match = users.find((user) => user.user.mailbox + "@" + user.user.host == email);
return match.id;
});
const register = new registerMessageInApp(1, attrs, 1); const register = new registerMessageInApp(1, attrs, 1);
register.ownerId = users[0].id;
jest.spyOn(register, "isFromUs").mockReturnValue(true);
jest.spyOn(register, "init").mockReturnValue("");
const createOrRegisterOnExistence = jest const createOrRegisterOnExistence = jest
.spyOn(register, "createOrRegisterOnExistence") .spyOn(register, "createOrRegisterOnExistence")
@ -28,9 +28,10 @@ describe("saveMessage", () => {
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM); expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM);
}); });
// it("DM message from user should be added to DM room", async () => {
// }); it("DM message from user should be added to DM room", async () => {
// todo multiple messages
});
// it("first GROUP message should create a group", () => {}); // it("first GROUP message should create a group", () => {});
}); });
describe("replies", () => { describe("replies", () => {

View File

@ -198,9 +198,9 @@ const names = [
"Philip", "Philip",
"Alexis", "Alexis",
"Louis", "Louis",
"Lori" "Lori",
]; ];
module.exports = { module.exports = {
names names,
} };