import { describe, expect, it } from "@effect/vitest" import { Locator } from "effect/ui/testing" const el = ( tag: string, attrs: Record = {}, ...children: ReadonlyArray ): Locator.Node => ({ tag, attrs, children }) const ids = (root: Locator.Node, locator: Locator.Locator) => Locator.all(root, locator).map((node) => node.attrs?.id) describe("Locator", () => { it("className matches one token of class, attr matches the whole value", () => { const tree = el( "ul", {}, el("li", { id: "a", class: "row" }), el("li", { id: "b", class: " selected\trow" }), el("li", { id: "c", class: "rows" }), el("li", { id: "d", class: "row\u00a0selected" }) ) expect(ids(tree, Locator.className("row"))).toEqual(["a", "b"]) expect(ids(tree, Locator.className(""))).toEqual([]) expect(ids(tree, Locator.attr("class", "row"))).toEqual(["a"]) expect(Locator.className("row").description).toBe(".row") }) it("attr matches a RegExp value and only own attributes", () => { const tree = el( "ul", {}, el("li", { id: "row-1" }), el("li", { id: "item-row-2" }) ) const locator = Locator.attr("id", /^row-/) expect(ids(tree, locator)).toEqual(["row-1"]) expect(locator.description).toBe("[id=/^row-/]") expect(Locator.all(tree, Locator.attr("constructor"))).toEqual([]) }) it("and with not translates li.row:not(.selected) button", () => { const row = (id: string, className: string) => el("li", { class: className }, el("span", {}, el("button", { id }))) const tree = el( "ul", {}, row("a", "row selected"), row("b", "row"), el("div", { class: "row" }, el("button", { id: "c" })) ) const unselectedRow = Locator.and( Locator.tag("li"), Locator.className("row"), Locator.not(Locator.className("selected")) ) expect(ids(tree, Locator.inside(unselectedRow, Locator.tag("button")))) .toEqual(["b"]) expect(unselectedRow.description).toBe("
  • and .row and not .selected") }) it("locators pipe through and, not and inside, made ones included", () => { const tree = el( "form", { id: "form" }, el("button", { id: "a", class: "primary" }), el("button", { id: "b", class: "primary selected" }) ) const mine = Locator.make( "mine", (root) => Locator.elements(root).filter((node) => node.attrs?.id === "form") ) const button = Locator.tag("button").pipe( Locator.and(Locator.className("primary")), Locator.and(Locator.not(Locator.className("selected"))), Locator.inside(mine) ) expect(ids(tree, button)).toEqual(["a"]) expect(ids(tree, mine.pipe(Locator.not))).toEqual(["a", "b"]) }) })