123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- /*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- import {
- BoundFunction,
- GetByBoundAttribute,
- GetByRole,
- GetByText,
- screen,
- waitForOptions,
- within,
- } from '@testing-library/react';
-
- function maybeScreen(container?: HTMLElement) {
- return container ? within(container) : screen;
- }
-
- interface ReactTestingQuery {
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ): Promise<T>;
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ): Promise<T[]>;
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement): T;
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement): T[];
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement): T | null;
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement): T[] | null;
- }
-
- export class QuerySelector {
- dispatchQuery: ReactTestingQuery;
-
- constructor(dispatchQuery: ReactTestingQuery) {
- this.dispatchQuery = dispatchQuery;
- }
-
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ): Promise<T> {
- return this.dispatchQuery.find<T>(container, waitForOptions);
- }
-
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ): Promise<T[]> {
- return this.dispatchQuery.findAll<T>(container, waitForOptions);
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement): T {
- return this.dispatchQuery.get<T>(container);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement): T[] {
- return this.dispatchQuery.getAll<T>(container);
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement): T | null {
- return this.dispatchQuery.query<T>(container);
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement): T[] | null {
- return this.dispatchQuery.queryAll<T>(container);
- }
-
- getAt<T extends HTMLElement = HTMLElement>(index: number, container?: HTMLElement): T {
- return this.getAll<T>(container)[index];
- }
-
- async findAt<T extends HTMLElement = HTMLElement>(
- index: number,
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ): Promise<T> {
- return (await this.findAll<T>(container, waitForOptions))[index];
- }
-
- queryAt<T extends HTMLElement = HTMLElement>(index: number, container?: HTMLElement): T | null {
- const all = this.queryAll<T>(container);
- if (all) {
- return all[index];
- }
- return null;
- }
-
- by(selector: ReactTestingQuery): QuerySelector {
- return new ChainDispatch(this, selector);
- }
-
- byText(...args: Parameters<BoundFunction<GetByText>>): QuerySelector {
- return this.by(new DispatchByText(args));
- }
-
- byRole(...args: Parameters<BoundFunction<GetByRole>>): QuerySelector {
- return this.by(new DispatchByRole(args));
- }
-
- byPlaceholderText(...args: Parameters<BoundFunction<GetByBoundAttribute>>): QuerySelector {
- return this.by(new DispatchByPlaceholderText(args));
- }
-
- byLabelText(...args: Parameters<BoundFunction<GetByText>>): QuerySelector {
- return this.by(new DispatchByLabelText(args));
- }
-
- byTestId(...args: Parameters<BoundFunction<GetByBoundAttribute>>): QuerySelector {
- return this.by(new DispatchByTestId(args));
- }
-
- byDisplayValue(...args: Parameters<BoundFunction<GetByBoundAttribute>>): QuerySelector {
- return this.by(new DispatchByDisplayValue(args));
- }
-
- byTitle(...args: Parameters<BoundFunction<GetByBoundAttribute>>): ReactTestingQuery {
- return this.by(new DispatchByTitle(args));
- }
- }
-
- class ChainDispatch extends QuerySelector {
- innerQuery: QuerySelector;
-
- constructor(insideQuery: QuerySelector, elementQuery: ReactTestingQuery) {
- super(elementQuery);
- this.innerQuery = insideQuery;
- }
-
- async find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- let inside: HTMLElement;
- try {
- inside = await this.innerQuery.find(container, waitForOptions);
- } catch (e) {
- const elements = this.innerQuery.getAll(container);
- const all = (
- await Promise.all(
- elements.map((e) => this.dispatchQuery.findAll<T>(e, waitForOptions).catch(() => null)),
- )
- )
- .flat()
- .filter((e) => e !== null);
- if (all.length !== 1) {
- throw e;
- }
- return all[0] as T;
- }
- return this.dispatchQuery.find<T>(inside, waitForOptions);
- }
-
- async findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return this.dispatchQuery.findAll<T>(await this.innerQuery.find(container, waitForOptions));
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- let inside: HTMLElement;
- try {
- inside = this.innerQuery.get(container);
- } catch (e) {
- const elements = this.innerQuery.getAll(container);
- const all = elements.map((e) => this.dispatchQuery.query<T>(e)).filter((e) => e !== null);
- if (all.length !== 1) {
- throw e;
- }
- return all[0] as T;
- }
- return this.dispatchQuery.get<T>(inside);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- const containers = this.innerQuery.getAll(container);
- return containers.reduce(
- (acc, item) => [...acc, ...(this.dispatchQuery.queryAll<T>(item) ?? [])],
- [],
- );
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- let inside: HTMLElement | null;
- try {
- inside = this.innerQuery.query(container);
- } catch (e) {
- const elements = this.innerQuery.queryAll(container);
- const all = elements?.map((e) => this.dispatchQuery.query<T>(e)).filter((e) => e !== null);
- if (all?.length !== 1) {
- throw e;
- }
- return all[0];
- }
- if (inside) {
- return this.dispatchQuery.query<T>(inside);
- }
- return null;
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- const innerContainer = this.innerQuery.query(container);
- if (innerContainer) {
- return this.dispatchQuery.queryAll<T>(innerContainer);
- }
- return null;
- }
- }
-
- class DispatchByText implements ReactTestingQuery {
- readonly args: Parameters<BoundFunction<GetByText>>;
-
- constructor(args: Parameters<BoundFunction<GetByText>>) {
- this.args = args;
- }
-
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findByText<T>(...this.args, waitForOptions);
- }
-
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findAllByText<T>(...this.args, waitForOptions);
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getByText<T>(...this.args);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getAllByText<T>(...this.args);
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryByText<T>(...this.args);
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryAllByText<T>(...this.args);
- }
- }
-
- class DispatchByLabelText implements ReactTestingQuery {
- readonly args: Parameters<BoundFunction<GetByText>>;
-
- constructor(args: Parameters<BoundFunction<GetByText>>) {
- this.args = args;
- }
-
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findByLabelText<T>(...this.args, waitForOptions);
- }
-
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findAllByLabelText<T>(...this.args, waitForOptions);
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getByLabelText<T>(...this.args);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getAllByLabelText<T>(...this.args);
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryByLabelText<T>(...this.args);
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryAllByLabelText<T>(...this.args);
- }
- }
-
- class DispatchByRole implements ReactTestingQuery {
- readonly args: Parameters<BoundFunction<GetByRole>>;
-
- constructor(args: Parameters<BoundFunction<GetByRole>>) {
- this.args = args;
- }
-
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findByRole<T>(...this.args, waitForOptions);
- }
-
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findAllByRole<T>(...this.args, waitForOptions);
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getByRole<T>(...this.args);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getAllByRole<T>(...this.args);
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryByRole<T>(...this.args);
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryAllByRole<T>(...this.args);
- }
- }
-
- class DispatchByTestId implements ReactTestingQuery {
- readonly args: Parameters<BoundFunction<GetByBoundAttribute>>;
-
- constructor(args: Parameters<BoundFunction<GetByBoundAttribute>>) {
- this.args = args;
- }
-
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findByTestId<T>(...this.args, waitForOptions);
- }
-
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findAllByTestId<T>(...this.args, waitForOptions);
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getByTestId<T>(...this.args);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getAllByTestId<T>(...this.args);
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryByTestId<T>(...this.args);
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryAllByTestId<T>(...this.args);
- }
- }
-
- class DispatchByTitle implements ReactTestingQuery {
- readonly args: Parameters<BoundFunction<GetByBoundAttribute>>;
-
- constructor(args: Parameters<BoundFunction<GetByBoundAttribute>>) {
- this.args = args;
- }
-
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findByTitle<T>(...this.args, waitForOptions);
- }
-
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findAllByTitle<T>(...this.args, waitForOptions);
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getByTitle<T>(...this.args);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getAllByTitle<T>(...this.args);
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryByTitle<T>(...this.args);
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryAllByTitle<T>(...this.args);
- }
- }
-
- class DispatchByDisplayValue implements ReactTestingQuery {
- readonly args: Parameters<BoundFunction<GetByBoundAttribute>>;
-
- constructor(args: Parameters<BoundFunction<GetByBoundAttribute>>) {
- this.args = args;
- }
-
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findByDisplayValue<T>(...this.args, waitForOptions);
- }
-
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findAllByDisplayValue<T>(...this.args, waitForOptions);
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getByDisplayValue<T>(...this.args);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getAllByDisplayValue<T>(...this.args);
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryByDisplayValue<T>(...this.args);
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryAllByDisplayValue<T>(...this.args);
- }
- }
-
- class DispatchByPlaceholderText implements ReactTestingQuery {
- readonly args: Parameters<BoundFunction<GetByBoundAttribute>>;
-
- constructor(args: Parameters<BoundFunction<GetByBoundAttribute>>) {
- this.args = args;
- }
-
- find<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findByPlaceholderText<T>(...this.args, waitForOptions);
- }
-
- findAll<T extends HTMLElement = HTMLElement>(
- container?: HTMLElement,
- waitForOptions?: waitForOptions,
- ) {
- return maybeScreen(container).findAllByPlaceholderText<T>(...this.args, waitForOptions);
- }
-
- get<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getByPlaceholderText<T>(...this.args);
- }
-
- getAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).getAllByPlaceholderText<T>(...this.args);
- }
-
- query<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryByPlaceholderText<T>(...this.args);
- }
-
- queryAll<T extends HTMLElement = HTMLElement>(container?: HTMLElement) {
- return maybeScreen(container).queryAllByPlaceholderText<T>(...this.args);
- }
- }
-
- export function byText(...args: Parameters<BoundFunction<GetByText>>) {
- return new QuerySelector(new DispatchByText(args));
- }
-
- export function byRole(...args: Parameters<BoundFunction<GetByRole>>) {
- return new QuerySelector(new DispatchByRole(args));
- }
-
- export function byPlaceholderText(...args: Parameters<BoundFunction<GetByBoundAttribute>>) {
- return new QuerySelector(new DispatchByPlaceholderText(args));
- }
-
- export function byLabelText(...args: Parameters<BoundFunction<GetByText>>) {
- return new QuerySelector(new DispatchByLabelText(args));
- }
-
- export function byTestId(...args: Parameters<BoundFunction<GetByBoundAttribute>>) {
- return new QuerySelector(new DispatchByTestId(args));
- }
-
- export function byTitle(...args: Parameters<BoundFunction<GetByBoundAttribute>>) {
- return new QuerySelector(new DispatchByTitle(args));
- }
-
- export function byDisplayValue(...args: Parameters<BoundFunction<GetByBoundAttribute>>) {
- return new QuerySelector(new DispatchByDisplayValue(args));
- }
|