Skip to content

Commit 6334614

Browse files
authored
Add disableLegacyContext test gates where needed (#26371)
The www builds include disableLegacyContext as a dynamic flag, so we should be running the tests in that mode, too. Previously we were overriding the flag during the test run. This strategy usually doesn't work because the flags get compiled out in the final build, but we happen to not test www in build mode, only source. To get of this hacky override, I added a test gate to every test that uses legacy context. When we eventually remove legacy context from the codebase, this should make it slightly easier to find which tests are affected. And removes one more hack from our hack-ridden test config. Given that sometimes www has features enabled that aren't on in other builds, we might want to consider testing its build artifacts in CI, rather than just source. That would have forced this cleanup to happen sooner. Currently we only test the public builds in CI.
1 parent 432ffc9 commit 6334614

21 files changed

+326
-245
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ describe('ReactCompositeComponent', () => {
587587
);
588588
});
589589

590+
// @gate !disableLegacyContext
590591
it('should pass context to children when not owner', () => {
591592
class Parent extends React.Component {
592593
render() {
@@ -652,6 +653,7 @@ describe('ReactCompositeComponent', () => {
652653
expect(childRenders).toBe(1);
653654
});
654655

656+
// @gate !disableLegacyContext
655657
it('should pass context when re-rendered for static child', () => {
656658
let parentInstance = null;
657659
let childInstance = null;
@@ -712,6 +714,7 @@ describe('ReactCompositeComponent', () => {
712714
expect(childInstance.context).toEqual({foo: 'bar', flag: true});
713715
});
714716

717+
// @gate !disableLegacyContext
715718
it('should pass context when re-rendered for static child within a composite component', () => {
716719
class Parent extends React.Component {
717720
static childContextTypes = {
@@ -768,6 +771,7 @@ describe('ReactCompositeComponent', () => {
768771
expect(wrapper.childRef.current.context).toEqual({flag: false});
769772
});
770773

774+
// @gate !disableLegacyContext
771775
it('should pass context transitively', () => {
772776
let childInstance = null;
773777
let grandchildInstance = null;
@@ -829,6 +833,7 @@ describe('ReactCompositeComponent', () => {
829833
expect(grandchildInstance.context).toEqual({foo: 'bar', depth: 1});
830834
});
831835

836+
// @gate !disableLegacyContext
832837
it('should pass context when re-rendered', () => {
833838
let parentInstance = null;
834839
let childInstance = null;
@@ -883,6 +888,7 @@ describe('ReactCompositeComponent', () => {
883888
expect(childInstance.context).toEqual({foo: 'bar', depth: 0});
884889
});
885890

891+
// @gate !disableLegacyContext
886892
it('unmasked context propagates through updates', () => {
887893
class Leaf extends React.Component {
888894
static contextTypes = {
@@ -946,6 +952,7 @@ describe('ReactCompositeComponent', () => {
946952
expect(div.children[0].id).toBe('aliens');
947953
});
948954

955+
// @gate !disableLegacyContext
949956
it('should trigger componentWillReceiveProps for context changes', () => {
950957
let contextChanges = 0;
951958
let propChanges = 0;
@@ -1219,6 +1226,7 @@ describe('ReactCompositeComponent', () => {
12191226
expect(a).toBe(b);
12201227
});
12211228

1229+
// @gate !disableLegacyContext || !__DEV__
12221230
it('context should be passed down from the parent', () => {
12231231
class Parent extends React.Component {
12241232
static childContextTypes = {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ describe('ReactDOMFiber', () => {
718718
);
719719
});
720720

721+
// @gate !disableLegacyContext
721722
it('should pass portal context when rendering subtree elsewhere', () => {
722723
const portalContainer = document.createElement('div');
723724

@@ -752,6 +753,7 @@ describe('ReactDOMFiber', () => {
752753
expect(portalContainer.innerHTML).toBe('<div>bar</div>');
753754
});
754755

756+
// @gate !disableLegacyContext
755757
it('should update portal context if it changes due to setState', () => {
756758
const portalContainer = document.createElement('div');
757759

@@ -796,6 +798,7 @@ describe('ReactDOMFiber', () => {
796798
expect(container.innerHTML).toBe('');
797799
});
798800

801+
// @gate !disableLegacyContext
799802
it('should update portal context if it changes due to re-render', () => {
800803
const portalContainer = document.createElement('div');
801804

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,7 @@ describe('ReactDOMFizzServer', () => {
16241624
}
16251625
});
16261626

1627+
// @gate !disableLegacyContext
16271628
it('should can suspend in a class component with legacy context', async () => {
16281629
class TestProvider extends React.Component {
16291630
static childContextTypes = {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ describe('ReactDOMServerIntegration', () => {
4444
});
4545

4646
describe('legacy context', function () {
47+
// The `itRenders` test abstraction doesn't work with @gate so we have
48+
// to do this instead.
49+
if (gate(flags => flags.disableLegacyContext)) {
50+
test('empty test to stop Jest from being a complainy complainer', () => {});
51+
return;
52+
}
53+
4754
let PurpleContext, RedContext;
4855
beforeEach(() => {
4956
class Parent extends React.Component {

packages/react-dom/src/__tests__/ReactErrorBoundaries-test.internal.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ describe('ReactErrorBoundaries', () => {
775775
assertLog(['ErrorBoundary componentWillUnmount']);
776776
});
777777

778+
// @gate !disableLegacyContext || !__DEV__
778779
it('renders an error state if context provider throws in componentWillMount', () => {
779780
class BrokenComponentWillMountWithContext extends React.Component {
780781
static childContextTypes = {foo: PropTypes.number};
@@ -799,45 +800,45 @@ describe('ReactErrorBoundaries', () => {
799800
expect(container.firstChild.textContent).toBe('Caught an error: Hello.');
800801
});
801802

802-
if (!require('shared/ReactFeatureFlags').disableModulePatternComponents) {
803-
it('renders an error state if module-style context provider throws in componentWillMount', () => {
804-
function BrokenComponentWillMountWithContext() {
805-
return {
806-
getChildContext() {
807-
return {foo: 42};
808-
},
809-
render() {
810-
return <div>{this.props.children}</div>;
811-
},
812-
UNSAFE_componentWillMount() {
813-
throw new Error('Hello');
814-
},
815-
};
816-
}
817-
BrokenComponentWillMountWithContext.childContextTypes = {
818-
foo: PropTypes.number,
803+
// @gate !disableModulePatternComponents
804+
// @gate !disableLegacyContext
805+
it('renders an error state if module-style context provider throws in componentWillMount', () => {
806+
function BrokenComponentWillMountWithContext() {
807+
return {
808+
getChildContext() {
809+
return {foo: 42};
810+
},
811+
render() {
812+
return <div>{this.props.children}</div>;
813+
},
814+
UNSAFE_componentWillMount() {
815+
throw new Error('Hello');
816+
},
819817
};
818+
}
819+
BrokenComponentWillMountWithContext.childContextTypes = {
820+
foo: PropTypes.number,
821+
};
820822

821-
const container = document.createElement('div');
822-
expect(() =>
823-
ReactDOM.render(
824-
<ErrorBoundary>
825-
<BrokenComponentWillMountWithContext />
826-
</ErrorBoundary>,
827-
container,
828-
),
829-
).toErrorDev(
830-
'Warning: The <BrokenComponentWillMountWithContext /> component appears to be a function component that ' +
831-
'returns a class instance. ' +
832-
'Change BrokenComponentWillMountWithContext to a class that extends React.Component instead. ' +
833-
"If you can't use a class try assigning the prototype on the function as a workaround. " +
834-
'`BrokenComponentWillMountWithContext.prototype = React.Component.prototype`. ' +
835-
"Don't use an arrow function since it cannot be called with `new` by React.",
836-
);
823+
const container = document.createElement('div');
824+
expect(() =>
825+
ReactDOM.render(
826+
<ErrorBoundary>
827+
<BrokenComponentWillMountWithContext />
828+
</ErrorBoundary>,
829+
container,
830+
),
831+
).toErrorDev(
832+
'Warning: The <BrokenComponentWillMountWithContext /> component appears to be a function component that ' +
833+
'returns a class instance. ' +
834+
'Change BrokenComponentWillMountWithContext to a class that extends React.Component instead. ' +
835+
"If you can't use a class try assigning the prototype on the function as a workaround. " +
836+
'`BrokenComponentWillMountWithContext.prototype = React.Component.prototype`. ' +
837+
"Don't use an arrow function since it cannot be called with `new` by React.",
838+
);
837839

838-
expect(container.firstChild.textContent).toBe('Caught an error: Hello.');
839-
});
840-
}
840+
expect(container.firstChild.textContent).toBe('Caught an error: Hello.');
841+
});
841842

842843
it('mounts the error message if mounting fails', () => {
843844
function renderError(error) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('ReactFunctionComponent', () => {
5959
expect(container.textContent).toBe('');
6060
});
6161

62+
// @gate !disableLegacyContext
6263
it('should pass context thru stateless component', () => {
6364
class Child extends React.Component {
6465
static contextTypes = {
@@ -305,6 +306,7 @@ describe('ReactFunctionComponent', () => {
305306

306307
// This guards against a regression caused by clearing the current debug fiber.
307308
// https://github.com/facebook/react/issues/10831
309+
// @gate !disableLegacyContext || !__DEV__
308310
it('should warn when giving a function ref with context', () => {
309311
function Child() {
310312
return null;
@@ -375,6 +377,7 @@ describe('ReactFunctionComponent', () => {
375377
]);
376378
});
377379

380+
// @gate !disableLegacyContext
378381
it('should receive context', () => {
379382
class Parent extends React.Component {
380383
static childContextTypes = {

packages/react-dom/src/__tests__/ReactLegacyErrorBoundaries-test.internal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ describe('ReactLegacyErrorBoundaries', () => {
800800
expect(log).toEqual(['ErrorBoundary componentWillUnmount']);
801801
});
802802

803+
// @gate !disableLegacyContext || !__DEV__
803804
it('renders an error state if context provider throws in componentWillMount', () => {
804805
class BrokenComponentWillMountWithContext extends React.Component {
805806
static childContextTypes = {foo: PropTypes.number};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ describe('ReactDOMServer', () => {
340340
expect(markup).toContain('hello, world');
341341
});
342342

343+
// @gate !disableLegacyContext
343344
it('renders with context when using custom constructor', () => {
344345
class Component extends React.Component {
345346
constructor() {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const renderSubtreeIntoContainer =
1717
require('react-dom').unstable_renderSubtreeIntoContainer;
1818

1919
describe('renderSubtreeIntoContainer', () => {
20+
// @gate !disableLegacyContext
2021
it('should pass context when rendering subtree elsewhere', () => {
2122
const portal = document.createElement('div');
2223

@@ -99,6 +100,7 @@ describe('renderSubtreeIntoContainer', () => {
99100
}
100101
});
101102

103+
// @gate !disableLegacyContext
102104
it('should update context if it changes due to setState', () => {
103105
const container = document.createElement('div');
104106
document.body.appendChild(container);
@@ -159,6 +161,7 @@ describe('renderSubtreeIntoContainer', () => {
159161
expect(portal.firstChild.innerHTML).toBe('changed-changed');
160162
});
161163

164+
// @gate !disableLegacyContext
162165
it('should update context if it changes due to re-render', () => {
163166
const container = document.createElement('div');
164167
document.body.appendChild(container);
@@ -238,6 +241,7 @@ describe('renderSubtreeIntoContainer', () => {
238241
expect(portal.firstChild.innerHTML).toBe('hello');
239242
});
240243

244+
// @gate !disableLegacyContext
241245
it('should get context through non-context-provider parent', () => {
242246
const container = document.createElement('div');
243247
document.body.appendChild(container);
@@ -281,6 +285,7 @@ describe('renderSubtreeIntoContainer', () => {
281285
expect(portal.textContent).toBe('foo');
282286
});
283287

288+
// @gate !disableLegacyContext
284289
it('should get context through middle non-context-provider layer', () => {
285290
const container = document.createElement('div');
286291
document.body.appendChild(container);

packages/react-native-renderer/src/__tests__/ReactNativeEvents-test.internal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ it('handles events', () => {
197197
]);
198198
});
199199

200+
// @gate !disableLegacyContext || !__DEV__
200201
it('handles events on text nodes', () => {
201202
expect(RCTEventEmitter.register).toHaveBeenCalledTimes(1);
202203
const EventEmitter = RCTEventEmitter.register.mock.calls[0][0];

0 commit comments

Comments
 (0)