Skip to content

Commit

Permalink
[react] Add tests for createContext (DefinitelyTyped#69348)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Apr 15, 2024
1 parent 74aefa2 commit 9236b5c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
25 changes: 20 additions & 5 deletions types/react/test/tsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,30 @@ const FunctionComponentWithoutProps: React.FunctionComponent = props => {
<FunctionComponentWithoutProps />;

// React.createContext
const ContextWithRenderProps = React.createContext("defaultValue");
const Context = React.createContext("defaultValue");

// @ts-expect-error Forgot value
<Context.Provider />;
<Context.Provider value="provided" />;
<Context.Provider value="provided">
<div />
</Context.Provider>;
// @ts-expect-error Wrong value type
<Context.Provider value={5} />;
// @ts-expect-error Requires explicit default value.
React.createContext();
const UndefinedContext = React.createContext(undefined);
// @ts-expect-error Forgot value even if it can be undefined
<UndefinedContext.Provider />;
<UndefinedContext.Provider value={undefined} />;

// unstable APIs should not be part of the typings
// @ts-expect-error
const ContextUsingUnstableObservedBits = React.createContext(undefined, (previous, next) => 7);
// @ts-expect-error
<ContextWithRenderProps.Consumer unstable_observedBits={4}>
<Context.Consumer unstable_observedBits={4}>
{(value: unknown) => null}
</ContextWithRenderProps.Consumer>;
</Context.Consumer>;

// Fragments
<div>
Expand Down Expand Up @@ -427,8 +442,8 @@ class LegacyContextAnnotated extends React.Component {
}

class NewContext extends React.Component {
static contextType = ContextWithRenderProps;
context: React.ContextType<typeof ContextWithRenderProps> = "";
static contextType = Context;
context: React.ContextType<typeof Context> = "";

render() {
// $ExpectType string
Expand Down
24 changes: 19 additions & 5 deletions types/react/ts5.0/test/tsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,29 @@ const FunctionComponentWithoutProps: React.FunctionComponent = props => {
<FunctionComponentWithoutProps />;

// React.createContext
const ContextWithRenderProps = React.createContext("defaultValue");
const Context = React.createContext("defaultValue");

// @ts-expect-error Forgot value
<Context.Provider />;
<Context.Provider value="provided" />;
<Context.Provider value="provided">
<div />
</Context.Provider>;
// @ts-expect-error Wrong value type
<Context.Provider value={5} />;
// @ts-expect-error Requires explicit default value.
React.createContext();
const UndefinedContext = React.createContext(undefined);
// @ts-expect-error Forgot value even if it can be undefined
<UndefinedContext.Provider />;

// unstable APIs should not be part of the typings
// @ts-expect-error
const ContextUsingUnstableObservedBits = React.createContext(undefined, (previous, next) => 7);
// @ts-expect-error
<ContextWithRenderProps.Consumer unstable_observedBits={4}>
<Context.Consumer unstable_observedBits={4}>
{(value: unknown) => null}
</ContextWithRenderProps.Consumer>;
</Context.Consumer>;

// Fragments
<div>
Expand Down Expand Up @@ -427,8 +441,8 @@ class LegacyContextAnnotated extends React.Component {
}

class NewContext extends React.Component {
static contextType = ContextWithRenderProps;
context: React.ContextType<typeof ContextWithRenderProps> = "";
static contextType = Context;
context: React.ContextType<typeof Context> = "";

render() {
// $ExpectType string
Expand Down

0 comments on commit 9236b5c

Please sign in to comment.