diff --git a/__tests__/data.js b/__tests__/data.js index b13fc62..843ea17 100644 --- a/__tests__/data.js +++ b/__tests__/data.js @@ -18,3 +18,37 @@ export const TEST_1_TAG = ["html", "head", "title", "body", "div", "a", "input"] export const TEST_1_CLASS = ["test-container", "test-footer", "a-link"] export const TEST_1_ID = ["a-link", "blo"] + +export const TEST_2_CONTENT = ` + + + + + +` + +export const TEST_2_TAG = ["div", "a", "input"] + +export const TEST_2_CLASS = ["test-container", "test-footer", "a-link"] + +export const TEST_2_ID = ["a-link", "blo"] diff --git a/__tests__/index.js b/__tests__/index.js index f86b9fc..9c4decf 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -1,33 +1,67 @@ import purgehtml from "./../index.js" import { TEST_1_CONTENT, TEST_1_TAG, TEST_1_CLASS, TEST_1_ID } from "./data" +import { TEST_2_CONTENT, TEST_2_TAG, TEST_2_CLASS, TEST_2_ID } from "./data" describe("purgehtml", () => { - it("finds tag selectors", () => { - const received = purgehtml.extract(TEST_1_CONTENT) - for (let item of TEST_1_TAG) { - expect(received.includes(item)).toBe(true) - } - }) + describe("from a normal html document", () => { + it("finds tag selectors", () => { + const received = purgehtml.extract(TEST_1_CONTENT) + for (let item of TEST_1_TAG) { + expect(received.includes(item)).toBe(true) + } + }) - it("finds classes selectors", () => { - const received = purgehtml.extract(TEST_1_CONTENT) - for (let item of TEST_1_CLASS) { - expect(received.includes(item)).toBe(true) - } - }) + it("finds classes selectors", () => { + const received = purgehtml.extract(TEST_1_CONTENT) + for (let item of TEST_1_CLASS) { + expect(received.includes(item)).toBe(true) + } + }) - it("finds id selectors", () => { - const received = purgehtml.extract(TEST_1_CONTENT) - for (let item of TEST_1_ID) { - expect(received.includes(item)).toBe(true) - } - }) + it("finds id selectors", () => { + const received = purgehtml.extract(TEST_1_CONTENT) + for (let item of TEST_1_ID) { + expect(received.includes(item)).toBe(true) + } + }) + + it("finds all selectors", () => { + const received = purgehtml.extract(TEST_1_CONTENT) + const selectors = [...TEST_1_TAG, ...TEST_1_CLASS, ...TEST_1_ID] + for (let item of selectors) { + expect(received.includes(item)).toBe(true) + } + }) + }); + + describe("from a template tag", () => { + it("finds tag selectors", () => { + const received = purgehtml.extract(TEST_2_CONTENT) + for (let item of TEST_2_TAG) { + expect(received.includes(item)).toBe(true) + } + }) + + it("finds classes selectors", () => { + const received = purgehtml.extract(TEST_2_CONTENT) + for (let item of TEST_2_CLASS) { + expect(received.includes(item)).toBe(true) + } + }) + + it("finds id selectors", () => { + const received = purgehtml.extract(TEST_2_CONTENT) + for (let item of TEST_2_ID) { + expect(received.includes(item)).toBe(true) + } + }) - it("finds all selectors", () => { - const received = purgehtml.extract(TEST_1_CONTENT) - const selectors = [...TEST_1_TAG, ...TEST_1_CLASS, ...TEST_1_ID] - for (let item of selectors) { - expect(received.includes(item)).toBe(true) - } + it("finds all selectors", () => { + const received = purgehtml.extract(TEST_2_CONTENT) + const selectors = [...TEST_2_TAG, ...TEST_2_CLASS, ...TEST_2_ID] + for (let item of selectors) { + expect(received.includes(item)).toBe(true) + } + }) }) }) diff --git a/index.js b/index.js index e308c60..3b912f6 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,11 @@ const getSelectorsInNodes = node => { ...ids, ...getSelectorsInNodes(childNode) ] + } else if (childNode.type === "root") { + selectors = [ + ...selectors, + ...getSelectorsInNodes(childNode) + ] } } return selectors diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..c1c5f83 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,3 @@ +module.exports = { + testEnvironment: "node" +}