Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Documentation lacks information on createEvent()

Đang mở
#798 3 bình luận 3 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 4 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
52/100
Loại issue
Tài liệu
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, react
Lĩnh vực
documentation, testing

Hướng nghiên cứu

Bắt đầu bằng việc xem lại tài liệu về createEvent() và các ví dụ beforeinstallprompt được cung cấp, bao gồm giải pháp tạm thời với Object.defineProperties. Cập nhật tài liệu bằng các ví dụ rõ ràng, cụ thể về việc tạo sự kiện và xác minh rằng ví dụ bao quát các thuộc tính sự kiện tùy chỉnh cũng như việc dispatch bằng fireEvent().

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

  • @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);
Ngôn ngữ chính
JavaScript
Star
479
Fork
738
Merge trung bình
5 ngày 1 giờ
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của testing-library/testing-library-docs

Tất cả issue của testing-library/testing-library-docs

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.