Skip to content

Commit

Permalink
chore: Update layouts/SidebarLayout to Vitest (#3216)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov authored Sep 20, 2024
1 parent 1ff723b commit e82ef28
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,82 @@ import { MemoryRouter } from 'react-router-dom'

import SidebarLayout from './SidebarLayout'

jest.mock('../shared/ErrorBoundary', () => ({ children }) => <>{children}</>)
jest.mock('layouts/Footer', () => () => 'Footer')
vi.mock('../shared/ErrorBoundary', () => ({
default: ({ children }: { children: React.ReactNode }) => <>{children}</>,
}))
vi.mock('layouts/Footer', () => ({ default: () => 'Footer' }))

const robinQuote = 'Holy Tintinnabulation!'
const batmanQuote =
'Why do we fall? So that we can learn to pick ourselves back up.'

describe('SidebarLayout', () => {
function setup(content, overrideClass) {
render(
<SidebarLayout
className={overrideClass}
sidebar={<div>{robinQuote}</div>}
>
{content}
</SidebarLayout>,
{
wrapper: MemoryRouter,
}
)
}

describe('it renders with no children', () => {
beforeEach(() => {
setup()
})

it('renders the sidebar', () => {
render(
<SidebarLayout sidebar={<div>{robinQuote}</div>}></SidebarLayout>,
{ wrapper: MemoryRouter }
)

const sidebar = screen.getByText(robinQuote)
expect(sidebar).toBeInTheDocument()
})
})

describe('it renders with children', () => {
beforeEach(() => {
setup(<p>{batmanQuote}</p>)
})

it('renders the sidebar', () => {
render(
<SidebarLayout sidebar={<div>{robinQuote}</div>}>
<p>{batmanQuote}</p>
</SidebarLayout>,
{ wrapper: MemoryRouter }
)

const sidebar = screen.getByText(robinQuote)
expect(sidebar).toBeInTheDocument()
})

it('renders the content of the page (children)', () => {
render(
<SidebarLayout sidebar={<div>{robinQuote}</div>}>
<p>{batmanQuote}</p>
</SidebarLayout>,
{ wrapper: MemoryRouter }
)

const content = screen.getByText(batmanQuote)
expect(content).toBeInTheDocument()
})
})

describe('it renders the content with default styles', () => {
beforeEach(() => {
setup(<p>{batmanQuote}</p>)
})

it('renders the sidebar', () => {
render(
<SidebarLayout sidebar={<div>{robinQuote}</div>}>
<p>{batmanQuote}</p>
</SidebarLayout>,
{ wrapper: MemoryRouter }
)

const content = screen.getByTestId('sidebar-content')
expect(content).toHaveClass('pl-0 lg:pl-8')
})
})

describe('it renders the content with custom styles', () => {
beforeEach(() => {
setup(<p>{batmanQuote}</p>, 'batcave')
})

it('renders the sidebar', () => {
render(
<SidebarLayout
className="text-red-500"
sidebar={<div>{robinQuote}</div>}
>
<p>{batmanQuote}</p>
</SidebarLayout>,
{ wrapper: MemoryRouter }
)

const content = screen.getByTestId('sidebar-content')
expect(content).toHaveClass('batcave')
expect(content).toHaveClass('text-red-500')
})
})
})
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import cs from 'classnames'
import PropType from 'prop-types'

import ErrorBoundary from '../shared/ErrorBoundary'
import NetworkErrorBoundary from '../shared/NetworkErrorBoundary'

function SidebarLayout({ sidebar, children, className = '' }) {
interface SidebarLayoutProps {
sidebar: React.ReactNode
className?: string
}

const SidebarLayout: React.FC<React.PropsWithChildren<SidebarLayoutProps>> = ({
sidebar,
children,
className = '',
}) => {
return (
<div className="container flex flex-col lg:flex-row">
<ErrorBoundary sentryScopes={[['layout', 'sidebar']]}>
Expand All @@ -22,8 +30,4 @@ function SidebarLayout({ sidebar, children, className = '' }) {
)
}

SidebarLayout.propTypes = {
sidebar: PropType.element.isRequired,
}

export default SidebarLayout
File renamed without changes.

0 comments on commit e82ef28

Please sign in to comment.