Skip to content

Commit

Permalink
feat(queries): add "When I find elements by label text"
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Oct 7, 2023
1 parent 897e399 commit f6df821
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions src/queries/label.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
import { When } from '@badeball/cypress-cucumber-preprocessor';

import { setCypressElementByLabelText } from '../utils';
import {
getCypressElement,
setCypressElement,
setCypressElementsByLabelText,
} from '../utils';

/**
* When I find elements by label text:
*
* ```gherkin
* When I find elements by label text {string}
* ```
*
* Finds all `label`, `aria-labelledby`, or `aria-label` that matches the text.
*
* @example
*
* ```gherkin
* When I find elements by label text "Email"
* ```
*
* @remarks
*
* This precedes steps like {@link When_I_type | "When I type"}. For example:
*
* ```gherkin
* When I find elements by label text "Email"
* And I get 1st element
* And I type "user@example.com"
* ```
*
* Inspired by Testing Library's [ByLabelText](https://testing-library.com/docs/queries/bylabeltext).
*
* @see
*
* - {@link When_I_find_element_by_label_text | When I find element by label text }
*/
export function When_I_find_elements_by_label_text(text: string) {
setCypressElementsByLabelText(text);
}

When(
'I find elements by label text {string}',
When_I_find_elements_by_label_text,
);

/**
* When I find element by label text:
Expand Down Expand Up @@ -30,10 +74,11 @@ import { setCypressElementByLabelText } from '../utils';
*
* @see
*
* - {@link When_I_find_input_by_label_text | When I find input by label text }
* - {@link When_I_find_elements_by_label_text | When I find elements by label text }
*/
export function When_I_find_element_by_label_text(text: string) {
setCypressElementByLabelText(text);
When_I_find_elements_by_label_text(text);
setCypressElement(getCypressElement().first());
}

When(
Expand Down

0 comments on commit f6df821

Please sign in to comment.