Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HamburgerMenu: rewrote using hooks + added tests #339

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions src/components/HamburgerMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import * as React from 'react'
import { Menu, MenuItem, MenuDivider } from '@blueprintjs/core'
import { withTranslation, WithTranslation } from 'react-i18next'
import { useTranslation } from 'react-i18next'

interface HamburgerProps extends WithTranslation {
interface HamburgerProps {
onOpenPrefs: () => void
onOpenShortcuts: () => void
}

export class HamburgerMenuClass extends React.Component<HamburgerProps> {
constructor(props: HamburgerProps) {
super(props)
}
export const HamburgerMenu = ({ onOpenPrefs, onOpenShortcuts }: HamburgerProps) => {
const { t } = useTranslation()

public render(): React.ReactNode {
const { t } = this.props

return (
<React.Fragment>
<Menu className="data-cy-app-menu">
<MenuItem text={t('NAV.PREFS')} icon="cog" onClick={this.props.onOpenPrefs} />
<MenuDivider />
<MenuItem text={t('NAV.SHORTCUTS')} icon="lightbulb" onClick={this.props.onOpenShortcuts} />
</Menu>
</React.Fragment>
)
}
return (
<>
<Menu className="data-cy-app-menu">
<MenuItem text={t('NAV.PREFS')} icon="cog" onClick={onOpenPrefs} />
<MenuDivider />
<MenuItem text={t('NAV.SHORTCUTS')} icon="lightbulb" onClick={onOpenShortcuts} />
</Menu>
</>
)
}

const HamburgerMenu = withTranslation()(HamburgerMenuClass)

export { HamburgerMenu }
38 changes: 38 additions & 0 deletions src/components/__tests__/HamburgerMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @jest-environment jsdom
*/
import React from 'react'
import { screen, render, setup, t } from 'rtl'
import { HamburgerMenu } from '../HamburgerMenu'

describe('Badge', () => {
const PROPS = {
onOpenPrefs: jest.fn(),
onOpenShortcuts: jest.fn(),
}

beforeEach(() => jest.clearAllMocks())

it('should show badge', () => {
render(<HamburgerMenu {...PROPS} />)

expect(screen.getByText(t('NAV.PREFS'))).toBeInTheDocument()
expect(screen.getByText(t('NAV.SHORTCUTS'))).toBeInTheDocument()
})

it('should call onOpenPrefs when clicking on prefs menu item', async () => {
const { user } = setup(<HamburgerMenu {...PROPS} />)

await user.click(screen.getByText(t('NAV.PREFS')))

expect(PROPS.onOpenPrefs).toHaveBeenCalled()
})

it('should call onOpenSHortcuts when clicking on shortcuts menu item', async () => {
const { user } = setup(<HamburgerMenu {...PROPS} />)

await user.click(screen.getByText(t('NAV.SHORTCUTS')))

expect(PROPS.onOpenShortcuts).toHaveBeenCalled()
})
})