Skip to content

Commit

Permalink
refactor: move onChange test to separate file
Browse files Browse the repository at this point in the history
onChange test requires import from preact/compat which affects other tests in the same test file so I moved it to separate file
  • Loading branch information
DanielMaczak committed Apr 13, 2024
1 parent 61ac9a7 commit 01e9336
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
27 changes: 27 additions & 0 deletions src/__tests__/events-compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { h } from 'preact' // required by render
import { fireEvent, render } from '..'
import { forwardRef } from 'preact/compat' // required for this test to make sense

test('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(<input type="text" onChange={handler} />)

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))
})
25 changes: 0 additions & 25 deletions src/__tests__/events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createRef, h } from 'preact'
import { fireEvent, render } from '..'
import { forwardRef } from 'preact/compat'

const eventTypes = [
{
Expand Down Expand Up @@ -181,30 +180,6 @@ 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(<input type="text" onChange={handler} />)

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()

Expand Down
1 change: 1 addition & 0 deletions src/fire-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Object.keys(domFireEvent).forEach((key) => {
// making the event name out of sync.
// The problematic code is in: preact/compat/src/render.js > handleDomVNode()
const keyFiltered = key === 'change' ? 'input' : key

return isInElem
? domFireEvent[keyFiltered](elem, init)
: domFireEvent(
Expand Down

0 comments on commit 01e9336

Please sign in to comment.