From ca69ec90a723f79d7938464cc94af9759572b3f7 Mon Sep 17 00:00:00 2001 From: Ffloriel Date: Mon, 29 Nov 2021 12:03:12 +0000 Subject: [PATCH] test: add tests for detailed extractor --- jest.config.js | 1 + packages/purgecss/__tests__/index.test.ts | 30 +++++++++++++++++-- packages/purgecss/__tests__/rejected.test.ts | 1 + .../test_examples/chaining-rules/index.css | 4 +++ packages/purgecss/src/VariablesStructure.ts | 8 +++-- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/jest.config.js b/jest.config.js index 6156b82e..39ee4c09 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,6 +6,7 @@ module.exports = { moduleFileExtensions: ["ts", "tsx", "js", "json"], moduleNameMapper: { "^purgecss$": "/packages/purgecss/src", + "^@fullhuman/purgecss-from-html$": "/packages/purgecss-from-html/src", }, rootDir: __dirname, testMatch: ["/packages/**/__tests__/**/*test.ts"], diff --git a/packages/purgecss/__tests__/index.test.ts b/packages/purgecss/__tests__/index.test.ts index f3056376..8ef9a472 100644 --- a/packages/purgecss/__tests__/index.test.ts +++ b/packages/purgecss/__tests__/index.test.ts @@ -1,7 +1,7 @@ -import PurgeCSS from "./../src/index"; +import purgecssFromHtml from "@fullhuman/purgecss-from-html"; import { ExtractorResult } from "../src/types"; - -import { ROOT_TEST_EXAMPLES } from "./utils"; +import PurgeCSS from "./../src/index"; +import { notFindInCSS, ROOT_TEST_EXAMPLES } from "./utils"; describe("purgecss with config file", () => { it("initialize without error with a config file specified", () => { @@ -71,3 +71,27 @@ describe("special characters, with custom Extractor", () => { expect(purgedCSS.includes("\\32 -panel")).toBe(false); }); }); + +describe("PurgeCSS with detailed extractor for html", () => { + let purgedCSS: string; + beforeAll(async () => { + const resultsPurge = await new PurgeCSS().purge({ + content: [`${ROOT_TEST_EXAMPLES}chaining-rules/index.html`], + css: [`${ROOT_TEST_EXAMPLES}chaining-rules/index.css`], + extractors: [ + { + extensions: ["html"], + extractor: purgecssFromHtml, + }, + ], + }); + purgedCSS = resultsPurge[0].css; + }); + + it("keeps parent1 selector", () => { + expect(purgedCSS.includes("parent1")).toBe(true); + }); + it("removes parent3, d33ef1, .parent2", () => { + notFindInCSS(expect, ["parent3", "d33ef1", "parent2"], purgedCSS); + }); +}); diff --git a/packages/purgecss/__tests__/rejected.test.ts b/packages/purgecss/__tests__/rejected.test.ts index f6cc757b..50b8fd3f 100644 --- a/packages/purgecss/__tests__/rejected.test.ts +++ b/packages/purgecss/__tests__/rejected.test.ts @@ -53,6 +53,7 @@ describe("rejected", () => { ".parent.d33ef1", ".parent2.def", ".parent3.def1", + "[href^='#']", ]); }); }); diff --git a/packages/purgecss/__tests__/test_examples/chaining-rules/index.css b/packages/purgecss/__tests__/test_examples/chaining-rules/index.css index ecba436d..7f29826a 100644 --- a/packages/purgecss/__tests__/test_examples/chaining-rules/index.css +++ b/packages/purgecss/__tests__/test_examples/chaining-rules/index.css @@ -36,4 +36,8 @@ } .parent3.def1{ color:red; +} + +[href^='#'] { + color: green; } \ No newline at end of file diff --git a/packages/purgecss/src/VariablesStructure.ts b/packages/purgecss/src/VariablesStructure.ts index d5704dd5..662a0b5b 100644 --- a/packages/purgecss/src/VariablesStructure.ts +++ b/packages/purgecss/src/VariablesStructure.ts @@ -40,8 +40,10 @@ class VariablesStructure { const variableName = variableMatch[1]; if (this.nodes.has(variableName)) { const usedVariableNodes = this.nodes.get(variableName); - nodes?.forEach(node => { - usedVariableNodes?.forEach(usedVariableNode => node.nodes.push(usedVariableNode)) + nodes?.forEach((node) => { + usedVariableNodes?.forEach((usedVariableNode) => + node.nodes.push(usedVariableNode) + ); }); } } @@ -74,7 +76,7 @@ class VariablesStructure { for (const used of this.usedVariables) { const usedNodes = this.nodes.get(used); if (usedNodes) { - for (const usedNode of usedNodes) { + for (const usedNode of usedNodes) { const usedVariablesMatchesInDeclaration = matchAll( usedNode.value.value, /var\((.+?)[,)]/g