Skip to content

Commit

Permalink
test(FloatingBox): Reuse common setup
Browse files Browse the repository at this point in the history
Setup functions can be safely abstracted and reused across tests.
  • Loading branch information
thyhjwb6 committed Dec 20, 2024
1 parent a093355 commit 00fe246
Showing 1 changed file with 30 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,42 @@ import { render, screen, waitFor } from '@testing-library/react'
import { FloatingBox } from '.'
import { useStatefulRef } from '../../hooks/useStatefulRef'

it('renders the floating box when provided with a `renderTarget` and arbitrary JSX content', () => {
function setupOffsetHeightMock(offsetHeight: number) {
jest.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
top: 0,
left: 0,
right: 100,
bottom: 100,
width: 100,
height: 100,
x: 0,
y: 0,
toJSON() {
return this
},
})

Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
value: offsetHeight,
})
}

function setup() {
render(
<FloatingBox
isVisible
placement="bottom"
renderTarget={<div>Hello, World!</div>}
role="dialog"
aria-modal
>
<pre>This is some arbitrary JSX</pre>
</FloatingBox>
)
}

it('renders the floating box when provided with a `renderTarget` and arbitrary JSX content', () => {
setup()

expect(screen.getByTestId('floating-box')).toHaveAttribute('role', 'dialog')
expect(screen.getByTestId('floating-box')).toHaveAttribute('aria-modal')
Expand Down Expand Up @@ -81,34 +106,8 @@ it('does not generate a new content `id` when the content changes', () => {
it('sets the position of the floating box', async () => {
const offsetHeight = 100

jest.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
top: 0,
left: 0,
right: 100,
bottom: 100,
width: 100,
height: 100,
x: 0,
y: 0,
toJSON() {
return this
},
})

Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
value: offsetHeight,
})

render(
<FloatingBox
isVisible
renderTarget={<div>Hello, World!</div>}
placement="bottom"
aria-modal
>
<pre>This is some arbitrary JSX</pre>
</FloatingBox>
)
setupOffsetHeightMock(offsetHeight)
setup()

await waitFor(() => {
expect(screen.getByTestId('floating-box')).toHaveStyle({
Expand All @@ -120,35 +119,8 @@ it('sets the position of the floating box', async () => {
it('sets the position of the floating box when the ref is null', async () => {
const offsetHeight = 100

jest.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
top: 0,
left: 0,
right: 100,
bottom: 100,
width: 100,
height: 100,
x: 0,
y: 0,
toJSON() {
return this
},
})

Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
value: offsetHeight,
})

render(
<FloatingBox
isVisible
renderTarget={<div>Hello, World!</div>}
placement="bottom"
aria-modal
ref={null}
>
<pre>This is some arbitrary JSX</pre>
</FloatingBox>
)
setupOffsetHeightMock(offsetHeight)
setup()

await waitFor(() => {
expect(screen.getByTestId('floating-box')).toHaveStyle({
Expand Down

0 comments on commit 00fe246

Please sign in to comment.