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

Add a test for rendering time on components #4773

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion packages/react/src/__tests__/ActionMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import React from 'react'
import theme from '../theme'
import {ActionMenu, ActionList, BaseStyles, ThemeProvider, SSRProvider, Tooltip, Button, IconButton} from '..'
import {Tooltip as TooltipV2} from '../TooltipV2/Tooltip'
import {behavesAsComponent, checkExports} from '../utils/testing'
import {behavesAsComponent, checkExports, checkRenderDuration} from '../utils/testing'
import {SingleSelect} from '../ActionMenu/ActionMenu.features.stories'
import {MixedSelection} from '../ActionMenu/ActionMenu.examples.stories'
import {SearchIcon, KebabHorizontalIcon} from '@primer/octicons-react'
@@ -144,6 +144,10 @@ describe('ActionMenu', () => {
ActionMenu,
})

checkRenderDuration(Example, 80)
checkRenderDuration(ExampleWithSubmenus, 38)
checkRenderDuration(ExampleWithTooltip, 66)

it('should open Menu on MenuButton click', async () => {
const component = HTMLRender(<Example />)
const button = component.getByRole('button')
20 changes: 19 additions & 1 deletion packages/react/src/utils/testing.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, {Profiler} from 'react'
import {promisify} from 'util'
import renderer from 'react-test-renderer'
import {render as HTMLRender} from '@testing-library/react'
@@ -185,6 +185,24 @@ export function unloadCSS(path: string) {
}
}

export function checkRenderDuration(Component: React.ComponentType, expectedDuration: number) {
it(`${Component.displayName} renders with expected performance ${expectedDuration}`, () => {
// Add and subtract a small value to account for the variability of the test environment
const minMaxValue = 1
let duration = 0
HTMLRender(
<Profiler
id="ComponentProfiler"
onRender={(id, phase, actualDuration, baseDuration) => (duration = Math.ceil(baseDuration))}
>
<Component />
</Profiler>,
)
expect(duration).toBeLessThanOrEqual(expectedDuration + minMaxValue)
expect(duration).toBeGreaterThanOrEqual(expectedDuration - minMaxValue)
})
}

// If a component requires certain props or other conditions in order
// to render without errors, you can pass a `toRender` function that
// returns an element ready to be rendered.