I'm using React-testing-library getByText to find a button that's hidden by a material ui component called Hidden. Since RTL won't find it, Material ui has an implementation of it here https://material-ui.com/components/use-media-query/#testing. But I can't figure out how to implement it to simply to find a button. The button is located once I remove Hidden, I am just not sure how to use their createMatchMedia() for a query within my test.
The Hidden component hides my button at 959px and down .
import React from "react"
import { render } from '../../../../test/test-utils'
import Collections from "./Collections"
import userEvent from '@testing-library/user-event'
import mediaQuery from 'css-mediaquery'
function createMatchMedia(width: any) {
return (query: string): any => ({
matches: mediaQuery.match(query, { width }),
addListener: () => {},
removeListener: () => {},
});
}
type CollectionsProps = React.ComponentProps<typeof Collections>
const baseProps: CollectionsProps = {
setValue: () => {},
setSelectedIndex: () => {},
pageStyle: {},
pageAnimations: {transition : {}, variants: {}},
motions: {animate:'', initial: '', exit: ''},
jumpTo: (jumpingTarget: string | number | Element): void => {}
}
const renderUI = (props: Partial<CollectionsProps>) =>
render(<Collections {...baseProps} {...props} />, {})
describe('When a filter is clicked', () => {
beforeAll( () => {
window.matchMedia = createMatchMedia(window.innerWidth)
})
let { getByText } = renderUI({})
test('items shown are only related to the picked Category', () => {
userEvent.click(getByText(/Team Colors/))
})
})