testing-library-docs icon indicating copy to clipboard operation
testing-library-docs copied to clipboard

Documentation lacks information on createEvent()

Open jamiehaywood opened this issue 4 years ago • 3 comments

  • @testing-library/dom version: 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);

jamiehaywood avatar Dec 24 '20 12:12 jamiehaywood

Any movement on this issue? I'm not sure whether this is an issue / I just don't know how to use fireEvent. @kentcdodds / @eps1lon ?

jamiehaywood avatar Apr 14 '21 21:04 jamiehaywood

Hi there @jamiehaywood,

Honestly, your solution looks about as good as one might expect I think. It's a bit complicated so maybe you could package it up so other's could use it. Some docs on how to create events would be a good idea as well. I'm sure a PR would be welcome for that.

kentcdodds avatar Apr 14 '21 22:04 kentcdodds

createEvent is a convenience wrapper around how you'd create native events. It looks like you're trying to stub the created event which you wouldn't be able to do in a browser anyway.

Though the docs are currently misleading since they assume jsdom (timeStamp is readonly). There's a lot of confusion how to test events so this might be worth a guide.

eps1lon avatar Apr 15 '21 07:04 eps1lon