1import { readFileSync } from "fs"; 2import { parse } from "."; 3import { tests } from "./__fixtures__/tests"; 4 5const broken = [ 6 "[", 7 "(", 8 "{", 9 "()", 10 "<>", 11 "{}", 12 ",", 13 ",a", 14 "a,", 15 "[id=012345678901234567890123456789", 16 "input[name=foo b]", 17 "input[name!foo]", 18 "input[name|]", 19 "input[name=']", 20 "input[name=foo[baz]]", 21 ':has("p")', 22 ":has(p", 23 ":foo(p()", 24 "#", 25 "##foo", 26 "/*", 27]; 28 29describe("Parse", () => { 30 describe("Own tests", () => { 31 for (const [selector, expected, message] of tests) { 32 test(message, () => 33 expect(parse(selector)).toStrictEqual(expected) 34 ); 35 } 36 }); 37 38 describe("Collected selectors (qwery, sizzle, nwmatcher)", () => { 39 const out = JSON.parse( 40 readFileSync(`${__dirname}/__fixtures__/out.json`, "utf8") 41 ); 42 for (const s of Object.keys(out)) { 43 test(s, () => { 44 expect(parse(s)).toStrictEqual(out[s]); 45 }); 46 } 47 }); 48 49 describe("Broken selectors", () => { 50 for (const selector of broken) { 51 it(`should not parse — ${selector}`, () => { 52 expect(() => parse(selector)).toThrow(Error); 53 }); 54 } 55 }); 56 57 it("should ignore comments", () => { 58 expect(parse("/* comment1 */ /**/ foo /*comment2*/")).toEqual([ 59 [{ name: "foo", namespace: null, type: "tag" }], 60 ]); 61 62 expect(() => parse("/*/")).toThrowError("Comment was not terminated"); 63 }); 64}); 65