Skip to content

feat: Add ByTextContent query #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/__tests__/text-matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cases(
queryAllByAltText: {
dom: `
<img
alt="Finding Nemo poster"
alt="Finding Nemo poster"
src="/finding-nemo.png"
/>`,
query: `Finding Nemo poster`,
Expand Down Expand Up @@ -87,7 +87,7 @@ cases(
dom: `
<img
alt="
Finding Nemo poster "
Finding Nemo poster "
src="/finding-nemo.png"
/>`,
query: /^Finding Nemo poster$/,
Expand Down Expand Up @@ -192,7 +192,7 @@ cases(
queryAllByAltText: {
dom: `
<img
alt="Finding Nemo poster"
alt="Finding Nemo poster"
src="/finding-nemo.png"
/>`,
query: `Finding Nemo poster`,
Expand Down Expand Up @@ -321,3 +321,18 @@ test('we support an older API with trim and collapseWhitespace instead of a norm
expect(queryAllByText('x y', {collapseWhitespace: false})).toHaveLength(0)
expect(queryAllByText('x y', {collapseWhitespace: false})).toHaveLength(1)
})

test('ByTextContent is a simple selector for HTMLElement.textContent', () => {
const {queryAllByTextContent} = render(
'<button> <span>Click</span> <span>me</span></button>',
)
expect(queryAllByTextContent('Click me', 'button')).toHaveLength(0)
expect(queryAllByTextContent(' Click me', 'button')).toHaveLength(1)
})

test('ByTextContent tests all elements by default', () => {
const {container,queryAllByTextContent} = render(
'<span>jekyll</span><div>jekyll</div></button>',
)
expect(queryAllByTextContent('jekyll')).toHaveLength(2)
})
1 change: 1 addition & 0 deletions src/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './alt-text'
export * from './title'
export * from './role'
export * from './test-id'
export * from './text-content'
29 changes: 29 additions & 0 deletions src/queries/text-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {buildQueries, matches} from './all-utils'

function queryAllByTextContent(container, text, selector = '*') {
return Array.from(container.querySelectorAll(selector)).filter(node =>
matches(node.textContent, node, text, s => s),
)
}

const getMultipleError = (c, text) =>
`Found multiple elements with the text content: ${text}`
const getMissingError = (c, text) =>
`Unable to find an element with the text content: ${text}. This could be because your text input didn't consider whitespace.`

const [
queryByTextContent,
getAllByTextContent,
getByTextContent,
findAllByTextContent,
findByTextContent,
] = buildQueries(queryAllByTextContent, getMultipleError, getMissingError)

export {
queryByTextContent,
queryAllByTextContent,
getByTextContent,
getAllByTextContent,
findAllByTextContent,
findByTextContent,
}
38 changes: 38 additions & 0 deletions typings/queries.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ export type FindByText = (
waitForElementOptions?: WaitForElementOptions,
) => Promise<HTMLElement>

export type QueryByTextContent = (
container: HTMLElement,
id: Matcher,
selector?: string,
) => HTMLElement | null

export type AllByTextContent = (
container: HTMLElement,
id: Matcher,
selector?: string,
) => HTMLElement[]

export type FindAllByTextContent = (
container: HTMLElement,
id: Matcher,
selector?: string,
waitForElementOptions?: WaitForElementOptions,
) => Promise<HTMLElement[]>

export type GetByTextContent = (
container: HTMLElement,
id: Matcher,
selector?: string,
) => HTMLElement

export type FindByTextContent = (
container: HTMLElement,
id: Matcher,
selector?: string,
waitForElementOptions?: WaitForElementOptions,
) => Promise<HTMLElement>

export const getByLabelText: GetByText
export const getAllByLabelText: AllByText
export const queryByLabelText: QueryByText
Expand All @@ -84,6 +116,12 @@ export const queryByText: QueryByText
export const queryAllByText: AllByText
export const findByText: FindByText
export const findAllByText: FindAllByText
export const getByTextContent: GetByTextContent
export const getAllByTextContent: AllByTextContent
export const queryByTextContent: QueryByTextContent
export const queryAllByTextContent: AllByTextContent
export const findByTextContent: FindByTextContent
export const findAllByTextContent: FindAllByTextContent
export const getByAltText: GetByBoundAttribute
export const getAllByAltText: AllByBoundAttribute
export const queryByAltText: QueryByBoundAttribute
Expand Down