@@ -8,31 +8,61 @@ const TestComponent = props => {
8
8
return < pre dangerouslySetInnerHTML = { { __html : JSON . stringify ( props ) } } />
9
9
}
10
10
11
- const shallowComponent = observer => {
12
- const ObservedComponent = observer ( TestComponent )
11
+ class ClassTestComponent extends React . Component {
12
+ constructor ( props ) {
13
+ super ( props )
14
+ this . state = {
15
+ count : 0 ,
16
+ }
17
+ }
18
+
19
+ handleClick = ( ) => {
20
+ this . setState ( prev => {
21
+ const next = prev === this . props . maxCount ? prev : prev + 1
22
+ return { count : next }
23
+ } )
24
+ }
25
+
26
+ render ( ) {
27
+ return (
28
+ < div >
29
+ < button onClick = { this . handleClick } > +</ button >
30
+ < span > { `Count: ${ this . state . count } ` } </ span >
31
+ < span > { `Max: ${ this . props . count . max } ` } </ span >
32
+ < span > { `Value: ${ this . props . info . key } ` } </ span >
33
+ </ div >
34
+ )
35
+ }
36
+ }
37
+
38
+ const shallowComponent = ( observer , component ) => {
39
+ const ObservedComponent = observer ( component )
13
40
return Enzyme . shallow ( < ObservedComponent /> )
14
41
}
15
42
16
43
let store = null
17
44
let anotherStore = null
45
+ let countStore = null
18
46
19
47
beforeAll ( ( ) => {
20
48
Enzyme . configure ( { adapter : new Adapter ( ) } )
21
49
store = observable ( { key : "initial value" } )
22
50
anotherStore = observable ( { anotherKey : "another initial value" } )
51
+ countStore = observable ( { max : 10 } )
23
52
} )
24
53
25
54
beforeEach ( ( ) => {
26
55
store . reset ( )
27
56
anotherStore . reset ( )
57
+ countStore . reset ( )
28
58
} )
29
59
30
60
it ( "wraps a component with the multiple observable" , ( ) => {
31
61
const observer = multipleObserver ( [
32
62
{ store, key : "value" } ,
33
63
{ store : anotherStore , key : "anotherValue" } ,
34
64
] )
35
- const component = shallowComponent ( observer )
65
+ const component = shallowComponent ( observer , TestComponent )
36
66
expect ( component . html ( ) ) . toEqual (
37
67
"<pre>" +
38
68
"{\"value\":{\"key\":\"initial value\"}," +
@@ -56,7 +86,7 @@ it("wraps a component with the multiple observable (mapped)", () => {
56
86
{ store, key : "value" , map : state => state . key } ,
57
87
{ store : anotherStore , key : "anotherValue" , map : state => state . anotherKey } ,
58
88
] )
59
- const component = shallowComponent ( observer )
89
+ const component = shallowComponent ( observer , TestComponent )
60
90
expect ( component . html ( ) ) . toEqual (
61
91
"<pre>" +
62
92
"{\"value\":\"initial value\"," +
@@ -74,3 +104,31 @@ it("wraps a component with the multiple observable (mapped)", () => {
74
104
)
75
105
component . unmount ( )
76
106
} )
107
+
108
+ it ( "wraps a class component with the multiple observable" , ( ) => {
109
+ const observer = multipleObserver ( [
110
+ { store, key : "info" } ,
111
+ { store : countStore , key : "count" } ,
112
+ ] )
113
+ const component = shallowComponent ( observer , ClassTestComponent )
114
+ expect ( component . html ( ) ) . toEqual (
115
+ "<div>" +
116
+ "<button>+</button>" +
117
+ "<span>Count: 0</span>" +
118
+ "<span>Max: 10</span>" +
119
+ "<span>Value: initial value</span>" +
120
+ "</div>" ,
121
+ )
122
+
123
+ store . set ( { key : "new value" } )
124
+ countStore . set ( { max : 20 } )
125
+ expect ( component . html ( ) ) . toEqual (
126
+ "<div>" +
127
+ "<button>+</button>" +
128
+ "<span>Count: 0</span>" +
129
+ "<span>Max: 20</span>" +
130
+ "<span>Value: new value</span>" +
131
+ "</div>" ,
132
+ )
133
+ component . unmount ( )
134
+ } )
0 commit comments