main
js 117 lines 2.98 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @emails react-core
8 */
9
10 'use strict';
11
12 let React;
13 let ReactDOMClient;
14 let act;
15
16 describe('SyntheticEvent', () => {
17 let container;
18 let root;
19
20 beforeEach(() => {
21 React = require('react');
22 ReactDOMClient = require('react-dom/client');
23 act = require('internal-test-utils').act;
24
25 container = document.createElement('div');
26 root = ReactDOMClient.createRoot(container);
27 document.body.appendChild(container);
28 });
29
30 afterEach(() => {
31 document.body.removeChild(container);
32 container = null;
33 });
34
35 it('should be able to `preventDefault`', async () => {
36 let expectedCount = 0;
37
38 const eventHandler = syntheticEvent => {
39 expect(syntheticEvent.isDefaultPrevented()).toBe(false);
40 syntheticEvent.preventDefault();
41 expect(syntheticEvent.isDefaultPrevented()).toBe(true);
42 expect(syntheticEvent.defaultPrevented).toBe(true);
43
44 expectedCount++;
45 };
46 const nodeRef = React.createRef();
47 await act(async () => {
48 root.render(<div onClick={eventHandler} ref={nodeRef} />);
49 });
50 const node = nodeRef.current;
51
52 const event = document.createEvent('Event');
53 event.initEvent('click', true, true);
54 node.dispatchEvent(event);
55
56 expect(expectedCount).toBe(1);
57 });
58
59 it('should be prevented if nativeEvent is prevented', async () => {
60 let expectedCount = 0;
61
62 const eventHandler = syntheticEvent => {
63 expect(syntheticEvent.isDefaultPrevented()).toBe(true);
64
65 expectedCount++;
66 };
67 const nodeRef = React.createRef();
68 await act(async () => {
69 root.render(<div onClick={eventHandler} ref={nodeRef} />);
70 });
71 const node = nodeRef.current;
72
73 let event;
74 event = document.createEvent('Event');
75 event.initEvent('click', true, true);
76 event.preventDefault();
77 node.dispatchEvent(event);
78
79 event = document.createEvent('Event');
80 event.initEvent('click', true, true);
81 // Emulate IE8
82 Object.defineProperty(event, 'defaultPrevented', {
83 get() {},
84 });
85 Object.defineProperty(event, 'returnValue', {
86 get() {
87 return false;
88 },
89 });
90 node.dispatchEvent(event);
91
92 expect(expectedCount).toBe(2);
93 });
94
95 it('should be able to `stopPropagation`', async () => {
96 let expectedCount = 0;
97
98 const eventHandler = syntheticEvent => {
99 expect(syntheticEvent.isPropagationStopped()).toBe(false);
100 syntheticEvent.stopPropagation();
101 expect(syntheticEvent.isPropagationStopped()).toBe(true);
102
103 expectedCount++;
104 };
105 const nodeRef = React.createRef();
106 await act(async () => {
107 root.render(<div onClick={eventHandler} ref={nodeRef} />);
108 });
109 const node = nodeRef.current;
110
111 const event = document.createEvent('Event');
112 event.initEvent('click', true, true);
113 node.dispatchEvent(event);
114
115 expect(expectedCount).toBe(1);
116 });
117 });