From ea889efc53d94e5947a951d9a686c7500caae745 Mon Sep 17 00:00:00 2001
From: igor <igor.markin@vereign.com>
Date: Thu, 3 Dec 2020 11:20:25 +0300
Subject: [PATCH] Add pseudoplain tests

---
 __tests__/pseudoplain-outlook-outlook.test.ts | 97 +++++++++++++++++++
 __tests__/utils.ts                            | 22 +++++
 package.json                                  |  1 +
 3 files changed, 120 insertions(+)
 create mode 100644 __tests__/pseudoplain-outlook-outlook.test.ts

diff --git a/__tests__/pseudoplain-outlook-outlook.test.ts b/__tests__/pseudoplain-outlook-outlook.test.ts
new file mode 100644
index 0000000..7f512ba
--- /dev/null
+++ b/__tests__/pseudoplain-outlook-outlook.test.ts
@@ -0,0 +1,97 @@
+import { diffStringsUnified } from "jest-diff";
+import { describe, test } from "@jest/globals";
+import { getDOMDocuments, getTestCasesDirs } from "./utils";
+const path = require("path");
+
+const TESTS_GLOBAL_PATH = "/files/outlook-outlook";
+const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
+
+const createDescribePseudoPlainTestCases = (testsPath: string) =>
+  /**
+   * @param casesGroupName - name of the folder with cases
+   * @param failingCases - a list of cases that are failing and ignored. Pending to be fixed
+   * @param casesToCheckOnly - a filter to use if you want to check specific cases
+   */
+  (
+    casesGroupName: string,
+    failingCases?: Array<string>,
+    casesToCheckOnly?: Array<string>
+  ) => (): void => {
+    const testsCasesPath = testsPath + "/" + casesGroupName;
+    let testCasesDirs = getTestCasesDirs(testsCasesPath);
+
+    if (casesToCheckOnly && casesToCheckOnly.length) {
+      testCasesDirs = testCasesDirs.filter((dir) =>
+        casesToCheckOnly.includes(dir)
+      );
+    }
+
+    if (failingCases && failingCases.length) {
+      testCasesDirs = testCasesDirs.filter(
+        (dir) => !failingCases.includes(dir)
+      );
+    }
+
+    test.each(testCasesDirs)("Case %s", (dirName: string) => {
+      const testCasePath = testsCasesPath + "/" + dirName;
+      const { sentHtmlDocument, receivedHtmlDocument } = getDOMDocuments(
+        testCasePath
+      );
+
+      const difference = diffStringsUnified(
+        receivedHtmlDocument.body.textContent,
+        sentHtmlDocument.body.textContent
+      );
+
+      console.log(difference);
+    });
+  };
+
+describe("[Pseudo PLAIN] Outlook-Outlook normalization", () => {
+  const describeFunction = createDescribePseudoPlainTestCases(testsPath);
+
+  // ["01"] - is a filter. Pass here names of directories with test cases you want to check
+  describe("Emails Chrome", describeFunction("chrome", null, ["01"]));
+
+  // describe(
+  //   "Emails Edge",
+  //   describeFunction("edge", [
+  //     "21", // This case has a src mismatch for the same image. Reproduce this case again
+  //     "08", // Files are missing for test case
+  //     "10", // Files are missing for test case
+  //   ])
+  // );
+  // describe(
+  //   "Emails Safari",
+  //   describeFunction("safari", [
+  //     "04", // This case contains <section> tag which is ignored by Outlook, and it also inserts a plenty of empty divs,
+  //     "08",
+  //   ])
+  // );
+
+  // Does not work at all
+  // describe(
+  //   "Emails MacOS",
+  //   describeFunction("macos", ["20", "21", "", "23", "24", "25", "26"])
+  // );
+
+  // describe(
+  //   "Emails Windows",
+  //   describeFunction("windows", [
+  //     "06",
+  //     "20",
+  //     "20forward",
+  //     "20reply",
+  //     "21",
+  //     "21forward",
+  //     "21reply",
+  //     "22",
+  //     "23",
+  //     "24",
+  //     "25",
+  //     "26",
+  //     "28",
+  //     "10", // missing files
+  //   ])
+  // );
+});
diff --git a/__tests__/utils.ts b/__tests__/utils.ts
index 7a0e7b9..33f9d3d 100644
--- a/__tests__/utils.ts
+++ b/__tests__/utils.ts
@@ -129,3 +129,25 @@ export const createDescribePlainTestCases = (testsPath: string) => (
     expect(receivedPlain).toContain(sentPlain);
   });
 };
+
+export const getDOMDocuments = (
+  testCasePath: string
+): {
+  sentHtmlDocument: HTMLDocument;
+  receivedHtmlDocument: HTMLDocument;
+} => {
+  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);
+
+  return {
+    sentHtmlDocument: sentDOM.window.document,
+    receivedHtmlDocument: receivedDOM.window.document,
+  };
+};
diff --git a/package.json b/package.json
index 5805b72..6831c4d 100644
--- a/package.json
+++ b/package.json
@@ -18,6 +18,7 @@
     "eslint": "^7.7.0",
     "husky": "^4.2.5",
     "jest": "^26.4.2",
+    "jest-diff": "^26.6.2",
     "lint-staged": "^10.2.13",
     "prettier": "^2.1.1",
     "typescript": "^4.0.2"
-- 
GitLab