Skip to content

Commit

Permalink
test(extract-selectors): create test suite for extractSelectors
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Dec 8, 2024
1 parent 14d7b96 commit b72c5e6
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/extract-selectors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { extractSelectors } from "../scripts/extract-selectors";

describe("extractSelectors", () => {
test("extracts single class from class attribute", () => {
const result = extractSelectors({
code: '<div class="test-class"></div>',
filename: "test.svelte",
});
expect(result).toEqual([".test-class"]);
});

test("extracts multiple classes from class attribute", () => {
const result = extractSelectors({
code: '<div class="class1 class2 class3"></div>',
filename: "test.svelte",
});
expect(result).toEqual([".class1", ".class2", ".class3"]);
});

test("extracts class directives", () => {
const result = extractSelectors({
code: "<div class:active={isActive}></div>",
filename: "test.svelte",
});
expect(result).toEqual([".active"]);
});

test("extracts global selectors", () => {
const result = extractSelectors({
code: "<style>:global(div) { color: red; }</style>",
filename: "test.svelte",
});
expect(result).toEqual([".div"]);
});

test("handles Carbon prefix classes", () => {
const result = extractSelectors({
code: '<div class="bx--button bx--text"></div>',
filename: "test.svelte",
});
expect(result).toEqual([".bx--button", ".bx--text"]);
});

test("deduplicates repeated classes", () => {
const result = extractSelectors({
code: '<div class="test test test"></div>',
filename: "test.svelte",
});
expect(result).toEqual([".test"]);
});

test("handles mixed scenarios", () => {
const result = extractSelectors({
code: `
<div class="regular-class bx--carbon-class">
<span class:active={isActive}></span>
<style>:global(body) { margin: 0; }</style>
</div>
`,
filename: "test.svelte",
});
expect(result).toEqual([".regular-class", ".bx--carbon-class", ".active"]);
});

test("handles empty class attributes", () => {
const result = extractSelectors({
code: '<div class=""></div>',
filename: "test.svelte",
});
expect(result).toEqual([]);
});
});

0 comments on commit b72c5e6

Please sign in to comment.