@@ -36,7 +36,9 @@ class ClickCounter extends React.Component {
3636 < div
3737 className = "clickLogDiv"
3838 key = { 'clickLog' + i }
39- ref = { 'clickLog' + i }
39+ ref = { current => {
40+ this . refs [ 'clickLog' + i ] = current ;
41+ } }
4042 /> ,
4143 ) ;
4244 }
@@ -73,7 +75,11 @@ class TestRefsComponent extends React.Component {
7375 < div >
7476 < div
7577 ref = { current => {
76- this . refs . resetDiv = current ;
78+ if ( current === null ) {
79+ delete this . refs . resetDiv ;
80+ } else {
81+ this . refs . resetDiv = current ;
82+ }
7783 } }
7884 onClick = { this . doReset } >
7985 Reset Me By Clicking This.
@@ -94,15 +100,6 @@ class TestRefsComponent extends React.Component {
94100 }
95101}
96102
97- const expectClickLogsLengthToBe = function ( instance , length ) {
98- const clickLogs = ReactTestUtils . scryRenderedDOMComponentsWithClass (
99- instance ,
100- 'clickLogDiv' ,
101- ) ;
102- expect ( clickLogs . length ) . toBe ( length ) ;
103- expect ( Object . keys ( instance . refs . myCounter . refs ) . length ) . toBe ( length ) ;
104- } ;
105-
106103describe ( 'reactiverefs' , ( ) => {
107104 let container ;
108105
@@ -149,22 +146,47 @@ describe('reactiverefs', () => {
149146 'clickIncrementer' ,
150147 ) ;
151148
152- expectClickLogsLengthToBe ( testRefsComponent , 1 ) ;
149+ let clickLogs = ReactTestUtils . scryRenderedDOMComponentsWithClass (
150+ testRefsComponent ,
151+ 'clickLogDiv' ,
152+ ) ;
153+ expect ( clickLogs . length ) . toBe ( 1 ) ;
154+ expect ( Object . keys ( testRefsComponent . refs . myCounter . refs ) ) . toHaveLength ( 1 ) ;
153155
154156 // After clicking the reset, there should still only be one click log ref.
155157 testRefsComponent . refs . resetDiv . click ( ) ;
156- expectClickLogsLengthToBe ( testRefsComponent , 1 ) ;
158+ clickLogs = ReactTestUtils . scryRenderedDOMComponentsWithClass (
159+ testRefsComponent ,
160+ 'clickLogDiv' ,
161+ ) ;
162+ expect ( clickLogs . length ) . toBe ( 1 ) ;
163+ expect ( Object . keys ( testRefsComponent . refs . myCounter . refs ) ) . toHaveLength ( 1 ) ;
157164
158165 // Begin incrementing clicks (and therefore refs).
159166 clickIncrementer . click ( ) ;
160- expectClickLogsLengthToBe ( testRefsComponent , 2 ) ;
167+ clickLogs = ReactTestUtils . scryRenderedDOMComponentsWithClass (
168+ testRefsComponent ,
169+ 'clickLogDiv' ,
170+ ) ;
171+ expect ( clickLogs . length ) . toBe ( 2 ) ;
172+ expect ( Object . keys ( testRefsComponent . refs . myCounter . refs ) ) . toHaveLength ( 2 ) ;
161173
162174 clickIncrementer . click ( ) ;
163- expectClickLogsLengthToBe ( testRefsComponent , 3 ) ;
175+ clickLogs = ReactTestUtils . scryRenderedDOMComponentsWithClass (
176+ testRefsComponent ,
177+ 'clickLogDiv' ,
178+ ) ;
179+ expect ( clickLogs . length ) . toBe ( 3 ) ;
180+ expect ( Object . keys ( testRefsComponent . refs . myCounter . refs ) ) . toHaveLength ( 3 ) ;
164181
165182 // Now reset again
166183 testRefsComponent . refs . resetDiv . click ( ) ;
167- expectClickLogsLengthToBe ( testRefsComponent , 1 ) ;
184+ clickLogs = ReactTestUtils . scryRenderedDOMComponentsWithClass (
185+ testRefsComponent ,
186+ 'clickLogDiv' ,
187+ ) ;
188+ expect ( clickLogs . length ) . toBe ( 1 ) ;
189+ expect ( Object . keys ( testRefsComponent . refs . myCounter . refs ) ) . toHaveLength ( 3 ) ;
168190 } ) ;
169191} ) ;
170192
@@ -230,15 +252,21 @@ describe('ref swapping', () => {
230252 < div >
231253 < div
232254 className = "first"
233- ref = { count % 3 === 0 ? 'hopRef' : 'divOneRef' }
255+ ref = { current => {
256+ this . refs [ count % 3 === 0 ? 'hopRef' : 'divOneRef' ] = current ;
257+ } }
234258 />
235259 < div
236260 className = "second"
237- ref = { count % 3 === 1 ? 'hopRef' : 'divTwoRef' }
261+ ref = { current => {
262+ this . refs [ count % 3 === 1 ? 'hopRef' : 'divTwoRef' ] = current ;
263+ } }
238264 />
239265 < div
240266 className = "third"
241- ref = { count % 3 === 2 ? 'hopRef' : 'divThreeRef' }
267+ ref = { current => {
268+ this . refs [ count % 3 === 2 ? 'hopRef' : 'divThreeRef' ] = current ;
269+ } }
242270 />
243271 </ div >
244272 ) ;
@@ -482,18 +510,11 @@ describe('root level refs', () => {
482510 } ) ;
483511} ) ;
484512
485- describe ( 'creating element with ref in constructor' , ( ) => {
513+ describe ( 'creating element with string ref in constructor' , ( ) => {
486514 class RefTest extends React . Component {
487515 constructor ( props ) {
488516 super ( props ) ;
489- this . p = (
490- < p
491- ref = { current => {
492- this . refs . p = current ;
493- } } >
494- Hello!
495- </ p >
496- ) ;
517+ this . p = < p ref = "p" > Hello!</ p > ;
497518 }
498519
499520 render ( ) {
0 commit comments