Skip to content

Commit fe2ecd2

Browse files
authored
Add test coverage for readContext() on the server (#14649)
* Rename context variables I just spent half an hour debugging why readContext(PurpleContext) doesn't work. * Add test coverage for readContext() on the server
1 parent 8f45a7f commit fe2ecd2

1 file changed

Lines changed: 57 additions & 14 deletions

File tree

‎packages/react-dom/src/__tests__/ReactDOMServerIntegrationNewContext-test.js‎

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ describe('ReactDOMServerIntegration', () => {
3737
});
3838

3939
describe('context',function(){
40-
letPurpleContext,RedContext,Consumer;
40+
letContext,PurpleContextProvider,RedContextProvider,Consumer;
4141
beforeEach(()=>{
42-
letContext=React.createContext('none');
42+
Context=React.createContext('none');
4343

4444
classParentextendsReact.Component{
4545
render(){
@@ -51,8 +51,12 @@ describe('ReactDOMServerIntegration', () => {
5151
}
5252
}
5353
Consumer=Context.Consumer;
54-
PurpleContext=props=><Parenttext="purple">{props.children}</Parent>;
55-
RedContext=props=><Parenttext="red">{props.children}</Parent>;
54+
PurpleContextProvider=props=>(
55+
<Parenttext="purple">{props.children}</Parent>
56+
);
57+
RedContextProvider=props=>(
58+
<Parenttext="red">{props.children}</Parent>
59+
);
5660
});
5761

5862
itRenders('class child with context',asyncrender=>{
@@ -67,9 +71,9 @@ describe('ReactDOMServerIntegration', () => {
6771
}
6872

6973
conste=awaitrender(
70-
<PurpleContext>
74+
<PurpleContextProvider>
7175
<ClassChildWithContext/>
72-
</PurpleContext>,
76+
</PurpleContextProvider>,
7377
);
7478
expect(e.textContent).toBe('purple');
7579
});
@@ -80,9 +84,9 @@ describe('ReactDOMServerIntegration', () => {
8084
}
8185

8286
conste=awaitrender(
83-
<PurpleContext>
87+
<PurpleContextProvider>
8488
<FunctionChildWithContext/>
85-
</PurpleContext>,
89+
</PurpleContextProvider>,
8690
);
8791
expect(e.textContent).toBe('purple');
8892
});
@@ -127,9 +131,9 @@ describe('ReactDOMServerIntegration', () => {
127131
constChild=props=><Grandchild/>;
128132

129133
conste=awaitrender(
130-
<PurpleContext>
134+
<PurpleContextProvider>
131135
<Child/>
132-
</PurpleContext>,
136+
</PurpleContextProvider>,
133137
);
134138
expect(e.textContent).toBe('purple');
135139
});
@@ -144,15 +148,54 @@ describe('ReactDOMServerIntegration', () => {
144148
};
145149

146150
conste=awaitrender(
147-
<PurpleContext>
148-
<RedContext>
151+
<PurpleContextProvider>
152+
<RedContextProvider>
149153
<Grandchild/>
150-
</RedContext>
151-
</PurpleContext>,
154+
</RedContextProvider>
155+
</PurpleContextProvider>,
152156
);
153157
expect(e.textContent).toBe('red');
154158
});
155159

160+
itRenders('readContext() in different components',asyncrender=>{
161+
functionreadContext(Ctx,observedBits){
162+
constdispatcher=
163+
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
164+
.ReactCurrentDispatcher.current;
165+
returndispatcher.readContext(Ctx,observedBits);
166+
}
167+
168+
classClsextendsReact.Component{
169+
render(){
170+
returnreadContext(Context);
171+
}
172+
}
173+
functionFn(){
174+
returnreadContext(Context);
175+
}
176+
constMemo=React.memo(()=>{
177+
returnreadContext(Context);
178+
});
179+
constFwdRef=React.forwardRef((props,ref)=>{
180+
returnreadContext(Context);
181+
});
182+
183+
conste=awaitrender(
184+
<PurpleContextProvider>
185+
<RedContextProvider>
186+
<span>
187+
<Fn/>
188+
<Cls/>
189+
<Memo/>
190+
<FwdRef/>
191+
<Consumer>{()=>readContext(Context)}</Consumer>
192+
</span>
193+
</RedContextProvider>
194+
</PurpleContextProvider>,
195+
);
196+
expect(e.textContent).toBe('redredredredred');
197+
});
198+
156199
itRenders('multiple contexts',asyncrender=>{
157200
constTheme=React.createContext('dark');
158201
constLanguage=React.createContext('french');

0 commit comments

Comments
 (0)