@@ -4,6 +4,7 @@ import React, { PropTypes, Component } from 'react';
4
4
import TestUtils from 'react-addons-test-utils' ;
5
5
import { createStore } from 'redux' ;
6
6
import { Provider } from '../../src/index' ;
7
+ import createProvider from '../../src/components/createProvider' ;
7
8
8
9
describe ( 'React' , ( ) => {
9
10
describe ( 'Provider' , ( ) => {
@@ -19,14 +20,152 @@ describe('React', () => {
19
20
}
20
21
}
21
22
23
+ it ( 'should not warn when using child-as-a-function before React 0.14' , ( ) => {
24
+ const store = createStore ( ( ) => ( { } ) ) ;
25
+ [ '0.13.0-beta' , '0.13.0' , '0.13.3' ] . forEach ( version => {
26
+ const LocalProvider = createProvider ( { ...React , version } ) ;
27
+
28
+ let spy = expect . spyOn ( console , 'error' ) ;
29
+ const tree = TestUtils . renderIntoDocument (
30
+ < LocalProvider store = { store } >
31
+ { ( ) => < Child /> }
32
+ </ LocalProvider >
33
+ ) ;
34
+ spy . destroy ( ) ;
35
+ expect ( spy . calls . length ) . toBe ( 0 ) ;
36
+
37
+ spy = expect . spyOn ( console , 'error' ) ;
38
+ tree . forceUpdate ( ) ;
39
+ spy . destroy ( ) ;
40
+ expect ( spy . calls . length ) . toBe ( 0 ) ;
41
+ } ) ;
42
+ } ) ;
43
+
44
+ it ( 'should warn once when using a single element before React 0.14' , ( ) => {
45
+ const store = createStore ( ( ) => ( { } ) ) ;
46
+ [ '0.13.0-beta' , '0.13.0' , '0.13.3' ] . forEach ( version => {
47
+ const LocalProvider = createProvider ( { ...React , version } ) ;
48
+ // Trick React into checking propTypes every time:
49
+ LocalProvider . displayName = Math . random ( ) . toString ( ) ;
50
+
51
+ let spy = expect . spyOn ( console , 'error' ) ;
52
+ const tree = TestUtils . renderIntoDocument (
53
+ < LocalProvider store = { store } >
54
+ < Child />
55
+ </ LocalProvider >
56
+ ) ;
57
+ spy . destroy ( ) ;
58
+
59
+ expect ( spy . calls . length ) . toBe ( 2 ) ;
60
+ expect ( spy . calls [ 0 ] . arguments [ 0 ] ) . toMatch (
61
+ / I n v a l i d p r o p ` c h i l d r e n ` o f t y p e ` o b j e c t ` s u p p l i e d t o .* , e x p e c t e d ` f u n c t i o n ` ./
62
+ ) ;
63
+ expect ( spy . calls [ 1 ] . arguments [ 0 ] ) . toMatch (
64
+ / W i t h R e a c t 0 .1 3 , y o u n e e d t o w r a p < P r o v i d e r > c h i l d i n t o a f u n c t i o n . T h i s r e s t r i c t i o n w i l l b e r e m o v e d w i t h R e a c t 0 .1 4 ./
65
+ ) ;
66
+
67
+ spy = expect . spyOn ( console , 'error' ) ;
68
+ tree . forceUpdate ( ) ;
69
+ spy . destroy ( ) ;
70
+ expect ( spy . calls . length ) . toBe ( 0 ) ;
71
+ } ) ;
72
+ } ) ;
73
+
74
+ it ( 'should warn once when using child-as-a-function after React 0.14' , ( ) => {
75
+ const store = createStore ( ( ) => ( { } ) ) ;
76
+ [ '0.14.0-beta3' , '0.14.0' , '0.14.2' , '0.15.0-beta' , '1.0.0-beta' , '1.0.0' ] . forEach ( version => {
77
+ const LocalProvider = createProvider ( { ...React , version } ) ;
78
+ // Trick React into checking propTypes every time:
79
+ LocalProvider . displayName = Math . random ( ) . toString ( ) ;
80
+
81
+ let spy = expect . spyOn ( console , 'error' ) ;
82
+ const tree = TestUtils . renderIntoDocument (
83
+ < LocalProvider store = { store } >
84
+ { ( ) => < Child /> }
85
+ </ LocalProvider >
86
+ ) ;
87
+ spy . destroy ( ) ;
88
+
89
+ expect ( spy . calls . length ) . toBe ( 2 ) ;
90
+ expect ( spy . calls [ 0 ] . arguments [ 0 ] ) . toMatch (
91
+ / I n v a l i d p r o p ` c h i l d r e n ` s u p p l i e d t o .* , e x p e c t e d a s i n g l e R e a c t E l e m e n t ./
92
+ ) ;
93
+ expect ( spy . calls [ 1 ] . arguments [ 0 ] ) . toMatch (
94
+ / W i t h R e a c t 0 .1 4 a n d l a t e r v e r s i o n s , y o u n o l o n g e r n e e d t o w r a p < P r o v i d e r > c h i l d i n t o a f u n c t i o n ./
95
+ ) ;
96
+
97
+ spy = expect . spyOn ( console , 'error' ) ;
98
+ tree . forceUpdate ( ) ;
99
+ spy . destroy ( ) ;
100
+ expect ( spy . calls . length ) . toBe ( 0 ) ;
101
+ } ) ;
102
+ } ) ;
103
+
104
+ it ( 'should enforce a single child' , ( ) => {
105
+ const store = createStore ( ( ) => ( { } ) ) ;
106
+
107
+ expect ( ( ) => TestUtils . renderIntoDocument (
108
+ < Provider store = { store } >
109
+ < div />
110
+ </ Provider >
111
+ ) ) . toNotThrow ( ) ;
112
+
113
+ expect ( ( ) => TestUtils . renderIntoDocument (
114
+ < Provider store = { store } >
115
+ </ Provider >
116
+ ) ) . toThrow ( / e x a c t l y o n e c h i l d / ) ;
117
+
118
+ expect ( ( ) => TestUtils . renderIntoDocument (
119
+ < Provider store = { store } >
120
+ < div />
121
+ < div />
122
+ </ Provider >
123
+ ) ) . toThrow ( / e x a c t l y o n e c h i l d / ) ;
124
+ } ) ;
125
+
126
+ it ( 'should enforce a single child when using function-as-a-child' , ( ) => {
127
+ const store = createStore ( ( ) => ( { } ) ) ;
128
+
129
+ expect ( ( ) => TestUtils . renderIntoDocument (
130
+ < Provider store = { store } >
131
+ { ( ) => < div /> }
132
+ </ Provider >
133
+ ) ) . toNotThrow ( ) ;
134
+
135
+ expect ( ( ) => TestUtils . renderIntoDocument (
136
+ < Provider store = { store } >
137
+ { ( ) => { } }
138
+ </ Provider >
139
+ ) ) . toThrow ( / e x a c t l y o n e c h i l d / ) ;
140
+ } ) ;
141
+
22
142
it ( 'should add the store to the child context' , ( ) => {
23
143
const store = createStore ( ( ) => ( { } ) ) ;
24
144
145
+ const spy = expect . spyOn ( console , 'error' ) ;
146
+ const tree = TestUtils . renderIntoDocument (
147
+ < Provider store = { store } >
148
+ < Child />
149
+ </ Provider >
150
+ ) ;
151
+ spy . destroy ( ) ;
152
+ expect ( spy . calls . length ) . toBe ( 0 ) ;
153
+
154
+ const child = TestUtils . findRenderedComponentWithType ( tree , Child ) ;
155
+ expect ( child . context . store ) . toBe ( store ) ;
156
+ } ) ;
157
+
158
+ it ( 'should add the store to the child context with function-as-a-child' , ( ) => {
159
+ const store = createStore ( ( ) => ( { } ) ) ;
160
+
161
+ const spy = expect . spyOn ( console , 'error' ) ;
25
162
const tree = TestUtils . renderIntoDocument (
26
163
< Provider store = { store } >
27
164
{ ( ) => < Child /> }
28
165
</ Provider >
29
166
) ;
167
+ spy . destroy ( ) ;
168
+ expect ( spy . calls . length ) . toBe ( 0 ) ;
30
169
31
170
const child = TestUtils . findRenderedComponentWithType ( tree , Child ) ;
32
171
expect ( child . context . store ) . toBe ( store ) ;
@@ -43,7 +182,7 @@ describe('React', () => {
43
182
render ( ) {
44
183
return (
45
184
< Provider store = { this . state . store } >
46
- { ( ) => < Child /> }
185
+ < Child />
47
186
</ Provider >
48
187
) ;
49
188
}
0 commit comments