-
Notifications
You must be signed in to change notification settings - Fork 254
Description
Reproduction example
Unit test case in issue
Prerequisites
import { createEvent } from '#src/event/createEvent'
import { render } from '#testHelpers'
test('it does not stringify click pointerType', () => {
const { element } = render(`<input type="checkbox"/>`)
const event = createEvent('click', element)
expect(event).toHaveProperty('pointerType', undefined)
})
Expected behavior
Test passes
Actual behavior
FAIL tests/event/createEvent.ts
● it does not stringify click pointerType
expect(received).toHaveProperty(path, value)
Expected path: "pointerType"
Expected value: undefined
Received value: "undefined"
5 | const { element } = render(`<input type="checkbox"/>`)
6 | const event = createEvent('click', element)
> 7 | expect(event).toHaveProperty('pointerType', undefined)
| ^
8 | })
9 |
at Object.toHaveProperty (tests/event/createEvent.ts:7:19)
User-event version
14.6.1
Environment
Testing Library framework: @testing-library/[email protected]
JS framework: [email protected]
Test environment: [email protected]
DOM implementation: [email protected]
Additional context
Simulating a click does produce an event with an invalid pointerType of "undefined" (as string).
It is expected that pointerType can be undefined, but it should never be that string.
The cause for this is this line of code:
user-event/src/event/createEvent.ts
Line 271 in 63ac399
| pointerType: String(pointerType), |
I tried adding a simple PR that accounts for undefined here, but due to the nature of assignProps, the undefined value will be turned into null, which is also not correct:
user-event/src/event/createEvent.ts
Lines 101 to 105 in 63ac399
| function assignProps<T extends object>(obj: T, props: {[k in keyof T]?: T[k]}) { | |
| for (const [key, value] of Object.entries(props)) { | |
| Object.defineProperty(obj, key, {get: () => value ?? null}) | |
| } | |
| } |
Updating assignProps lead to too many snapshot changes, so I didn't pursue this further, but hope I've provided enough information that allows provide a fix.
Thanks 🙌