Skip to content

Commit

Permalink
feat: byAttribute API merges MatcherOptions and WaitForOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
molvqingtai committed Mar 13, 2024
1 parent 8868a7a commit a7f5b61
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
43 changes: 27 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const dialogButton = getByAttribute(document.body, 'data-action', /^open/)

userEvent.click(dialogButton)

const dialog = await findByAttribute(document.body, 'data-id', 'dialog', undefined, { timeout: 1000 })
const dialog = await findByAttribute(document.body, 'data-id', 'dialog', { timeout: 1000 })

const usernameInput = getByAttribute(dialog, 'data-name', 'username')
const passwordInput = getByAttribute(dialog, 'data-name', 'password')
Expand All @@ -105,25 +105,36 @@ userEvent.click(loginButton)

## Expose API

This project exposes the following api, and the usage is consistent with the original project [@testing-library/dom](https://github.com/testing-library/dom-testing-library) .
This project exposes the following api, and the usage is consistent with the original project [types-of-queries](https://testing-library.com/docs/queries/about/#types-of-queries).

**BySelector**
* **BySelector**

* getBySelector
* getAllBySelector
* queryBySelector
* queryAllBySelector
* findBySelector
* findAllBySelector
+ getBySelector

**ByAttribute**
* getAllBySelector

* queryBySelector

* queryAllBySelector

* findBySelector

* findAllBySelector

* **ByAttribute**

* getByAttribute

* getAllByAttribute

* queryByAttribute

* queryAllByAttribute

* findByAttribute

* findAllByAttribute

* getByAttribute
* getAllByAttribute
* queryByAttribute
* queryAllByAttribute
* findByAttribute
* findAllByAttribute



Expand Down
14 changes: 4 additions & 10 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('Test byAttribute', () => {
const loginButtonRef = getByAttribute(container, 'class', 'login-button')
userEvent.click(loginButtonRef)

const messageRef = await findByAttribute(container, 'class', 'message', undefined, { timeout: 2000 })
const messageRef = await findByAttribute(container, 'class', 'message', { timeout: 2000 })
expect(messageRef.textContent).toBe('Login success')
})

Expand All @@ -137,15 +137,9 @@ describe('Test byAttribute', () => {
const loginButtonRef = getByAttribute(container, 'class', 'login-button')
userEvent.click(loginButtonRef)

const [messageRef, closeButtonRef] = await findAllByAttribute(
container,
'class',
/message|close-button/,
undefined,
{
timeout: 2000
}
)
const [messageRef, closeButtonRef] = await findAllByAttribute(container, 'class', /message|close-button/, {
timeout: 2000
})
expect(messageRef.textContent).toBe('Login success')
expect(closeButtonRef.textContent).toBe('close')
})
Expand Down
10 changes: 4 additions & 6 deletions src/queries/byAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,20 @@ const findAllByAttribute = async <R extends HTMLElement = HTMLElement>(
container: HTMLElement,
attribute: string,
value: Matcher,
options?: MatcherOptions,
waitForOptions?: WaitForOptions
options?: MatcherOptions & WaitForOptions
): Promise<R[]> => {
// @ts-expect-error -- Incorrect derivation of buildQueries internal type
return (await _findAllByAttribute(container, value, { ...options, attribute }, waitForOptions)) as R[]
return (await _findAllByAttribute(container, value, { ...options, attribute }, options)) as R[]
}

const findByAttribute = async <R extends HTMLElement = HTMLElement>(
container: HTMLElement,
attribute: string,
value: Matcher,
options?: MatcherOptions,
waitForOptions?: WaitForOptions
options?: MatcherOptions & WaitForOptions
): Promise<R> => {
// @ts-expect-error -- Incorrect derivation of buildQueries internal type
return (await _findByAttribute(container, value, { ...options, attribute }, waitForOptions)) as R
return (await _findByAttribute(container, value, { ...options, attribute }, options)) as R
}

export { queryByAttribute, queryAllByAttribute, getAllByAttribute, getByAttribute, findAllByAttribute, findByAttribute }

0 comments on commit a7f5b61

Please sign in to comment.