Files
supabase/studio/tests/helpers.ts
2022-03-01 13:58:55 +08:00

38 lines
872 B
TypeScript

import { screen, getByText, fireEvent } from '@testing-library/react'
interface SelectorOptions {
container?: HTMLElement
}
/**
* Returns the toggle button given a text matcher
*
* Defaults to screen if container option is not provided
*/
export const getToggleByText = (
text: string | RegExp,
options: SelectorOptions = {}
): HTMLElement | null => {
const container = options?.container
let textNode
if (container) {
textNode = getByText(container as HTMLElement, text)
} else {
textNode = screen.getByText(text)
}
if (textNode && textNode.parentElement) {
return textNode.parentElement.querySelector('button')
} else {
return textNode
}
}
export const clickDropdown = (elem: HTMLElement) => {
fireEvent.pointerDown(
elem,
new window.PointerEvent('pointerdown', {
ctrlKey: false,
button: 0,
})
)
}