forked from bldrs-ai/Share
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<TooltipIconButton> Tests (bldrs-ai#179)
* Create manual mock for testing React components This wraps the component with the requisite theme provider. * Convert component to ES6 arrow function * Add tests for <TooltipIconButton> * Refactor to remove redundant inline wrapper * Add internal attribute to support testing * Add missing blank lines after imports * Reorder file context so name of first object corresponds with filename
- Loading branch information
1 parent
c8c0be2
commit f5f872e
Showing
3 changed files
with
69 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />', () => { | ||
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) => { | ||
const {theme, colorMode} = useTheme() | ||
|
||
return ( | ||
<ColorModeContext.Provider value={colorMode}> | ||
<ThemeProvider theme={theme}> | ||
{children} | ||
</ThemeProvider> | ||
</ColorModeContext.Provider> | ||
) | ||
} | ||
|
||
const ColorModeContext = createContext({ | ||
toggleColorMode: () => {}, | ||
}) |