-
Notifications
You must be signed in to change notification settings - Fork 15
Attach a stable testid to default baseElement #44
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,7 @@ export async function render( | |
| // default to document.body instead of documentElement to avoid output of potentially-large | ||
| // head elements (such as JSS style blocks) in debug output | ||
| baseElement = document.body | ||
| document.body.dataset.testid = 'test-body' | ||
|
||
| } | ||
|
|
||
| if (!container) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import React from 'react' | ||
| import type { JSX } from 'react/jsx-runtime' | ||
|
|
||
| export function ComponentThatChanges({ | ||
| timeout = 1500, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not artificially make our tests slower. Is there a better way to test this? |
||
| }: { | ||
| timeout?: number | ||
| }): JSX.Element { | ||
| const [show, setShow] = React.useState(false) | ||
|
|
||
| React.useEffect(() => { | ||
| const timer = setTimeout(() => setShow(true), timeout) | ||
| return () => clearTimeout(timer) | ||
| }, [timeout]) | ||
|
|
||
| return <div>{show ? 'Hello Vitest!' : 'Loading...'}</div> | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { render } from 'vitest-browser-react' | |
| import { HelloWorld } from './fixtures/HelloWorld' | ||
| import { Counter } from './fixtures/Counter' | ||
| import { SuspendedHelloWorld } from './fixtures/SuspendedHelloWorld' | ||
| import { ComponentThatChanges } from './fixtures/ComponentThatChanges' | ||
|
|
||
| test('renders simple component', async () => { | ||
| const screen = await render(<HelloWorld />) | ||
|
|
@@ -49,3 +50,32 @@ test('waits for suspended boundaries', async ({ onTestFinished }) => { | |
| await result | ||
| expect(page.getByText('Hello Vitest')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('should use default testid as the root selector', async () => { | ||
| const stuff = document.createElement('div') | ||
| stuff.textContent = 'DOM content that might change' | ||
| document.body.appendChild(stuff) | ||
|
|
||
| setTimeout(() => { | ||
| stuff.textContent = 'Changed' | ||
| }, 1000) | ||
|
|
||
| const screen = await render(<div></div>) | ||
|
|
||
| expect(page.elementLocator(screen.baseElement).selector).toEqual( | ||
| 'internal:testid=[data-testid="test-body"s]', | ||
| ) | ||
| }) | ||
|
|
||
| test('Should correctly select an element after dom changes', async () => { | ||
| const stuff = document.createElement('div') | ||
| stuff.textContent = 'DOM content that might change' | ||
| document.body.appendChild(stuff) | ||
| setTimeout(() => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing this code doesn't break the test, that seems weird. With your changes or without |
||
| stuff.textContent = 'Changed' | ||
| }, 1000) | ||
|
|
||
| const screen = await render(<ComponentThatChanges />) | ||
|
|
||
| await expect.element(screen.getByText('Hello Vitest!')).toBeVisible() | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently attached the
testidonly in cases wherebaseElementwas not provided.So if consumers provides a different
baseElementit might fail again.Not sure if the library should always attach it or not.