improve saveMessage (switch to class) and started to test

This commit is contained in:
grimhilt
2023-03-29 21:00:43 +02:00
parent 053213eecb
commit 1a63b3154a
9 changed files with 366 additions and 149 deletions

View File

@@ -1,52 +1,40 @@
process.env["NODE_DEV"] = "TEST";
const { saveFromParsedData } = require("../../mails/storeMessage");
const { TestDb } = require("../sql/test-utilsDb");
const MYSQL = require("../../db/config.json").MYSQL;
const { getAddresseId } = require("../../db/mail");
const { generateAttrs, generateUsers } = require("../test-utils/test-attrsUtils");
const { registerMessageInApp, roomType } = require("../../mails/saveMessage");
let db;
beforeAll(async () => {
const options = {
databaseOptions: {
host: "localhost",
port: 3306,
user: MYSQL.user,
password: MYSQL.pwd,
database: "mail_test",
},
dbSchema: "../../sql/structureV2.sql",
};
db = new TestDb(options);
await db.init();
});
jest.mock("../../db/mail");
afterAll(async () => {
db.cleanTables();
});
// todo mock db
describe("saveMessage", async () => {
describe("rooms", async () => {
it("messages not related and from same sender should be in the same room", async () => {
await saveFromParsedData(
{
to: { value: [{ address: "address1@email.com" }] },
from: { value: [{ address: "address2@email.com" }] },
messageId: "<messageId1>",
},
1,
);
await saveFromParsedData(
{
to: { value: [{ address: "address1@email.com" }] },
from: { value: [{ address: "address2@email.com" }] },
messageId: "<messageId2>",
},
2,
);
// todo call parser
const query = ""
db.execQueryAsync().then((res) => {
expect(res.length).toBe(2);
})
describe("saveMessage", () => {
describe("is not a reply", () => {
it("first DM from user should create a DM room", async () => {
const users = generateUsers(2);
const attrs = generateAttrs({ from: [users[0].user], to: [users[1].user] });
getAddresseId.mockReturnValue(users[1].id);
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
.spyOn(register, "createOrRegisterOnExistence")
.mockImplementation(() => undefined);
await register.save();
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM);
});
// it("DM message from user should be added to DM room", async () => {
// });
// it("first GROUP message should create a group", () => {});
});
describe("replies", () => {
it("", () => {});
});
describe("", () => {});
});

View File

@@ -0,0 +1,206 @@
const names = [
"James",
"Mary",
"Robert",
"Patricia",
"John",
"Jennifer",
"Michael",
"Linda",
"David",
"Elizabeth",
"William",
"Barbara",
"Richard",
"Susan",
"Joseph",
"Jessica",
"Thomas",
"Sarah",
"Charles",
"Karen",
"Christopher",
"Lisa",
"Daniel",
"Nancy",
"Matthew",
"Betty",
"Anthony",
"Margaret",
"Mark",
"Sandra",
"Donald",
"Ashley",
"Steven",
"Kimberly",
"Paul",
"Emily",
"Andrew",
"Donna",
"Joshua",
"Michelle",
"Kenneth",
"Carol",
"Kevin",
"Amanda",
"Brian",
"Dorothy",
"George",
"Melissa",
"Timothy",
"Deborah",
"Ronald",
"Stephanie",
"Edward",
"Rebecca",
"Jason",
"Sharon",
"Jeffrey",
"Laura",
"Ryan",
"Cynthia",
"Jacob",
"Kathleen",
"Gary",
"Amy",
"Nicholas",
"Angela",
"Eric",
"Shirley",
"Jonathan",
"Anna",
"Stephen",
"Brenda",
"Larry",
"Pamela",
"Justin",
"Emma",
"Scott",
"Nicole",
"Brandon",
"Helen",
"Benjamin",
"Samantha",
"Samuel",
"Katherine",
"Gregory",
"Christine",
"Alexander",
"Debra",
"Frank",
"Rachel",
"Patrick",
"Carolyn",
"Raymond",
"Janet",
"Jack",
"Catherine",
"Dennis",
"Maria",
"Jerry",
"Heather",
"Tyler",
"Diane",
"Aaron",
"Ruth",
"Jose",
"Julie",
"Adam",
"Olivia",
"Nathan",
"Joyce",
"Henry",
"Virginia",
"Douglas",
"Victoria",
"Zachary",
"Kelly",
"Peter",
"Lauren",
"Kyle",
"Christina",
"Ethan",
"Joan",
"Walter",
"Evelyn",
"Noah",
"Judith",
"Jeremy",
"Megan",
"Christian",
"Andrea",
"Keith",
"Cheryl",
"Roger",
"Hannah",
"Terry",
"Jacqueline",
"Gerald",
"Martha",
"Harold",
"Gloria",
"Sean",
"Teresa",
"Austin",
"Ann",
"Carl",
"Sara",
"Arthur",
"Madison",
"Lawrence",
"Frances",
"Dylan",
"Kathryn",
"Jesse",
"Janice",
"Jordan",
"Jean",
"Bryan",
"Abigail",
"Billy",
"Alice",
"Joe",
"Julia",
"Bruce",
"Judy",
"Gabriel",
"Sophia",
"Logan",
"Grace",
"Albert",
"Denise",
"Willie",
"Amber",
"Alan",
"Doris",
"Juan",
"Marilyn",
"Wayne",
"Danielle",
"Elijah",
"Beverly",
"Randy",
"Isabella",
"Roy",
"Theresa",
"Vincent",
"Diana",
"Ralph",
"Natalie",
"Eugene",
"Brittany",
"Russell",
"Charlotte",
"Bobby",
"Marie",
"Mason",
"Kayla",
"Philip",
"Alexis",
"Louis",
"Lori"
];
module.exports = {
names
}