From 9236b5cd0c15232e8c0748363b46d720da570c05 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Mon, 15 Apr 2024 21:59:17 +0200 Subject: [PATCH] [react] Add tests for `createContext` (#69348) --- types/react/test/tsx.tsx | 25 ++++++++++++++++++++----- types/react/ts5.0/test/tsx.tsx | 24 +++++++++++++++++++----- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/types/react/test/tsx.tsx b/types/react/test/tsx.tsx index d32b209f201f17..08a4004b53a5c8 100644 --- a/types/react/test/tsx.tsx +++ b/types/react/test/tsx.tsx @@ -155,15 +155,30 @@ const FunctionComponentWithoutProps: React.FunctionComponent = props => { ; // React.createContext -const ContextWithRenderProps = React.createContext("defaultValue"); +const Context = React.createContext("defaultValue"); + +// @ts-expect-error Forgot value +; +; + +
+; +// @ts-expect-error Wrong value type +; +// @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 +; +; // unstable APIs should not be part of the typings // @ts-expect-error const ContextUsingUnstableObservedBits = React.createContext(undefined, (previous, next) => 7); // @ts-expect-error - + {(value: unknown) => null} -; +; // Fragments
@@ -427,8 +442,8 @@ class LegacyContextAnnotated extends React.Component { } class NewContext extends React.Component { - static contextType = ContextWithRenderProps; - context: React.ContextType = ""; + static contextType = Context; + context: React.ContextType = ""; render() { // $ExpectType string diff --git a/types/react/ts5.0/test/tsx.tsx b/types/react/ts5.0/test/tsx.tsx index fb3816fa7a2b43..9c65cd1433cdba 100644 --- a/types/react/ts5.0/test/tsx.tsx +++ b/types/react/ts5.0/test/tsx.tsx @@ -155,15 +155,29 @@ const FunctionComponentWithoutProps: React.FunctionComponent = props => { ; // React.createContext -const ContextWithRenderProps = React.createContext("defaultValue"); +const Context = React.createContext("defaultValue"); + +// @ts-expect-error Forgot value +; +; + +
+; +// @ts-expect-error Wrong value type +; +// @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 +; // unstable APIs should not be part of the typings // @ts-expect-error const ContextUsingUnstableObservedBits = React.createContext(undefined, (previous, next) => 7); // @ts-expect-error - + {(value: unknown) => null} -; +; // Fragments
@@ -427,8 +441,8 @@ class LegacyContextAnnotated extends React.Component { } class NewContext extends React.Component { - static contextType = ContextWithRenderProps; - context: React.ContextType = ""; + static contextType = Context; + context: React.ContextType = ""; render() { // $ExpectType string