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

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

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

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

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

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

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

6973
const e = await render(
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
const e = await render(
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
const Child = props => <Grandchild />;
128132

129133
const e = await render(
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
const e = await render(
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', async render => {
161+
function readContext(Ctx, observedBits) {
162+
const dispatcher =
163+
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
164+
.ReactCurrentDispatcher.current;
165+
return dispatcher.readContext(Ctx, observedBits);
166+
}
167+
168+
class Cls extends React.Component {
169+
render() {
170+
return readContext(Context);
171+
}
172+
}
173+
function Fn() {
174+
return readContext(Context);
175+
}
176+
const Memo = React.memo(() => {
177+
return readContext(Context);
178+
});
179+
const FwdRef = React.forwardRef((props, ref) => {
180+
return readContext(Context);
181+
});
182+
183+
const e = await render(
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', async render => {
157200
const Theme = React.createContext('dark');
158201
const Language = React.createContext('french');

0 commit comments

Comments
 (0)