testing-library / testing-library/testing-library-docs
Documentation lacks information on createEvent()
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 479
- Forks
- 740
- PR merge metrics
- No merged PRs in 30d
Description
@testing-library/domversion: latest- Testing Framework and version: jest 26.0.0
- DOM Environment: jsdom latest
Relevant code or config:
import * as React from 'react'
import {render, screen, createEvent, fireEvent, act} from '@testing-library/react'
import {App} from '../index'
test('renders true when event has been fired', async () => {
render(<App />)
await act(async () => {
const event = createEvent('beforeinstallprompt', window, {
userChoice: new Promise((res) =>
res({outcome: 'accepted', platform: ''}),
),
prompt: () => new Promise((res) => res(undefined)),
})
fireEvent(window, event)
})
expect(screen.queryByText(/false/i)).not.toBeInTheDocument()
})
export function App() {
const [eventHeard, setEventHeard] = useState(false)
useEffect(() => {
const evt = (e) => {
e.preventDefault()
setEventHeard(true)
}
window.addEventListener('beforeinstallprompt', evt)
return () => window.removeEventListener('beforeinstallprompt', evt)
})
return <h1>{JSON.stringify(eventHeard)}</h1>
}
What you did:
I was trying to mock the beforeinstallprompt event using createEvent(). The documentation wasn't very clear as to how I create events, so I took a look at the source and it still wasn't clear to me.
What happened:
Reproduction:
Problem description:
Suggested solution:
Update the documentation with examples of exactly how to create events.
Interim solution:
use Object.defineProperties and assign the properties that you need to the object.
const event = createEvent('beforeinstallprompt', window);
Object.defineProperties(event, {
userChoice: {
value: new Promise((res) =>
res({ outcome: 'accepted', platform: '' })
),
},
prompt: { value: () => new Promise((res) => res(undefined)) },
});
fireEvent(window, event);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the createEvent() documentation and the supplied beforeinstallprompt examples, including the Object.defineProperties interim solution. Update the documentation with clear, concrete event-creation examples and verify that the example covers custom event properties and dispatch with fireEvent().
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- documentation, testing
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100