Skip to content
Snippets Groups Projects
Commit b6006e21 authored by Igor Markin's avatar Igor Markin
Browse files

Implement basic gmail tests using MIME parser

parent 97b6faf7
No related branches found
No related tags found
1 merge request!9Eml test cases
...@@ -13,5 +13,5 @@ describe("[HTML] GMail-GMail", () => { ...@@ -13,5 +13,5 @@ describe("[HTML] GMail-GMail", () => {
EMAIL_VENDORS.GMAIL EMAIL_VENDORS.GMAIL
); );
describe("One", describeFunction("one", ["sssas"])); describe("Chrome-Chrome", describeFunction("chrome-chrome", [""]));
}); });
...@@ -13,5 +13,5 @@ describe("[Pseudo PLAIN] GMail-GMail", () => { ...@@ -13,5 +13,5 @@ describe("[Pseudo PLAIN] GMail-GMail", () => {
EMAIL_VENDORS.GMAIL EMAIL_VENDORS.GMAIL
); );
describe("One", describeFunction("one")); describe("Gmail-Gmail", describeFunction("chrome-chrome"));
}); });
...@@ -8,5 +8,5 @@ const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); ...@@ -8,5 +8,5 @@ const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`);
describe("[Plain] GMail-GMail", () => { describe("[Plain] GMail-GMail", () => {
const describeFunction = createDescribePlainTestCases(testsPath); const describeFunction = createDescribePlainTestCases(testsPath);
describe("One", describeFunction("one", ["04", "17", "18", "20"])); describe("Chrome-Chrome", describeFunction("chrome-chrome", []));
}); });
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
const fs = require("fs"); const fs = require("fs");
import MIMEParser from "@vereign/mime-parser";
const SENT_HTML_NAME = "s_htmlContent.html"; const SENT_EML_NAME = "sent.eml";
const RECEIVED_HTML_NAME = "r_htmlContent.html"; const RECEIVED_EML_NAME = "received.eml";
const SENT_PLAIN_NAME = "s_plainContent.data";
const RECEIVED_PLAIN_NAME = "r_plainContent.data";
import { PlainNormalizer, HTMLNormalizer } from "../src"; import { PlainNormalizer, HTMLNormalizer } from "../src";
import { expect, test } from "@jest/globals"; import { expect, test } from "@jest/globals";
import { DOM } from "@vereign/dom"; import { DOM } from "@vereign/dom";
...@@ -14,7 +13,7 @@ expect.extend({ ...@@ -14,7 +13,7 @@ expect.extend({
toContainWithDiff(target, source) { toContainWithDiff(target, source) {
let pass = true; let pass = true;
try { try {
expect(target).toContain(source); expect(target).toEqual(source);
} catch (e) { } catch (e) {
pass = false; pass = false;
} }
...@@ -26,21 +25,31 @@ expect.extend({ ...@@ -26,21 +25,31 @@ expect.extend({
}, },
}); });
const mimeCache: { [key: string]: MIMEParser } = {};
const getMime = (path: string): MIMEParser => {
if (!mimeCache[path]) {
const mimeString = fs.readFileSync(path).toString();
mimeCache[path] = new MIMEParser(mimeString);
}
return mimeCache[path];
};
export const getNormalizedPlain = ( export const getNormalizedPlain = (
testCasePath: string testCasePath: string
): { ): {
sentPlain: string; sentPlain: string;
receivedPlain: string; receivedPlain: string;
} => { } => {
const sentPlain = fs const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
.readFileSync(`${testCasePath}/${SENT_PLAIN_NAME}`) const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
.toString();
const receivedPlain = fs
.readFileSync(`${testCasePath}/${RECEIVED_PLAIN_NAME}`)
.toString();
const sentNormalizedPlain = PlainNormalizer.normalizePlain(sentPlain); const sentNormalizedPlain = PlainNormalizer.normalizePlain(
const receivedNormalizedPlain = PlainNormalizer.normalizePlain(receivedPlain); sentMime.getPlain()
);
const receivedNormalizedPlain = PlainNormalizer.normalizePlain(
receivedMime.getPlain()
);
return { return {
sentPlain: sentNormalizedPlain, sentPlain: sentNormalizedPlain,
...@@ -61,12 +70,11 @@ export const getNormalizedHtml = ( ...@@ -61,12 +70,11 @@ export const getNormalizedHtml = (
sentHtml: string; sentHtml: string;
receivedHtml: string; receivedHtml: string;
} => { } => {
const sentHtml = fs const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
.readFileSync(`${testCasePath}/${SENT_HTML_NAME}`) const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
.toString();
const receivedHtml = fs const sentHtml = sentMime.getHTML();
.readFileSync(`${testCasePath}/${RECEIVED_HTML_NAME}`) const receivedHtml = receivedMime.getHTML();
.toString();
const sentDOM = new DOM(sentHtml); const sentDOM = new DOM(sentHtml);
const receivedDOM = new JSDOM(receivedHtml); const receivedDOM = new JSDOM(receivedHtml);
...@@ -125,6 +133,7 @@ export const createDescribeHtmlTestCases = ( ...@@ -125,6 +133,7 @@ export const createDescribeHtmlTestCases = (
// eslint-disable-next-line // eslint-disable-next-line
// @ts-ignore // @ts-ignore
// console.log(receivedHtml);
expect(receivedHtml).toContainWithDiff(sentHtml); expect(receivedHtml).toContainWithDiff(sentHtml);
}); });
}; };
...@@ -152,7 +161,6 @@ export const createDescribePlainTestCases = (testsPath: string) => ( ...@@ -152,7 +161,6 @@ export const createDescribePlainTestCases = (testsPath: string) => (
const normalizedPlain = getNormalizedPlain(testCasePath); const normalizedPlain = getNormalizedPlain(testCasePath);
const { sentPlain, receivedPlain } = normalizedPlain; const { sentPlain, receivedPlain } = normalizedPlain;
// expect(sentPlain.length).toBeGreaterThan(0); // expect(sentPlain.length).toBeGreaterThan(0);
// expect(receivedPlain.length).toBeGreaterThan(0); // expect(receivedPlain.length).toBeGreaterThan(0);
...@@ -211,12 +219,6 @@ export const createDescribePseudoPlainTestCases = ( ...@@ -211,12 +219,6 @@ export const createDescribePseudoPlainTestCases = (
expect(normalizedReceivedPseudoPlainText).toEqual( expect(normalizedReceivedPseudoPlainText).toEqual(
normalizedSentPseudoPlainText normalizedSentPseudoPlainText
); );
// const diff = diffStringsUnified(
// normalizedReceivedPseudoPlainText,
// normalizedSentPseudoPlainText
// );
// console.log(diff);
}); });
}; };
...@@ -226,15 +228,11 @@ export const getDOMDocuments = ( ...@@ -226,15 +228,11 @@ export const getDOMDocuments = (
sentHtmlDocument: HTMLDocument; sentHtmlDocument: HTMLDocument;
receivedHtmlDocument: HTMLDocument; receivedHtmlDocument: HTMLDocument;
} => { } => {
const sentHtml = fs const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
.readFileSync(`${testCasePath}/${SENT_HTML_NAME}`) const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
.toString();
const receivedHtml = fs
.readFileSync(`${testCasePath}/${RECEIVED_HTML_NAME}`)
.toString();
const sentDOM = new DOM(sentHtml); const sentDOM = new DOM(sentMime.getHTML());
const receivedDOM = new JSDOM(receivedHtml); const receivedDOM = new JSDOM(receivedMime.getHTML());
return { return {
sentHtmlDocument: sentDOM.window.document, sentHtmlDocument: sentDOM.window.document,
......
...@@ -1262,6 +1262,10 @@ ...@@ -1262,6 +1262,10 @@
"@types/parse5" "^5.0.3" "@types/parse5" "^5.0.3"
parse5 "^6.0.1" parse5 "^6.0.1"
"@vereign/mime-parser@git+ssh://git@code.vereign.com:code/js-toolbox/mime-parser.git":
version "1.0.0"
resolved "git+ssh://git@code.vereign.com:code/js-toolbox/mime-parser.git#d48073f6910f7c76b12883beecfb4a23721667bc"
abab@^2.0.3: abab@^2.0.3:
version "2.0.5" version "2.0.5"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a" resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment