tuil
ReferencePackages@mwillbanks/tuil-testing-inkAPI

SemanticScreen

export exported by @mwillbanks/tuil-testing-ink.

View rawEdit

export

Public export exported by @mwillbanks/tuil-testing-ink.

export class SemanticScreen {
  constructor(readonly snapshot: () => SemanticSnapshot) {}

  frame(): string {
    return this.snapshot().frame;
  }

  getByRole(
    role: SemanticRole,
    query: SemanticQuery = {},
  ): QueryableSemanticNode {
    return this.#one(
      this.snapshot().nodes.filter(
        (node) =>
          node.role === role &&
          matchesText(node.label, query.name) &&
          (query.selected === undefined || node.selected === query.selected) &&
          (query.checked === undefined || node.checked === query.checked) &&
          (query.disabled === undefined || node.disabled === query.disabled),
      ),
      describe("role", { role, ...query }),
    );
  }

  getAllByRole(
    role: SemanticRole,
    query: SemanticQuery = {},
  ): readonly QueryableSemanticNode[] {
    const matches = this.snapshot().nodes.filter(
      (node) =>
        node.role === role &&
        matchesText(node.label, query.name) &&
        (query.selected === undefined || node.selected === query.selected) &&
        (query.checked === undefined || node.checked === query.checked) &&
        (query.disabled === undefined || node.disabled === query.disabled),
    );
    if (matches.length === 0) {
      throw new Error(
        `Unable to find any semantic nodes by ${describe("role", role)}`,
      );
    }
    return matches;
  }

  getByLabelText(label: string | RegExp): QueryableSemanticNode {
    return this.#one(
      this.snapshot().nodes.filter((node) => matchesText(node.label, label)),
      describe("label", label),
    );
  }

  getByText(text: string | RegExp): QueryableSemanticNode {
    return this.#one(
      this.snapshot().nodes.filter(
        (node) => matchesText(node.text, text) || matchesText(node.label, text),
      ),
      describe("text", text),
    );
  }

  getByTestId(testId: string): QueryableSemanticNode {
    return this.#one(
      this.snapshot().nodes.filter((node) => node.testId === testId),
      describe("test id", testId),
    );
  }

  #one(
    matches: readonly QueryableSemanticNode[],
    description: string,
  ): QueryableSemanticNode {
    if (matches.length === 0) {
      throw new Error(`Unable to find a semantic node by ${description}`);
    }
    if (matches.length > 1) {
      throw new Error(`Found multiple semantic nodes by ${description}`);
    }
    return matches[0] as QueryableSemanticNode;
  }
}

Members

MemberTypeRequiredDescriptionRelated types
__constructoranyYesThe __constructor member uses the any contract.
frame() => stringYesThe frame member uses the () => string contract.
getByRole(role: SemanticRole, query?: SemanticQuery) => QueryableSemanticNodeYesThe getByRole member uses the (role: SemanticRole, query?: SemanticQuery) => QueryableSemanticNode contract.QueryableSemanticNode, SemanticQuery, SemanticRole
getAllByRole(role: SemanticRole, query?: SemanticQuery) => readonly QueryableSemanticNode[]YesThe getAllByRole member uses the (role: SemanticRole, query?: SemanticQuery) => readonly QueryableSemanticNode[] contract.QueryableSemanticNode, SemanticQuery, SemanticRole
getByLabelText(label: string | RegExp) => QueryableSemanticNodeYesThe getByLabelText member uses the (label: string | RegExp) => QueryableSemanticNode contract.QueryableSemanticNode
getByText(text: string | RegExp) => QueryableSemanticNodeYesThe getByText member uses the (text: string | RegExp) => QueryableSemanticNode contract.QueryableSemanticNode
getByTestId(testId: string) => QueryableSemanticNodeYesThe getByTestId member uses the (testId: string) => QueryableSemanticNode contract.QueryableSemanticNode
#one(matches: readonly QueryableSemanticNode[], description: string) => QueryableSemanticNodeYesThe #one member uses the (matches: readonly QueryableSemanticNode[], description: string) => QueryableSemanticNode contract.QueryableSemanticNode

Parameters

This declaration has no public members.

Returns

This declaration does not return a value.

Throws

No thrown errors are documented for this declaration.

No package-local related types.

Source

View the secondary source reference

Package

@mwillbanks/tuil-testing-ink

On this page