Skip to content

Commit

Permalink
feat(fireEvent): improve code for definable read-only properties
Browse files Browse the repository at this point in the history
  • Loading branch information
9inpachi committed Oct 11, 2022
1 parent eb800af commit b901f7f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/__tests__/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
7 changes: 3 additions & 4 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b901f7f

Please sign in to comment.