From b901f7ff9fc7b4cc938d8e3003be7b90e094de69 Mon Sep 17 00:00:00 2001 From: Fawad Ali Date: Mon, 26 Sep 2022 22:54:05 +0500 Subject: [PATCH] feat(fireEvent): improve code for definable read-only properties --- src/__tests__/events.js | 2 ++ src/events.js | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/__tests__/events.js b/src/__tests__/events.js index 5ff0118d..b79cea35 100644 --- a/src/__tests__/events.js +++ b/src/__tests__/events.js @@ -383,6 +383,8 @@ test('assigns read-only target properties', () => { expect(node.scrollWidth).toBe(0) fireEvent.scroll(node, {target: {scrollWidth: 10}}) + + expect(spy).toHaveBeenCalledTimes(1) expect(node.scrollWidth).toBe(10) }) diff --git a/src/events.js b/src/events.js index 1fb960dc..aba4f7b4 100644 --- a/src/events.js +++ b/src/events.js @@ -46,14 +46,13 @@ function createEvent( }) } - const modifiableProperties = {} + // Make read-only properties of events specifiable for tests. Object.keys(targetProperties).forEach(key => { - modifiableProperties[key] = { + Object.defineProperty(node, key, { value: targetProperties[key], writable: true, - } + }) }) - Object.defineProperties(node, modifiableProperties) const window = getWindowFromNode(node) const EventConstructor = window[EventType] || window.Event