Skip to content

Commit 8bedf88

Browse files
leefreemanxyztusharpandey13
authored andcommitted
refactor: remove explicit return types for functional components in tests and providers
1 parent 44dbcc4 commit 8bedf88

File tree

7 files changed

+31
-26
lines changed

7 files changed

+31
-26
lines changed

__tests__/helpers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const createWrapper = ({
88
}: Partial<Auth0ProviderOptions> = {}) => {
99
return function Wrapper({
1010
children,
11-
}: PropsWithChildren<Record<string, unknown>>): JSX.Element {
11+
}: PropsWithChildren<Record<string, unknown>>): React.JSX.Element {
1212
return (
1313
<Auth0Provider domain={domain} clientId={clientId} {...opts}>
1414
{children}

__tests__/ssr.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('In a Node SSR environment', () => {
1313
ReactDOMServer.renderToString(
1414
<Auth0Provider clientId="__client_id__" domain="__domain__">
1515
<Auth0Context.Consumer>
16-
{(value): JSX.Element => {
16+
{(value): React.JSX.Element => {
1717
({ isLoading, isAuthenticated, user, loginWithRedirect } = value);
1818
return <div>App</div>;
1919
}}

__tests__/with-auth0.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import withAuth0, { WithAuth0Props } from '../src/with-auth0';
77
describe('withAuth0', () => {
88
it('should wrap a class component', () => {
99
class MyComponent extends Component<WithAuth0Props> {
10-
render(): JSX.Element {
10+
render() {
1111
return <>hasAuth: {`${!!this.props.auth0}`}</>;
1212
}
1313
}
@@ -19,7 +19,7 @@ describe('withAuth0', () => {
1919
it('should wrap a class component and provide context', () => {
2020
const context = React.createContext<Auth0ContextInterface>(initialContext);
2121
class MyComponent extends Component<WithAuth0Props> {
22-
render(): JSX.Element {
22+
render() {
2323
return <>hasAuth: {`${!!this.props.auth0}`}</>;
2424
}
2525
}

__tests__/with-authentication-required.test.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const mockClient = jest.mocked(new Auth0Client({ clientId: '', domain: '' }));
1212
describe('withAuthenticationRequired', () => {
1313
it('should block access to a private component when not authenticated', async () => {
1414
mockClient.getUser.mockResolvedValue(undefined);
15-
const MyComponent = (): JSX.Element => <>Private</>;
15+
const MyComponent = () => <>Private</>;
1616
const WrappedComponent = withAuthenticationRequired(MyComponent);
1717
render(
1818
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
@@ -27,7 +27,7 @@ describe('withAuthenticationRequired', () => {
2727

2828
it('should allow access to a private component when authenticated', async () => {
2929
mockClient.getUser.mockResolvedValue({ name: '__test_user__' });
30-
const MyComponent = (): JSX.Element => <>Private</>;
30+
const MyComponent = () => <>Private</>;
3131
const WrappedComponent = withAuthenticationRequired(MyComponent);
3232
await act(() => {
3333
render(
@@ -49,8 +49,8 @@ describe('withAuthenticationRequired', () => {
4949
const deferred = defer<User | undefined>();
5050
mockClient.getUser.mockResolvedValue(deferred.promise);
5151

52-
const MyComponent = (): JSX.Element => <>Private</>;
53-
const OnRedirecting = (): JSX.Element => <>Redirecting</>;
52+
const MyComponent = () => <>Private</>;
53+
const OnRedirecting = () => <>Redirecting</>;
5454
const WrappedComponent = withAuthenticationRequired(MyComponent, {
5555
onRedirecting: OnRedirecting,
5656
});
@@ -84,7 +84,7 @@ describe('withAuthenticationRequired', () => {
8484
mockClient.loginWithRedirect.mockImplementationOnce(async () => {
8585
callOrder.push('loginWithRedirect');
8686
});
87-
const MyComponent = (): JSX.Element => <>Private</>;
87+
const MyComponent = () => <>Private</>;
8888
const OnBeforeAuthentication = jest
8989
.fn()
9090
.mockImplementationOnce(async () => {
@@ -112,7 +112,7 @@ describe('withAuthenticationRequired', () => {
112112

113113
it('should pass additional options on to loginWithRedirect', async () => {
114114
mockClient.getUser.mockResolvedValue(undefined);
115-
const MyComponent = (): JSX.Element => <>Private</>;
115+
const MyComponent = () => <>Private</>;
116116
const WrappedComponent = withAuthenticationRequired(MyComponent, {
117117
loginOptions: {
118118
fragment: 'foo',
@@ -134,7 +134,7 @@ describe('withAuthenticationRequired', () => {
134134

135135
it('should merge additional appState with the returnTo', async () => {
136136
mockClient.getUser.mockResolvedValue(undefined);
137-
const MyComponent = (): JSX.Element => <>Private</>;
137+
const MyComponent = () => <>Private</>;
138138
const WrappedComponent = withAuthenticationRequired(MyComponent, {
139139
loginOptions: {
140140
appState: {
@@ -162,7 +162,7 @@ describe('withAuthenticationRequired', () => {
162162

163163
it('should accept a returnTo function', async () => {
164164
mockClient.getUser.mockResolvedValue(undefined);
165-
const MyComponent = (): JSX.Element => <>Private</>;
165+
const MyComponent = () => <>Private</>;
166166
const WrappedComponent = withAuthenticationRequired(MyComponent, {
167167
returnTo: () => '/foo',
168168
});
@@ -184,9 +184,9 @@ describe('withAuthenticationRequired', () => {
184184

185185
it('should call loginWithRedirect only once even if parent state changes', async () => {
186186
mockClient.getUser.mockResolvedValue(undefined);
187-
const MyComponent = (): JSX.Element => <>Private</>;
187+
const MyComponent = () => <>Private</>;
188188
const WrappedComponent = withAuthenticationRequired(MyComponent);
189-
const App = ({ foo }: { foo: number }): JSX.Element => (
189+
const App = ({ foo }: { foo: number }) => (
190190
<div>
191191
{foo}
192192
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
@@ -210,7 +210,7 @@ describe('withAuthenticationRequired', () => {
210210
mockClient.getUser.mockResolvedValueOnce({ name: '__test_user__' });
211211
mockClient.getUser.mockResolvedValueOnce(undefined);
212212
const context = React.createContext<Auth0ContextInterface>(initialContext);
213-
const MyComponent = (): JSX.Element => <>Private</>;
213+
const MyComponent = () => <>Private</>;
214214
const WrappedComponent = withAuthenticationRequired(MyComponent, {
215215
context,
216216
});
@@ -241,7 +241,7 @@ describe('withAuthenticationRequired', () => {
241241
mockClient.getUser.mockResolvedValueOnce(undefined);
242242
mockClient.getUser.mockResolvedValueOnce({ name: '__test_user__' });
243243
const context = React.createContext<Auth0ContextInterface>(initialContext);
244-
const MyComponent = (): JSX.Element => <>Private</>;
244+
const MyComponent = () => <>Private</>;
245245
const WrappedComponent = withAuthenticationRequired(MyComponent, {
246246
context,
247247
});

src/auth0-provider.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const defaultOnRedirectCallback = (appState?: AppState): void => {
132132
*
133133
* Provides the Auth0Context to its child components.
134134
*/
135-
const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
135+
const Auth0Provider = (opts: Auth0ProviderOptions) => {
136136
const {
137137
children,
138138
skipRedirectCallback,
@@ -146,6 +146,11 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
146146
const [state, dispatch] = useReducer(reducer, initialAuthState);
147147
const didInitialise = useRef(false);
148148

149+
const handleError = useCallback((error: Error) => {
150+
dispatch({ type: 'ERROR', error });
151+
return error;
152+
}, []);
153+
149154
useEffect(() => {
150155
if (didInitialise.current) {
151156
return;
@@ -164,10 +169,10 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
164169
}
165170
dispatch({ type: 'INITIALISED', user });
166171
} catch (error) {
167-
dispatch({ type: 'ERROR', error: loginError(error) });
172+
handleError(loginError(error));
168173
}
169174
})();
170-
}, [client, onRedirectCallback, skipRedirectCallback]);
175+
}, [client, onRedirectCallback, skipRedirectCallback, handleError]);
171176

172177
const loginWithRedirect = useCallback(
173178
(opts?: RedirectLoginOptions): Promise<void> => {
@@ -187,7 +192,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
187192
try {
188193
await client.loginWithPopup(options, config);
189194
} catch (error) {
190-
dispatch({ type: 'ERROR', error: loginError(error) });
195+
handleError(loginError(error));
191196
return;
192197
}
193198
const user = await client.getUser();

src/with-auth0.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const withAuth0 = <P extends WithAuth0Props>(
3030
Component: ComponentType<P>,
3131
context = Auth0Context
3232
): ComponentType<Omit<P, keyof WithAuth0Props>> => {
33-
return function WithAuth(props): JSX.Element {
33+
return function WithAuth(props): React.JSX.Element {
3434
return (
3535
<context.Consumer>
36-
{(auth: Auth0ContextInterface): JSX.Element => (
36+
{(auth: Auth0ContextInterface): React.JSX.Element => (
3737
<Component {...(props as P)} auth0={auth} />
3838
)}
3939
</context.Consumer>

src/with-authentication-required.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import Auth0Context, {
88
/**
99
* @ignore
1010
*/
11-
const defaultOnRedirecting = (): JSX.Element => <></>;
11+
const defaultOnRedirecting = (): React.JSX.Element => <></>;
1212

1313
/**
1414
* @ignore
1515
*/
16-
const defaultOnBeforeAuthentication = async (): Promise<void> => {/* noop */};
16+
const defaultOnBeforeAuthentication = async (): Promise<void> => {/* noop */ };
1717

1818
/**
1919
* @ignore
@@ -52,7 +52,7 @@ export interface WithAuthenticationRequiredOptions {
5252
*
5353
* Render a message to show that the user is being redirected to the login.
5454
*/
55-
onRedirecting?: () => JSX.Element;
55+
onRedirecting?: () => React.JSX.Element;
5656
/**
5757
* ```js
5858
* withAuthenticationRequired(Profile, {
@@ -98,7 +98,7 @@ const withAuthenticationRequired = <P extends object>(
9898
Component: ComponentType<P>,
9999
options: WithAuthenticationRequiredOptions = {}
100100
): FC<P> => {
101-
return function WithAuthenticationRequired(props: P): JSX.Element {
101+
return function WithAuthenticationRequired(props: P): React.JSX.Element {
102102
const {
103103
returnTo = defaultReturnTo,
104104
onRedirecting = defaultOnRedirecting,

0 commit comments

Comments
 (0)