diff --git a/__tests__/html-gmail-gmail.test.ts b/__tests__/html-gmail-gmail.test.ts index 6b2a782bc4c8fa810613a0a80d39d6d8a40c77bd..bb4fa7a3e58a07ee640965e9f0c6a3799d6f4690 100644 --- a/__tests__/html-gmail-gmail.test.ts +++ b/__tests__/html-gmail-gmail.test.ts @@ -13,5 +13,5 @@ describe("[HTML] GMail-GMail", () => { EMAIL_VENDORS.GMAIL ); - describe("One", describeFunction("one", ["sssas"])); + describe("Chrome-Chrome", describeFunction("chrome-chrome", [""])); }); diff --git a/__tests__/htmltext-gmail-gmail.test.ts b/__tests__/htmltext-gmail-gmail.test.ts index 0e00189ad7a30b9dcfde615d80b7bd94101cc5fc..a23c2de1c1eed2c585f0b6dff6c47df53c75cb8a 100644 --- a/__tests__/htmltext-gmail-gmail.test.ts +++ b/__tests__/htmltext-gmail-gmail.test.ts @@ -13,5 +13,5 @@ describe("[Pseudo PLAIN] GMail-GMail", () => { EMAIL_VENDORS.GMAIL ); - describe("One", describeFunction("one")); + describe("Gmail-Gmail", describeFunction("chrome-chrome")); }); diff --git a/__tests__/plain-gmail-gmail.test.ts b/__tests__/plain-gmail-gmail.test.ts index 58245170427225e1e43f9f01ba7b0af6a31117b3..33b9954f25323ea55bff2114a8b2b44c9c48b309 100644 --- a/__tests__/plain-gmail-gmail.test.ts +++ b/__tests__/plain-gmail-gmail.test.ts @@ -8,5 +8,5 @@ const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Plain] GMail-GMail", () => { const describeFunction = createDescribePlainTestCases(testsPath); - describe("One", describeFunction("one", ["04", "17", "18", "20"])); + describe("Chrome-Chrome", describeFunction("chrome-chrome", [])); }); diff --git a/__tests__/utils.ts b/__tests__/utils.ts index 9d0a0aa2734530053f9ec795afe42b0bf5f0bce9..e9b26b43cbd6c49bcc3bb0c0fd3e6c1bd687823f 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -1,10 +1,9 @@ import { JSDOM } from "jsdom"; const fs = require("fs"); +import MIMEParser from "@vereign/mime-parser"; -const SENT_HTML_NAME = "s_htmlContent.html"; -const RECEIVED_HTML_NAME = "r_htmlContent.html"; -const SENT_PLAIN_NAME = "s_plainContent.data"; -const RECEIVED_PLAIN_NAME = "r_plainContent.data"; +const SENT_EML_NAME = "sent.eml"; +const RECEIVED_EML_NAME = "received.eml"; import { PlainNormalizer, HTMLNormalizer } from "../src"; import { expect, test } from "@jest/globals"; import { DOM } from "@vereign/dom"; @@ -14,7 +13,7 @@ expect.extend({ toContainWithDiff(target, source) { let pass = true; try { - expect(target).toContain(source); + expect(target).toEqual(source); } catch (e) { pass = false; } @@ -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 = ( testCasePath: string ): { sentPlain: string; receivedPlain: string; } => { - const sentPlain = fs - .readFileSync(`${testCasePath}/${SENT_PLAIN_NAME}`) - .toString(); - const receivedPlain = fs - .readFileSync(`${testCasePath}/${RECEIVED_PLAIN_NAME}`) - .toString(); + const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); + const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); - const sentNormalizedPlain = PlainNormalizer.normalizePlain(sentPlain); - const receivedNormalizedPlain = PlainNormalizer.normalizePlain(receivedPlain); + const sentNormalizedPlain = PlainNormalizer.normalizePlain( + sentMime.getPlain() + ); + const receivedNormalizedPlain = PlainNormalizer.normalizePlain( + receivedMime.getPlain() + ); return { sentPlain: sentNormalizedPlain, @@ -61,12 +70,11 @@ export const getNormalizedHtml = ( sentHtml: string; receivedHtml: string; } => { - const sentHtml = fs - .readFileSync(`${testCasePath}/${SENT_HTML_NAME}`) - .toString(); - const receivedHtml = fs - .readFileSync(`${testCasePath}/${RECEIVED_HTML_NAME}`) - .toString(); + const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); + const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); + + const sentHtml = sentMime.getHTML(); + const receivedHtml = receivedMime.getHTML(); const sentDOM = new DOM(sentHtml); const receivedDOM = new JSDOM(receivedHtml); @@ -125,6 +133,7 @@ export const createDescribeHtmlTestCases = ( // eslint-disable-next-line // @ts-ignore + // console.log(receivedHtml); expect(receivedHtml).toContainWithDiff(sentHtml); }); }; @@ -152,7 +161,6 @@ export const createDescribePlainTestCases = (testsPath: string) => ( const normalizedPlain = getNormalizedPlain(testCasePath); const { sentPlain, receivedPlain } = normalizedPlain; - // expect(sentPlain.length).toBeGreaterThan(0); // expect(receivedPlain.length).toBeGreaterThan(0); @@ -211,12 +219,6 @@ export const createDescribePseudoPlainTestCases = ( expect(normalizedReceivedPseudoPlainText).toEqual( normalizedSentPseudoPlainText ); - - // const diff = diffStringsUnified( - // normalizedReceivedPseudoPlainText, - // normalizedSentPseudoPlainText - // ); - // console.log(diff); }); }; @@ -226,15 +228,11 @@ export const getDOMDocuments = ( sentHtmlDocument: HTMLDocument; receivedHtmlDocument: HTMLDocument; } => { - const sentHtml = fs - .readFileSync(`${testCasePath}/${SENT_HTML_NAME}`) - .toString(); - const receivedHtml = fs - .readFileSync(`${testCasePath}/${RECEIVED_HTML_NAME}`) - .toString(); + const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); + const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); - const sentDOM = new DOM(sentHtml); - const receivedDOM = new JSDOM(receivedHtml); + const sentDOM = new DOM(sentMime.getHTML()); + const receivedDOM = new JSDOM(receivedMime.getHTML()); return { sentHtmlDocument: sentDOM.window.document, diff --git a/package.json b/package.json index 2c3fcbb07c88d8055ee0b901058fbee1c13168f4..96071af5eeffd76ec20409e2f53ca24c6b14d619 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@typescript-eslint/eslint-plugin": "^3.10.1", "@typescript-eslint/parser": "^3.10.1", "@vereign/dom": "git+ssh://git@code.vereign.com:code/js-toolbox/gsdom.git", + "@vereign/mime-parser": "git+ssh://git@code.vereign.com:code/js-toolbox/mime-parser.git", "babel-eslint": "^10.1.0", "babel-jest": "^26.3.0", "eslint": "^7.7.0", diff --git a/yarn.lock b/yarn.lock index 3f414693a59887184a0d14dd0c581baa181b8d11..0d847285d71f117a15c07fef537f61df8ae304da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1262,6 +1262,10 @@ "@types/parse5" "^5.0.3" 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: version "2.0.5" resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a"