1+ import { configure , mount } from 'enzyme' ;
2+ import Adapter from 'enzyme-adapter-react-16' ;
13import * as React from 'react' ;
2- import { createRef } from "react" ;
3- import { mount } from 'enzyme' ;
4- import { createCallbackRef , mergeRefs , useTransformRef , useCallbackRef , useMergeRefs } from "../src" ;
5- import { refToCallback , useRefToCallback } from "../src/refToCallback" ;
4+ import { createRef } from 'react' ;
65
6+ import {
7+ refToCallback ,
8+ useRefToCallback ,
9+ createCallbackRef ,
10+ mergeRefs ,
11+ useTransformRef ,
12+ useCallbackRef ,
13+ useMergeRefs ,
14+ } from '../src' ;
15+
16+ configure ( { adapter : new Adapter ( ) } ) ;
717
818describe ( 'Specs' , ( ) => {
919 describe ( 'useCallbackRef' , ( ) => {
@@ -16,7 +26,11 @@ describe('Specs', () => {
1626 it ( 'shall work as createRef' , ( ) => {
1727 const spy = jest . fn ( ) ;
1828 const ref = createCallbackRef < HTMLDivElement > ( spy ) ;
19- mount ( < div ref = { ref } > test</ div > ) . setProps ( { } ) . update ( ) ;
29+
30+ mount ( < div ref = { ref } > test</ div > )
31+ . setProps ( { } )
32+ . update ( ) ;
33+
2034 expect ( spy ) . toBeCalledWith ( ref . current , null ) ;
2135 expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
2236 expect ( ref . current ) . not . toBe ( null ) ;
@@ -26,51 +40,49 @@ describe('Specs', () => {
2640 const spy1 = jest . fn ( ) ;
2741 const spy2 = jest . fn ( ) ;
2842 let counter = 0 ;
43+
2944 const Test = ( ) => {
3045 const x = counter ++ ;
3146 const ref = useCallbackRef < HTMLDivElement > ( null , ( ) => spy1 ( x ) ) ;
3247 const mref = useMergeRefs ( [ ref , spy2 ] ) ;
33- return < div key = { x < 2 ? '1' : '2' } ref = { mref } > test</ div > ;
48+
49+ return (
50+ < div key = { x < 2 ? '1' : '2' } ref = { mref } >
51+ test
52+ </ div >
53+ ) ;
3454 } ;
35- const wrapper = mount ( < Test /> ) ;
55+
56+ const wrapper = mount ( < Test /> ) ;
3657 expect ( spy1 ) . toBeCalledWith ( 0 ) ;
3758 expect ( spy1 ) . toHaveBeenCalledTimes ( 1 ) ;
3859 expect ( spy2 ) . not . toBeCalledWith ( null ) ;
3960 expect ( spy2 ) . toHaveBeenCalledTimes ( 1 ) ;
4061
41- wrapper . setProps ( { x : 42 } ) ;
62+ wrapper . setProps ( { x : 42 } ) ;
4263 expect ( spy1 ) . toBeCalledWith ( 0 ) ;
4364 expect ( spy1 ) . toHaveBeenCalledTimes ( 1 ) ;
4465 expect ( spy2 ) . not . toBeCalledWith ( null ) ;
4566
46- wrapper . setProps ( { x : 24 } ) ;
67+ wrapper . setProps ( { x : 24 } ) ;
4768 expect ( spy1 ) . toBeCalledWith ( 2 ) ;
4869 expect ( spy1 ) . toHaveBeenCalledTimes ( 3 ) ;
4970 expect ( spy2 ) . toBeCalledWith ( null ) ;
5071 } ) ;
5172 } ) ;
5273
5374 describe ( 'mergeRef' , ( ) => {
54- it ( " merges two refs" , ( ) => {
75+ it ( ' merges two refs' , ( ) => {
5576 const spy1 = jest . fn ( ) ;
5677 const ref1 = createCallbackRef < HTMLDivElement > ( spy1 ) ;
5778 const spy2 = jest . fn ( ) ;
5879 const ref2 = createCallbackRef < HTMLDivElement > ( spy2 ) ;
5980 const ref3 = createRef ( ) as React . MutableRefObject < any > ;
6081 const ref4 = jest . fn ( ) ;
6182
62- const TestComponent = ( ) => (
63- < div
64- ref = { mergeRefs ( [
65- ref1 ,
66- ref2 ,
67- ref3 ,
68- ref4 ,
69- ] ) }
70- > test</ div >
71- ) ;
83+ const TestComponent = ( ) => < div ref = { mergeRefs ( [ ref1 , ref2 , ref3 , ref4 ] ) } > test</ div > ;
7284
73- mount ( < TestComponent /> ) ;
85+ mount ( < TestComponent /> ) ;
7486
7587 const ref = ref1 . current ;
7688 expect ( ref ) . not . toBe ( null ) ;
@@ -85,21 +97,28 @@ describe('Specs', () => {
8597 const spy = jest . fn ( ) ;
8698 const ref = createRef < HTMLDivElement > ( ) ;
8799 let counter = 0 ;
100+
88101 const Test = ( ) => {
89102 const x = counter ++ ;
90103 const mref = mergeRefs < HTMLDivElement > ( [ spy , ref ] ) ;
91- return < div key = { x < 1 ? '1' : '2' } ref = { mref } > test</ div > ;
104+
105+ return (
106+ < div key = { x < 1 ? '1' : '2' } ref = { mref } >
107+ test
108+ </ div >
109+ ) ;
92110 } ;
93- const wrapper = mount ( < Test /> ) ;
111+
112+ const wrapper = mount ( < Test /> ) ;
94113 expect ( spy ) . not . toBeCalledWith ( null ) ;
95114 expect ( spy ) . toBeCalledWith ( ref . current ) ;
96115 expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
97116
98- wrapper . setProps ( { x : 42 } ) ;
117+ wrapper . setProps ( { x : 42 } ) ;
99118 expect ( spy ) . toBeCalledWith ( null ) ;
100119 expect ( spy ) . toBeCalledWith ( ref . current ) ;
101120 expect ( spy ) . toHaveBeenCalledTimes ( 3 ) ;
102- } )
121+ } ) ;
103122 } ) ;
104123
105124 describe ( 'transformRef' , ( ) => {
@@ -117,28 +136,21 @@ describe('Specs', () => {
117136 const ref4 = refToCallback < HTMLDivElement > ( spy4 ) ;
118137 const ref4s = useRefToCallback < HTMLDivElement > ( ref4t ) ;
119138
120- return (
121- < div
122- ref = { useMergeRefs ( [
123- ref1 ,
124- ref3 ,
125- ref4 ,
126- ref4s
127- ] ) }
128- > test</ div >
129- ) ;
139+ return < div ref = { useMergeRefs ( [ ref1 , ref3 , ref4 , ref4s ] ) } > test</ div > ;
130140 } ;
131141
132- mount ( < TestComponent /> ) . setProps ( { x : 1 } ) . update ( ) ;
142+ mount ( < TestComponent /> )
143+ . setProps ( { x : 1 } )
144+ . update ( ) ;
133145
134146 const ref = ref1 . current ;
135147 expect ( ref ) . not . toBe ( null ) ;
136148
137149 expect ( spy1 ) . toBeCalledWith ( ref , null ) ;
138- expect ( spy2 ) . toBeCalledWith ( " test" , null ) ;
150+ expect ( spy2 ) . toBeCalledWith ( ' test' , null ) ;
139151 expect ( ref4t . current ) . toBe ( ref ) ;
140152 expect ( spy4 ) . toBeCalledWith ( ref ) ;
141- } )
153+ } ) ;
142154 } ) ;
143155
144156 describe ( 'edge cases' , ( ) => {
@@ -150,13 +162,16 @@ describe('Specs', () => {
150162
151163 it ( 'merging null refs' , ( ) => {
152164 const ref1 = createRef ( ) ;
165+
153166 const TestComponent = ( ) => {
154167 const ref = useMergeRefs ( [ null , ref1 ] ) ;
155168 ref . current = 'xx' ;
169+
156170 return null ;
157171 } ;
158- mount ( < TestComponent /> ) ;
159- expect ( ref1 . current ) . toBe ( 'xx' )
172+
173+ mount ( < TestComponent /> ) ;
174+ expect ( ref1 . current ) . toBe ( 'xx' ) ;
160175 } ) ;
161- } )
176+ } ) ;
162177} ) ;
0 commit comments