Skip to content

Commit

Permalink
Improve unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Mar 23, 2021
1 parent dc61c0e commit a62525f
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions test/attributes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as CSSselect from "../src";
import { parseDOM } from "htmlparser2";
import { parseDocument } from "htmlparser2";
import { falseFunc } from "boolbase";
import type { Element } from "domhandler";

const dom = parseDOM(
'<div><div data-foo="In the end, it doesn\'t really matter."></div><div data-foo="Indeed-that\'s a delicate matter.">'
) as Element[];
const dom = parseDocument(
'<div data-foo="In the end, it doesn\'t really matter."></div><div data-foo="Indeed-that\'s a delicate matter.">'
);
const domChilds = dom.children as Element[];

describe("Attributes", () => {
describe("ignore case", () => {
Expand All @@ -15,70 +16,70 @@ describe("Attributes", () => {
dom
);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
matches = CSSselect.selectAll(
'[data-foo="inDeeD-THAT\'s a DELICATE matteR." i]',
dom
);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
});

it("should for ^=", () => {
let matches = CSSselect.selectAll("[data-foo^=IN i]", dom);
expect(matches).toHaveLength(2);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
matches = CSSselect.selectAll("[data-foo^=in i]", dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
matches = CSSselect.selectAll("[data-foo^=iN i]", dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
});

it("should for $=", () => {
let matches = CSSselect.selectAll('[data-foo$="MATTER." i]', dom);
expect(matches).toHaveLength(2);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
matches = CSSselect.selectAll('[data-foo$="matter." i]', dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
matches = CSSselect.selectAll('[data-foo$="MaTtEr." i]', dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
});

it("should for !=", () => {
let matches = CSSselect.selectAll(
'[data-foo!="indeed-that\'s a delicate matter." i]',
dom
);
expect(matches).toHaveLength(2);
expect(matches).toStrictEqual([dom[0].children[0] as Element]);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([domChilds[0]]);
matches = CSSselect.selectAll(
'[data-foo!="inDeeD-THAT\'s a DELICATE matteR." i]',
dom
);
expect(matches).toStrictEqual([dom[0].children[0] as Element]);
expect(matches).toStrictEqual([domChilds[0]]);
});

it("should for *=", () => {
let matches = CSSselect.selectAll("[data-foo*=IT i]", dom);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[0] as Element]);
expect(matches).toStrictEqual([domChilds[0]]);
matches = CSSselect.selectAll("[data-foo*=tH i]", dom);
expect(matches).toStrictEqual(dom[0].children as Element[]);
expect(matches).toStrictEqual(domChilds);
});

it("should for |=", () => {
let matches = CSSselect.selectAll("[data-foo|=indeed i]", dom);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
matches = CSSselect.selectAll("[data-foo|=inDeeD i]", dom);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
});

it("should for ~=", () => {
let matches = CSSselect.selectAll("[data-foo~=IT i]", dom);
expect(matches).toHaveLength(1);
expect(matches).toStrictEqual([dom[0].children[0] as Element]);
expect(matches).toStrictEqual([domChilds[0]]);
matches = CSSselect.selectAll("[data-foo~=dElIcAtE i]", dom);
expect(matches).toStrictEqual([dom[0].children[1] as Element]);
expect(matches).toStrictEqual([domChilds[1]]);
});
});

Expand Down

0 comments on commit a62525f

Please sign in to comment.