Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Documentation lacks information on createEvent()

未关闭
#798 3 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
52/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
技术栈
javascript, react

调研方向

首先查看 createEvent() 文档和提供的 beforeinstallprompt 示例,包括使用 Object.defineProperties 的临时解决方案。使用清晰、具体的事件创建示例更新文档,并验证该示例涵盖自定义事件属性以及使用 fireEvent() 进行分发。

由索引模型根据 Issue 内容生成。

描述

  • @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);
主要语言
JavaScript
星标
479
派生
738
平均合并
5 天 1 小时
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

testing-library/testing-library-docs 的其他 Issue

查看 testing-library/testing-library-docs 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。