Skip to content

Commit

Permalink
test: add tests for detailed extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ffloriel committed Nov 29, 2021
1 parent 18c5e80 commit ca69ec9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
moduleFileExtensions: ["ts", "tsx", "js", "json"],
moduleNameMapper: {
"^purgecss$": "<rootDir>/packages/purgecss/src",
"^@fullhuman/purgecss-from-html$": "<rootDir>/packages/purgecss-from-html/src",
},
rootDir: __dirname,
testMatch: ["<rootDir>/packages/**/__tests__/**/*test.ts"],
Expand Down
30 changes: 27 additions & 3 deletions packages/purgecss/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions packages/purgecss/__tests__/rejected.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe("rejected", () => {
".parent.d33ef1",
".parent2.def",
".parent3.def1",
"[href^='#']",
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
}
.parent3.def1{
color:red;
}

[href^='#'] {
color: green;
}
8 changes: 5 additions & 3 deletions packages/purgecss/src/VariablesStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
});
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ca69ec9

Please sign in to comment.