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

<TooltipIconButton> Tests #179

Merged
merged 8 commits into from
May 3, 2022
24 changes: 13 additions & 11 deletions src/Components/Buttons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ import {assertDefined} from '../utils/assert'
* @param {Object} icon
* @param {string} placement
* @param {string} size
* @param {string} dataTestId Internal attribute for component testing
* @return {Object} React component
*/
export function TooltipIconButton({
export const TooltipIconButton = ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we were using more modern syntax but there was something about function that worked better. I forget what. Is there a reason to prefer const here besides shorter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest reverting this to function as it's more consistent with the rest of our code. I think the rule we're using is "top-level exported functions should use function keyword". I'll add that to styleguide.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

title,
onClick,
icon,
placement='right',
size='medium',
}) {
placement = 'right',
size = 'medium',
'data-testid': dataTestId,
}) => {
assertDefined(title, icon, onClick)
const classes = useStyles(useTheme())

return (
<span className={classes.root}>
<Tooltip title={title} describeChild placement={placement}>
<IconButton onClick={onClick} size={size}>
{icon}
</IconButton>
</Tooltip>
</span>
<Tooltip classes={{tooltip: classes.root}} title={title} describeChild placement={placement}
data-testid={dataTestId}>
<IconButton onClick={onClick} size={size}>
{icon}
</IconButton>
</Tooltip>
)
}

Expand Down
31 changes: 31 additions & 0 deletions src/Components/Buttons.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react'
import {fireEvent, render} from '@testing-library/react'
import {TooltipIconButton} from './Buttons'
import QuestionIcon from '../assets/2D_Icons/Question.svg'
import {MockComponent} from '../__mocks__/MockComponent'


describe('<TooltipIconButton />', () => {
pablo-mayrgundter marked this conversation as resolved.
Show resolved Hide resolved
test('should throw error if missing required props', () => {
expect(() => render(<MockComponent>
<TooltipIconButton/>
</MockComponent>)).toThrow('Arg 0 is not defined')
})

test('should render successfully', async () => {
const rendered = render(<MockComponent>
<TooltipIconButton
data-testid={'test-button'}
title={'Hello. Is it me you\'re looking for?'}
onClick={() => {}}
icon={<QuestionIcon/>}
/>
</MockComponent>)

const button = rendered.getByTestId('test-button')
fireEvent.mouseOver(button)

const tooltip = await rendered.findByRole('tooltip')
expect(tooltip).toBeInTheDocument()
})
})
24 changes: 24 additions & 0 deletions src/__mocks__/MockComponent.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import useTheme from '../Theme'
import {ThemeProvider} from '@mui/material/styles'
import React, {createContext} from 'react'


/**
* @param {Object} children React component(s)
* @return {Object} React component
*/
export const MockComponent = ({children}) => {
pablo-mayrgundter marked this conversation as resolved.
Show resolved Hide resolved
const {theme, colorMode} = useTheme()

return (
<ColorModeContext.Provider value={colorMode}>
<ThemeProvider theme={theme}>
{children}
</ThemeProvider>
</ColorModeContext.Provider>
)
}

const ColorModeContext = createContext({
toggleColorMode: () => {},
})