From 61ac9a72cbf75392b68cd5539058659b4aff1b13 Mon Sep 17 00:00:00 2001 From: DanielMaczak Date: Sat, 13 Apr 2024 15:27:39 +0200 Subject: [PATCH 1/4] fix: fireEvent doesnt mirror onChange behavior when using preact/compat this fix aligns this library with testing-library for React where fireEvent.change() works as expected --- src/__tests__/events.js | 35 +++++++++++++++++++++++++++++++++-- src/fire-event.js | 12 ++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/__tests__/events.js b/src/__tests__/events.js index 3cea179..c722ef2 100644 --- a/src/__tests__/events.js +++ b/src/__tests__/events.js @@ -1,5 +1,6 @@ import { createRef, h } from 'preact' import { fireEvent, render } from '..' +import { forwardRef } from 'preact/compat' const eventTypes = [ { @@ -180,6 +181,30 @@ test('onInput works', () => { expect(handler).toHaveBeenCalledWith(expect.objectContaining(otherProperties)) }) +test.only('calling `fireEvent` with `preact/compat` and onChange works too', () => { + const handler = jest.fn() + + // forwardRef needs to be imported from preact/compat for this test to make sense. + // Preact behavior when using onChange is described here: + // https://preactjs.com/guide/v10/differences-to-react#use-oninput-instead-of-onchange + // We want to test if onChange event gets caught with fireEvent.change() + const { + container: { firstChild: input } + } = render() + + const targetProperties = { value: 'a' } + const otherProperties = { isComposing: true } + const init = { + target: targetProperties, + ...otherProperties + } + + expect(fireEvent.change(input, init)).toBe(true) + + expect(handler).toHaveBeenCalledTimes(1) + expect(handler).toHaveBeenCalledWith(expect.objectContaining(otherProperties)) +}) + test('calling `fireEvent` directly works too', () => { const handler = jest.fn() @@ -200,8 +225,14 @@ test('calling `fireEvent` directly works too', () => { }) test('`fireEvent` returns false when prevented', () => { - const { container: { firstChild: button } } = render( - (