Skip to content
Snippets Groups Projects
outlook-outlook.test.ts 2.63 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { describe, test, expect } from "@jest/globals";
    import { JSDOM } from "jsdom";
    import HTMLNormalizer from "../src/HTMLNormalizer";
    import { EMAIL_VENDORS } from "../src/constants";
    const fs = require("fs");
    const path = require("path");
    const util = require("util");
    
    // Test cases from https://code.vereign.com/alexey.lunin/outlook-files-upload
    
    const TESTS_GLOBAL_PATH = "/outlook-files-upload/uploads";
    const SENT_HTML_NAME = "s_initialHtmlContent.data";
    const RECEIVED_HTML_NAME = "r_htmlContent.data";
    
    const testsPath = path.resolve(__dirname, `..${TESTS_GLOBAL_PATH}`);
    const getTestCasesDirs = (testCasesPath: string) => {
      return fs.readdirSync(testCasesPath).filter(function (file) {
        return fs.statSync(testCasesPath + "/" + file).isDirectory();
      });
    };
    
    const getNormalizedHtml = (
      testCasePath: string
    ): {
      sentHtml: string;
      receivedHtml: string;
    } => {
      const sentHtml = fs
        .readFileSync(`${testCasePath}/${SENT_HTML_NAME}`)
        .toString();
      const receivedHtml = fs
        .readFileSync(`${testCasePath}/${RECEIVED_HTML_NAME}`)
        .toString();
    
      const sentDOM = new JSDOM(sentHtml);
      const receivedDOM = new JSDOM(receivedHtml);
    
      const sentNormalizedHtml = HTMLNormalizer.normalizeVendorHtml(
        sentDOM.window.document,
        EMAIL_VENDORS.OUTLOOK
      );
      const receivedNormalizedHtml = HTMLNormalizer.normalizeVendorHtml(
        receivedDOM.window.document,
        EMAIL_VENDORS.OUTLOOK
      );
    
      return {
        sentHtml: sentNormalizedHtml,
        receivedHtml: receivedNormalizedHtml,
      };
    };
    
    describe("Outlook emails HTML normalization", () => {
      const describeTestCases = (casesName: string) => () => {
        const testsCasesPath = testsPath + "/" + casesName;
        const testCasesDirs = getTestCasesDirs(testsCasesPath);
    
        test.each(testCasesDirs)("Case %s", (dirName: string) => {
          const testCasePath = testsCasesPath + "/" + dirName;
          let normalizedHtmls;
          try {
            normalizedHtmls = getNormalizedHtml(testCasePath);
          } catch (e) {
            console.log("Invalid test case: " + dirName);
            return;
          }
    
          const { sentHtml, receivedHtml } = normalizedHtmls;
    
          expect(receivedHtml.length).toBeGreaterThan(0);
          expect(sentHtml.length).toBeGreaterThan(0);
          expect(receivedHtml).toContain(sentHtml);
        });
      };
    
      describe("Emails Chrome", describeTestCases("chrome"));
      describe("Emails Edge", describeTestCases("edge"));
      //describe("Emails Gmail", describeTestCases("gmail"));
    
      // it("Emails Edge", () => {
      // })
      //
      // it("Emails Gmail", () => {
      // })
      //
      // it("Emails MacOS", () => {
      // })
      //
      // it("Emails Safari", () => {
      // })
      //
      // it("Emails Windows", () => {
      // })
    });