@wordpress/element 6.45.1-next.v.202605131032.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +146 -26
- package/build/find-dom-node.cjs +99 -0
- package/build/find-dom-node.cjs.map +7 -0
- package/build/index.cjs +3 -0
- package/build/index.cjs.map +2 -2
- package/build/react-platform.cjs +14 -8
- package/build/react-platform.cjs.map +2 -2
- package/build/react.cjs +10 -0
- package/build/react.cjs.map +2 -2
- package/build/serialize.cjs +3 -1
- package/build/serialize.cjs.map +2 -2
- package/build-module/find-dom-node.mjs +68 -0
- package/build-module/find-dom-node.mjs.map +7 -0
- package/build-module/index.mjs +2 -0
- package/build-module/index.mjs.map +2 -2
- package/build-module/react-platform.mjs +14 -8
- package/build-module/react-platform.mjs.map +2 -2
- package/build-module/react.mjs +10 -0
- package/build-module/react.mjs.map +2 -2
- package/build-module/serialize.mjs +3 -1
- package/build-module/serialize.mjs.map +2 -2
- package/build-types/find-dom-node.d.ts +10 -0
- package/build-types/find-dom-node.d.ts.map +1 -0
- package/build-types/index.d.ts +1 -0
- package/build-types/index.d.ts.map +1 -1
- package/build-types/raw-html.d.ts +25 -11
- package/build-types/raw-html.d.ts.map +1 -1
- package/build-types/react-platform.d.ts +44 -22
- package/build-types/react-platform.d.ts.map +1 -1
- package/build-types/react.d.ts +36 -1
- package/build-types/react.d.ts.map +1 -1
- package/build-types/serialize.d.ts.map +1 -1
- package/package.json +8 -7
- package/src/find-dom-node.ts +92 -0
- package/src/index.ts +1 -0
- package/src/react-platform.ts +54 -28
- package/src/react.ts +48 -1
- package/src/serialize.ts +3 -1
- package/src/test/find-dom-node.js +109 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { act, render } from '@testing-library/react';
|
|
2
|
+
import { Component } from '..';
|
|
3
|
+
import findDOMNodePolyfill from '../find-dom-node';
|
|
4
|
+
|
|
5
|
+
describe( 'findDOMNode', () => {
|
|
6
|
+
it( 'should return null when passed null', () => {
|
|
7
|
+
expect( findDOMNodePolyfill( null ) ).toBeNull();
|
|
8
|
+
expect( console ).toHaveWarned();
|
|
9
|
+
} );
|
|
10
|
+
|
|
11
|
+
it( 'should return the element when passed a DOM element', () => {
|
|
12
|
+
const div = document.createElement( 'div' );
|
|
13
|
+
expect( findDOMNodePolyfill( div ) ).toBe( div );
|
|
14
|
+
} );
|
|
15
|
+
|
|
16
|
+
it( 'should track the current DOM node across re-renders', () => {
|
|
17
|
+
class Toggle extends Component {
|
|
18
|
+
constructor( props ) {
|
|
19
|
+
super( props );
|
|
20
|
+
this.state = { count: 0 };
|
|
21
|
+
this.results = [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
componentDidMount() {
|
|
25
|
+
this.recordNode();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
componentDidUpdate() {
|
|
29
|
+
this.recordNode();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
recordNode() {
|
|
33
|
+
const node = findDOMNodePolyfill( this );
|
|
34
|
+
this.results.push( node.tagName );
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
render() {
|
|
38
|
+
return this.state.count % 2 === 0 ? (
|
|
39
|
+
<div>even</div>
|
|
40
|
+
) : (
|
|
41
|
+
<span>odd</span>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let toggleRef;
|
|
47
|
+
render( <Toggle ref={ ( ref ) => ( toggleRef = ref ) } /> );
|
|
48
|
+
act( () => toggleRef.setState( { count: 1 } ) );
|
|
49
|
+
act( () => toggleRef.setState( { count: 2 } ) );
|
|
50
|
+
act( () => toggleRef.setState( { count: 3 } ) );
|
|
51
|
+
|
|
52
|
+
expect( toggleRef.results ).toEqual( [ 'DIV', 'SPAN', 'DIV', 'SPAN' ] );
|
|
53
|
+
} );
|
|
54
|
+
|
|
55
|
+
it( 'should return null for an unmounted component', () => {
|
|
56
|
+
class MyComponent extends Component {
|
|
57
|
+
render() {
|
|
58
|
+
return <div>hello</div>;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let instanceRef;
|
|
63
|
+
const { rerender } = render(
|
|
64
|
+
<MyComponent
|
|
65
|
+
ref={ ( ref ) => {
|
|
66
|
+
instanceRef = ref;
|
|
67
|
+
return () => {};
|
|
68
|
+
} }
|
|
69
|
+
/>
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
expect( findDOMNodePolyfill( instanceRef ) ).toBeInstanceOf(
|
|
73
|
+
window.HTMLDivElement
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
rerender( <p>replaced</p> );
|
|
77
|
+
|
|
78
|
+
expect( instanceRef ).not.toBeNull();
|
|
79
|
+
expect( findDOMNodePolyfill( instanceRef ) ).toBeNull();
|
|
80
|
+
} );
|
|
81
|
+
|
|
82
|
+
it( 'should find DOM node rendered by a nested child component', () => {
|
|
83
|
+
function Empty() {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function Inner() {
|
|
88
|
+
return <span className="inner">hello</span>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
class Outer extends Component {
|
|
92
|
+
render() {
|
|
93
|
+
return (
|
|
94
|
+
<>
|
|
95
|
+
<Empty />
|
|
96
|
+
<Inner />
|
|
97
|
+
</>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let outerRef;
|
|
103
|
+
render( <Outer ref={ ( ref ) => ( outerRef = ref ) } /> );
|
|
104
|
+
|
|
105
|
+
const node = findDOMNodePolyfill( outerRef );
|
|
106
|
+
expect( node ).toBeInstanceOf( window.HTMLSpanElement );
|
|
107
|
+
expect( node.className ).toBe( 'inner' );
|
|
108
|
+
} );
|
|
109
|
+
} );
|