{"size": 4954, "ext": "js", "max_stars_repo_path": "tests/unit/services/EmailService.test.js", "max_stars_repo_name": "WKolaj/SidiroAR_BACKEND", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "tests/unit/services/EmailService.test.js", "max_issues_repo_name": "WKolaj/SidiroAR_BACKEND", "max_issues_repo_issues_event_min_datetime": "2021-05-10T23:30:08.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T04:52:31.000Z", "max_forks_repo_path": "tests/unit/services/EmailService.test.js", "max_forks_repo_name": "WKolaj/SidiroAR_BACKEND", "max_forks_repo_forks_event_min_datetime": "2021-04-07T10:00:35.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-18T14:36:06.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 2, "avg_line_length": 26.2116402116, "max_line_length": 117, "alphanum_fraction": 0.5744852644}
const EmailService = require("../../../services/EmailService/EmailService");
const nodemailer = require("nodemailer");
const config = require("config");
const { snooze } = require("../../../utilities/utilities");
const fs = require("fs");

describe("EmailService", () => {
  describe("sendMail", () => {
    let recipient, subject, htmlContent;

    beforeEach(() => {
      recipient = "1234@abcd.com";
      subject = "test email";
      htmlContent = "<p>test content</p>";
    });

    let exec = async () => {
      return await EmailService.sendMail(recipient, subject, htmlContent);
    };

    it("should send email to given recipient with given subject and given htmlContent", async () => {
      await exec();
      expect(nodemailer.mockSendMailFunction).toHaveBeenCalledTimes(1);

      let expectedTranportSettings = {
        pool: true,
        host: "pricelist.nazwa.pl",
        port: 465,
        secure: true,
        auth: {
          user: config.get("emailLogin"),
          pass: config.get("emailPassword"),
        },
      };
      expect(nodemailer.mockSendMailFunction.mock.calls[0][0]).toEqual(
        expectedTranportSettings
      );

      let expectedMailOptions = {
        from: config.get("emailLogin"),
        to: recipient,
        subject: subject,
        html: htmlContent,
      };
      expect(nodemailer.mockSendMailFunction.mock.calls[0][1]).toEqual(
        expectedMailOptions
      );
    });

    it("should resolve even if there is an error while sending mail", async () => {
      nodemailer.setMockThrowErrorWhileSending(true);

      await expect(
        new Promise(async (resolve, reject) => {
          try {
            await exec();
            return resolve(true);
          } catch (err) {
            return reject(err);
          }
        })
      ).resolves.toBeDefined();
    });
  });

  describe("generateEmailContent", () => {
    let enMailContent;
    let plMailContent;
    let login;
    let password;
    let language;

    beforeEach(() => {
      login = "testLogin";
      language = "pl";

      enMailContent = fs.readFileSync(
        "./services/EmailService/emailTemplate/en.html",
        "UTF-8"
      );

      plMailContent = fs.readFileSync(
        "./services/EmailService/emailTemplate/pl.html",
        "UTF-8"
      );
    });

    let exec = async () => {
      return EmailService.generateEmailContent(login, password, language);
    };

    it("should return valid html content, personlized for given user and language", async () => {
      let content = await exec();

      let expectedContent = plMailContent
        .replace("@PAR_LOGIN", login)
        .replace("@PAR_PIN", password);

      expect(content).toEqual(expectedContent);
    });

    it("should return valid html content, personlized for given user and language - if language is en", async () => {
      language = "en";

      let content = await exec();

      let expectedContent = enMailContent
        .replace("@PAR_LOGIN", login)
        .replace("@PAR_PIN", password);

      expect(content).toEqual(expectedContent);
    });

    it("should throw if there is no language supported", async () => {
      language = "fakeLang";

      let rejectError;

      await expect(
        new Promise(async (resolve, reject) => {
          try {
            await exec();
            return resolve(true);
          } catch (err) {
            rejectError = err;
            return reject(err);
          }
        })
      ).rejects.toBeDefined();

      expect(rejectError).toBeDefined();
      expect(rejectError.message).toEqual(
        "Unsupported language fakeLang - email content not found"
      );
    });
  });

  describe("generateEmailSubject", () => {
    let login;
    let password;
    let language;

    beforeEach(() => {
      language = "pl";
    });

    let exec = async () => {
      return EmailService.generateEmailSubject(language);
    };

    it("should return valid subject, personlized for given user and language", async () => {
      let subject = await exec();

      expect(subject).toEqual("SidiroAR - rejestracja");
    });

    it("should return valid subject, personlized for given user and language - if lang is en", async () => {
      language = "en";

      let subject = await exec();

      expect(subject).toEqual("SidiroAR - registration");
    });

    it("should throw if there is no language supported", async () => {
      language = "fakeLang";

      let rejectError;

      await expect(
        new Promise(async (resolve, reject) => {
          try {
            await exec();
            return resolve(true);
          } catch (err) {
            rejectError = err;
            return reject(err);
          }
        })
      ).rejects.toBeDefined();

      expect(rejectError).toBeDefined();
      expect(rejectError.message).toEqual(
        "Unsupported language fakeLang - email subject not found"
      );
    });
  });
});
