Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhanlonii committed Dec 19, 2024
1 parent 0ecee36 commit c405242
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 67 deletions.
17 changes: 15 additions & 2 deletions packages/react/src/__tests__/ReactContextValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ describe('ReactContextValidator', () => {
' in ComponentInFooBarContext (at **)',
'Component uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
' in ComponentInFooBarContext (at **)',
(gate(flags => flags.enableOwnerStacks)
? ' in ComponentInFooBarContext (at **)'
: ' in Component (at **)'),
]);
expect(instance.childRef.current.context).toEqual({foo: 'abc'});
});
Expand Down Expand Up @@ -157,7 +159,9 @@ describe('ReactContextValidator', () => {
' in Parent (at **)',
'Component uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
' in Parent (at **)',
(gate(flags => flags.enableOwnerStacks)
? ' in Parent (at **)'
: ' in Component (at **)'),
]);

expect(constructorContext).toEqual({foo: 'abc'});
Expand Down Expand Up @@ -278,12 +282,21 @@ describe('ReactContextValidator', () => {
' in ParentContextProvider (at **)',
'MiddleMissingContext uses the legacy childContextTypes API which will soon be removed. ' +
'Use React.createContext() instead. (https://react.dev/link/legacy-context)\n' +
(gate(flags => flags.enableOwnerStacks)
? ''
: ' in MiddleMissingContext (at **)\n') +
' in ParentContextProvider (at **)',
'MiddleMissingContext.childContextTypes is specified but there is no getChildContext() method on the instance. ' +
'You can either define getChildContext() on MiddleMissingContext or remove childContextTypes from it.\n' +
(gate(flags => flags.enableOwnerStacks)
? ''
: ' in MiddleMissingContext (at **)\n') +
' in ParentContextProvider (at **)',
'ChildContextConsumer uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
(gate(flags => flags.enableOwnerStacks)
? ''
: ' in ChildContextConsumer (at **)\n') +
' in MiddleMissingContext (at **)\n' +
' in ParentContextProvider (at **)',
]);
Expand Down
126 changes: 63 additions & 63 deletions packages/react/src/__tests__/ReactES6Class-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,47 +264,47 @@ describe('ReactES6Class', () => {
runTest(<Foo update={true} />, 'DIV', 'updated');
});

if (!require('shared/ReactFeatureFlags').disableLegacyContext) {
it('renders based on context in the constructor', () => {
class Foo extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {tag: context.tag, className: this.context.className};
}
render() {
const Tag = this.state.tag;
return <Tag className={this.state.className} />;
}
// @gate !disableLegacyContext
it('renders based on context in the constructor', () => {
class Foo extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {tag: context.tag, className: this.context.className};
}
Foo.contextTypes = {
tag: PropTypes.string,
className: PropTypes.string,
};
render() {
const Tag = this.state.tag;
return <Tag className={this.state.className} />;
}
}
Foo.contextTypes = {
tag: PropTypes.string,
className: PropTypes.string,
};

class Outer extends React.Component {
getChildContext() {
return {tag: 'span', className: 'foo'};
}
render() {
return <Foo />;
}
class Outer extends React.Component {
getChildContext() {
return {tag: 'span', className: 'foo'};
}
Outer.childContextTypes = {
tag: PropTypes.string,
className: PropTypes.string,
};
runTest(<Outer />, 'SPAN', 'foo');
render() {
return <Foo />;
}
}
Outer.childContextTypes = {
tag: PropTypes.string,
className: PropTypes.string,
};
runTest(<Outer />, 'SPAN', 'foo');

assertConsoleErrorDev([
'Outer uses the legacy childContextTypes API which will soon be removed. ' +
'Use React.createContext() instead. (https://react.dev/link/legacy-context)\n' +
' in Outer (at **)',
'Foo uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
' in Outer (at **)',
]);
});
}
assertConsoleErrorDev([
'Outer uses the legacy childContextTypes API which will soon be removed. ' +
'Use React.createContext() instead. (https://react.dev/link/legacy-context)\n' +
' in Outer (at **)',
'Foo uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
(gate(flags => flags.enableOwnerStacks) ? '' : ' in Foo (at **)\n') +
' in Outer (at **)',
]);
});

it('renders only once when setting state in componentWillMount', () => {
let renderCount = 0;
Expand Down Expand Up @@ -603,32 +603,32 @@ describe('ReactES6Class', () => {
);
});

if (!require('shared/ReactFeatureFlags').disableLegacyContext) {
it('supports this.context passed via getChildContext', () => {
class Bar extends React.Component {
render() {
return <div className={this.context.bar} />;
}
// @gate !disableLegacyContext
it('supports this.context passed via getChildContext', () => {
class Bar extends React.Component {
render() {
return <div className={this.context.bar} />;
}
Bar.contextTypes = {bar: PropTypes.string};
class Foo extends React.Component {
getChildContext() {
return {bar: 'bar-through-context'};
}
render() {
return <Bar />;
}
}
Bar.contextTypes = {bar: PropTypes.string};
class Foo extends React.Component {
getChildContext() {
return {bar: 'bar-through-context'};
}
Foo.childContextTypes = {bar: PropTypes.string};
runTest(<Foo />, 'DIV', 'bar-through-context');
assertConsoleErrorDev([
'Foo uses the legacy childContextTypes API which will soon be removed. ' +
'Use React.createContext() instead. (https://react.dev/link/legacy-context)\n' +
' in Foo (at **)',
'Bar uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
' in Foo (at **)',
]);
});
}
render() {
return <Bar />;
}
}
Foo.childContextTypes = {bar: PropTypes.string};
runTest(<Foo />, 'DIV', 'bar-through-context');
assertConsoleErrorDev([
'Foo uses the legacy childContextTypes API which will soon be removed. ' +
'Use React.createContext() instead. (https://react.dev/link/legacy-context)\n' +
' in Foo (at **)',
'Bar uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
(gate(flags => flags.enableOwnerStacks) ? '' : ' in Bar (at **)\n') +
' in Foo (at **)',
]);
});
});
10 changes: 8 additions & 2 deletions packages/react/src/__tests__/ReactTypeScriptClass-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,10 @@ describe('ReactTypeScriptClass', function () {
' in ProvideChildContextTypes (at **)',
'StateBasedOnContext uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
' in ProvideChildContextTypes.Object.<anonymous>.ProvideChildContextTypes (at **)',
(ReactFeatureFlags.enableOwnerStacks
? ''
: ' in StateBasedOnContext (at **)\n') +
' in ProvideChildContextTypes (at **)',
]);
});
}
Expand Down Expand Up @@ -721,7 +724,10 @@ describe('ReactTypeScriptClass', function () {
' in ProvideContext (at **)',
'ReadContext uses the legacy contextTypes API which will soon be removed. ' +
'Use React.createContext() with static contextType instead. (https://react.dev/link/legacy-context)\n' +
' in ProvideContext.Object.<anonymous>.ProvideContext (at **)',
(ReactFeatureFlags.enableOwnerStacks
? ''
: ' in ReadContext (at **)\n') +
' in ProvideContext (at **)',
]);
});
}
Expand Down

0 comments on commit c405242

Please sign in to comment.