import userEvent from '@testing-library/user-event' import * as React from 'react' import renderWithTheme from '../../tests/helpers/renderWithTheme' import { ScreenSizeContext, SIZES } from '../ScreenSizeProvider/ScreenSizeProvider' import MenuBar from './MenuBar' // @ts-ignore global.MutationObserver = class { constructor() { return } disconnect() { return } observe() { return } } // Example for typescript how to use a custom component with MenuBar.Item. const Linkish = (props: { to: string; children: string }) => {props.children} interface Sample { sample: string prop?: number } const WithRef = React.forwardRef(({ sample, prop }, ref) => (
{sample} {prop}
)) class Class extends React.Component { render() { return `${this.props.sample} ${this.props.prop}` } } const Menu = () => { const navRef = React.useRef(null) const listRef = React.useRef(null) const linkRef = React.useRef(null) const itemRef = React.useRef(null) const customRef = React.useRef(null) return ( Emphasized} aria-current> Link to the Past Birdy The Mighty alert('Clicked')} children={A button} /> Styled Link ) } describe('component: MenuBar', () => { test('sidebar', () => { const { container } = renderWithTheme( ) const hamburger = container.querySelectorAll('[role="button"]') expect(hamburger).toHaveLength(1) }) test('sidebar collapse', () => { const { getByText, container } = renderWithTheme( ) const hamburger = container.querySelector('[role="button"]') as HTMLElement const nestedList = getByText('Emphasized').closest('span') as HTMLElement const nestedItem = getByText('Nested') expect(hamburger).toBeVisible() expect(nestedList).not.toBeVisible() expect(nestedItem).not.toBeVisible() userEvent.click(hamburger) expect(nestedList).toBeVisible() expect(nestedItem).not.toBeVisible() userEvent.click(nestedList) expect(nestedList).toBeVisible() expect(nestedItem).toBeVisible() userEvent.click(nestedList) expect(nestedList).toBeVisible() expect(nestedItem).not.toBeVisible() }) test('focus', () => { const { getByText } = renderWithTheme( First Submenu First Nested First Nested Last Submenu Last Last ) userEvent.tab() expect(getByText('First')).toHaveFocus() /* NOTE * As it stands, most of the logic in this component relies on elements * being "visible", and as such the logic doesn't work in tests. I'm keeping * this line, commented out, in case I have to change the algorithms when * implementing the "small" styles to support the vertical menubar. */ //fireEvent.keyDown(document.activeElement, { key: 'ArrowRight' }) //expect(getByText('Submenu')).toHaveFocus() }) // NOTE Uncommenting this should result in 13 Typescript errors, one for each line. //test('bad types', () => { // const Typing = () => { // const ref1 = React.useRef(null) // const ref2 = React.useRef(null) // const ref3 = React.useRef(null) // const ref6 = React.useRef(null) // return ( // // One // Two // Three // {/* Doesn't accept children */} // Four // Five // {/* Doesn't accept ref */} // Six // {/* Unknown prop */} // Seven // // // {/* Missing required props */} // Ten // // // // // ) // } // expect(0).toBe(1) //}) })