1
1
/* eslint-disable react/prop-types */
2
2
/* eslint-disable react/button-has-type */
3
- import React , { useState } from " react" ;
4
- import { shallow , mount } from " enzyme" ;
5
- import composeHooks from " ../index" ;
3
+ import React , { useState } from ' react' ;
4
+ import { shallow , mount } from ' enzyme' ;
5
+ import composeHooks from ' ../index' ;
6
6
7
7
const INITIAL_COUNT = 0 ;
8
- const INITIAL_VALUE = "hi" ;
8
+ const INITIAL_VALUE = 'hi' ;
9
9
10
10
const useCount = ( ) => {
11
11
const [ count , setCount ] = useState ( INITIAL_COUNT ) ;
@@ -24,70 +24,34 @@ const useUseState = () => useState(INITIAL_COUNT);
24
24
25
25
const TestComponent = ( ) => < div > Test</ div > ;
26
26
27
- test ( "returns component if no hooks" , ( ) => {
28
- const Container = composeHooks ( ) ( TestComponent ) ;
29
- const wrapper = shallow ( < Container /> ) ;
30
- expect ( wrapper . html ( ) ) . toMatchInlineSnapshot ( `"<div>Test</div>"` ) ;
31
- } ) ;
32
-
33
- test ( "throws if no component" , ( ) => {
34
- expect ( ( ) => composeHooks ( ) ( ) ) . toThrowErrorMatchingInlineSnapshot (
35
- `"Component must be provided to compose"`
36
- ) ;
37
- } ) ;
38
-
39
- test ( "passes custom hooks to component" , ( ) => {
27
+ test ( 'passes custom hooks to component' , ( ) => {
40
28
const Container = composeHooks ( { useCount, useChange } ) ( TestComponent ) ;
41
29
const wrapper = shallow ( < Container /> ) ;
42
- const { count, increment, decrement, value, onChange } = wrapper
43
- . find ( TestComponent )
44
- . props ( ) ;
30
+ const { count, increment, decrement, value, onChange } = wrapper . find ( TestComponent ) . props ( ) ;
45
31
expect ( count ) . toBe ( INITIAL_COUNT ) ;
46
32
expect ( value ) . toBe ( INITIAL_VALUE ) ;
47
- expect ( typeof increment ) . toBe ( " function" ) ;
48
- expect ( typeof decrement ) . toBe ( " function" ) ;
49
- expect ( typeof onChange ) . toBe ( " function" ) ;
33
+ expect ( typeof increment ) . toBe ( ' function' ) ;
34
+ expect ( typeof decrement ) . toBe ( ' function' ) ;
35
+ expect ( typeof onChange ) . toBe ( ' function' ) ;
50
36
} ) ;
51
37
52
- test ( " passes props to component" , ( ) => {
38
+ test ( ' passes props to component' , ( ) => {
53
39
const Container = composeHooks ( { useChange } ) ( TestComponent ) ;
54
40
const wrapper = shallow ( < Container foo = "bar" /> ) ;
55
41
const { foo } = wrapper . find ( TestComponent ) . props ( ) ;
56
- expect ( foo ) . toBe ( "bar" ) ;
57
- } ) ;
58
-
59
- test ( "if prop and hook names collide, props win" , ( ) => {
60
- const Container = composeHooks ( { useChange } ) ( TestComponent ) ;
61
- const wrapper = shallow ( < Container /> ) ;
62
- expect ( wrapper . find ( TestComponent ) . props ( ) . value ) . toBe ( "hi" ) ;
63
- wrapper . setProps ( { value : "newValue" } ) ;
64
- expect ( wrapper . find ( TestComponent ) . props ( ) . value ) . toBe ( "newValue" ) ;
65
- } ) ;
66
-
67
- test ( "warns on hook name collisions" , ( ) => {
68
- console . warn = jest . fn ( ) . mockImplementationOnce ( ( ) => { } ) ;
69
- const useChangeTwo = ( ) => ( { value : "duplicate-hook-prop" } ) ;
70
- const Container = composeHooks ( { useChange, useChangeTwo } ) ( TestComponent ) ;
71
- const wrapper = shallow ( < Container /> ) ;
72
- expect ( console . warn . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot (
73
- `"prop 'value' exists, overriding with value: duplicate-hook-prop"`
74
- ) ;
75
- expect ( wrapper . find ( TestComponent ) . props ( ) . value ) . toBe ( "duplicate-hook-prop" ) ;
76
- jest . restoreAllMocks ( ) ;
42
+ expect ( foo ) . toBe ( 'bar' ) ;
77
43
} ) ;
78
44
79
- test ( "hooks work as expected" , ( ) => {
80
- const Component = ( { value, onChange } ) => (
81
- < input value = { value } onChange = { onChange } />
82
- ) ;
45
+ test ( 'hooks work as expected' , ( ) => {
46
+ const Component = ( { value, onChange } ) => < input value = { value } onChange = { onChange } /> ;
83
47
const Container = composeHooks ( { useChange } ) ( Component ) ;
84
48
const wrapper = mount ( < Container /> ) ;
85
- expect ( wrapper . find ( " input" ) . props ( ) . value ) . toBe ( INITIAL_VALUE ) ;
86
- wrapper . find ( " input" ) . simulate ( " change" , { target : { value : " new" } } ) ;
87
- expect ( wrapper . find ( " input" ) . props ( ) . value ) . toBe ( " new" ) ;
49
+ expect ( wrapper . find ( ' input' ) . props ( ) . value ) . toBe ( INITIAL_VALUE ) ;
50
+ wrapper . find ( ' input' ) . simulate ( ' change' , { target : { value : ' new' } } ) ;
51
+ expect ( wrapper . find ( ' input' ) . props ( ) . value ) . toBe ( ' new' ) ;
88
52
} ) ;
89
53
90
- test ( " works with custom hook that returns array" , ( ) => {
54
+ test ( ' works with custom hook that returns array' , ( ) => {
91
55
const Component = ( { simpleHook } ) => {
92
56
const [ count , setCount ] = simpleHook ;
93
57
return < button onClick = { ( ) => setCount ( c => c + 1 ) } > { count } </ button > ;
@@ -96,16 +60,71 @@ test("works with custom hook that returns array", () => {
96
60
const Container = composeHooks ( { simpleHook : useUseState } ) ( Component ) ;
97
61
const wrapper = mount ( < Container /> ) ;
98
62
expect ( wrapper . text ( ) ) . toBe ( INITIAL_COUNT . toString ( ) ) ;
99
- wrapper . find ( " button" ) . simulate ( " click" ) ;
63
+ wrapper . find ( ' button' ) . simulate ( ' click' ) ;
100
64
expect ( wrapper . text ( ) ) . toBe ( ( INITIAL_COUNT + 1 ) . toString ( ) ) ;
101
65
} ) ;
102
66
67
+ test ( 'works with custom hook that returns single value' , ( ) => {
68
+ // Check single function value
69
+ let outerFoo ;
70
+ const useFoo = ( ) => {
71
+ const [ foo , setFoo ] = useState ( 'before' ) ;
72
+ outerFoo = foo ;
73
+ return setFoo ;
74
+ } ;
75
+ // Check single value
76
+ const useBar = ( ) => {
77
+ const [ bar ] = useState ( 'Click me' ) ;
78
+ return bar ;
79
+ } ;
80
+ const Component = ( { setFoo, bar } ) => < button onClick = { ( ) => setFoo ( 'after' ) } > { bar } </ button > ;
81
+ const Container = composeHooks ( { setFoo : useFoo , bar : useBar } ) ( Component ) ;
82
+ const wrapper = mount ( < Container /> ) ;
83
+ expect ( outerFoo ) . toBe ( 'before' ) ;
84
+ wrapper . find ( { children : 'Click me' } ) . simulate ( 'click' ) ;
85
+ expect ( outerFoo ) . toBe ( 'after' ) ;
86
+ } ) ;
87
+
103
88
test ( 'can pass props to hooks via function' , ( ) => {
104
89
const TEST_VALUE = 'test-value' ;
105
90
const Component = ( { value } ) => value ;
106
91
const Container = composeHooks ( props => ( {
107
- useChange : useChange ( props . initialValue )
92
+ useChange : ( ) => useChange ( props . initialValue ) ,
108
93
} ) ) ( Component ) ;
109
94
const wrapper = mount ( < Container initialValue = { TEST_VALUE } /> ) ;
110
95
expect ( wrapper . text ( ) ) . toBe ( TEST_VALUE ) ;
111
96
} ) ;
97
+
98
+ describe ( 'Edge cases' , ( ) => {
99
+ it ( 'returns component if no hooks' , ( ) => {
100
+ const Container = composeHooks ( ) ( TestComponent ) ;
101
+ const wrapper = shallow ( < Container /> ) ;
102
+ expect ( wrapper . html ( ) ) . toMatchInlineSnapshot ( `"<div>Test</div>"` ) ;
103
+ } ) ;
104
+
105
+ it ( 'throws if no component' , ( ) => {
106
+ expect ( ( ) => composeHooks ( ) ( ) ) . toThrowErrorMatchingInlineSnapshot (
107
+ `"Component must be provided to compose"`
108
+ ) ;
109
+ } ) ;
110
+
111
+ it ( 'if prop and hook names collide, props win' , ( ) => {
112
+ const Container = composeHooks ( { useChange } ) ( TestComponent ) ;
113
+ const wrapper = shallow ( < Container /> ) ;
114
+ expect ( wrapper . find ( TestComponent ) . props ( ) . value ) . toBe ( 'hi' ) ;
115
+ wrapper . setProps ( { value : 'newValue' } ) ;
116
+ expect ( wrapper . find ( TestComponent ) . props ( ) . value ) . toBe ( 'newValue' ) ;
117
+ } ) ;
118
+
119
+ it ( 'warns on hook name collisions' , ( ) => {
120
+ console . warn = jest . fn ( ) . mockImplementationOnce ( ( ) => { } ) ;
121
+ const useChangeTwo = ( ) => ( { value : 'duplicate-hook-prop' } ) ;
122
+ const Container = composeHooks ( { useChange, useChangeTwo } ) ( TestComponent ) ;
123
+ const wrapper = shallow ( < Container /> ) ;
124
+ expect ( console . warn . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot (
125
+ `"prop 'value' exists, overriding with value: duplicate-hook-prop"`
126
+ ) ;
127
+ expect ( wrapper . find ( TestComponent ) . props ( ) . value ) . toBe ( 'duplicate-hook-prop' ) ;
128
+ jest . restoreAllMocks ( ) ;
129
+ } ) ;
130
+ } ) ;
0 commit comments