Skip to content

Commit

Permalink
chore: improved data testing by ui and test id (#7428)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanl17 authored Aug 30, 2024
1 parent 9da634e commit deca138
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/sanity/src/ui-components/dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const Dialog = forwardRef(function Dialog(
text={t('common.dialog.cancel-button.text')}
tone="default"
onClick={props.onClose}
data-testid="cancel-button"
{...footer.cancelButton}
/>
)}
Expand All @@ -84,6 +85,7 @@ export const Dialog = forwardRef(function Dialog(
padding={2}
text={t('common.dialog.confirm-button.text')}
tone="critical"
data-testid="confirm-button"
{...footer.confirmButton}
/>
)}
Expand Down
18 changes: 18 additions & 0 deletions packages/sanity/test/setup/customQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {buildQueries, type Matcher, type MatcherOptions, queryHelpers} from '@testing-library/react'

const queryAllByDataUi = (container: HTMLElement, id: Matcher, options?: MatcherOptions) => {
return queryHelpers.queryAllByAttribute('data-ui', container, id, options)
}

const getMultipleError = (c: any, dataUiValue: string) =>
`Found multiple elements with the data-ui attribute of: ${dataUiValue}`
const getMissingError = (c: any, dataUiValue: string) =>
`Unable to find an element with the data-ui attribute of: ${dataUiValue}`

const [queryByDataUi, getAllByDataUi, getByDataUi, findAllByDataUi, findByDataUi] = buildQueries(
queryAllByDataUi,
getMultipleError,
getMissingError,
)

export {findAllByDataUi, findByDataUi, getAllByDataUi, getByDataUi, queryAllByDataUi, queryByDataUi}

0 comments on commit deca138

Please sign in to comment.