Skip to content

Commit

Permalink
Fix warnings during tests (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck committed Jun 23, 2023
1 parent bcfd219 commit c24ddea
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
8 changes: 8 additions & 0 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('Auth0Provider', () => {
});

it('should support redirectUri', async () => {
const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
const opts = {
clientId: 'foo',
domain: 'bar',
Expand All @@ -78,10 +79,12 @@ describe('Auth0Provider', () => {
},
})
);
expect(warn).toHaveBeenCalled();
await waitForNextUpdate();
});

it('should support authorizationParams.redirectUri', async () => {
const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
const opts = {
clientId: 'foo',
domain: 'bar',
Expand All @@ -102,6 +105,7 @@ describe('Auth0Provider', () => {
},
})
);
expect(warn).toHaveBeenCalled();
await waitForNextUpdate();
});

Expand Down Expand Up @@ -347,6 +351,7 @@ describe('Auth0Provider', () => {
});

it('should provide a login method supporting redirectUri', async () => {
const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
Expand All @@ -362,9 +367,11 @@ describe('Auth0Provider', () => {
redirect_uri: '__redirect_uri__',
},
});
expect(warn).toHaveBeenCalled();
});

it('should provide a login method supporting authorizationParams.redirectUri', async () => {
const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
Expand All @@ -382,6 +389,7 @@ describe('Auth0Provider', () => {
redirect_uri: '__redirect_uri__',
},
});
expect(warn).toHaveBeenCalled();
});

it('should provide a logout method', async () => {
Expand Down
18 changes: 18 additions & 0 deletions __tests__/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ export const createWrapper = ({
);
};
};

export interface Defer<TData> {
resolve: (value: TData | PromiseLike<TData>) => void;
reject: (reason?: unknown) => void;
promise: Promise<TData>;
}

export function defer<TData>() {
const deferred: Defer<TData> = {} as unknown as Defer<TData>;

const promise = new Promise<TData>(function (resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});

deferred.promise = promise;
return deferred;
}
66 changes: 45 additions & 21 deletions __tests__/with-authentication-required.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import '@testing-library/jest-dom/extend-expect';
import React from 'react';
import withAuthenticationRequired from '../src/with-authentication-required';
import { render, screen, waitFor, act } from '@testing-library/react';
import { Auth0Client} from '@auth0/auth0-spa-js';
import { Auth0Client, User } from '@auth0/auth0-spa-js';
import Auth0Provider from '../src/auth0-provider';
import { Auth0ContextInterface, initialContext } from '../src/auth0-context';
import { defer } from './helpers';

const mockClient = jest.mocked(new Auth0Client({ clientId: '', domain: '' }));

Expand All @@ -28,11 +29,14 @@ describe('withAuthenticationRequired', () => {
mockClient.getUser.mockResolvedValue({ name: '__test_user__' });
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent);
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
await act(() => {
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
});

await waitFor(() =>
expect(mockClient.loginWithRedirect).not.toHaveBeenCalled()
);
Expand All @@ -41,25 +45,33 @@ describe('withAuthenticationRequired', () => {
);
});

it('should show a custom redirecting message', async () => {
mockClient.getUser.mockResolvedValue(
Promise.resolve({ name: '__test_user__' })
);
it('should show a custom redirecting message when not authenticated', async () => {
const deferred = defer<User | undefined>();
mockClient.getUser.mockResolvedValue(deferred.promise);

const MyComponent = (): JSX.Element => <>Private</>;
const OnRedirecting = (): JSX.Element => <>Redirecting</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
onRedirecting: OnRedirecting,
});
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
const { rerender } = await act(() =>
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
)
);

await waitFor(() =>
expect(screen.getByText('Redirecting')).toBeInTheDocument()
);
await waitFor(() =>
expect(mockClient.loginWithRedirect).not.toHaveBeenCalled()

deferred.resolve({ name: '__test_user__' });

rerender(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
await waitFor(() =>
expect(screen.queryByText('Redirecting')).not.toBeInTheDocument()
Expand All @@ -69,9 +81,15 @@ describe('withAuthenticationRequired', () => {
it('should call onBeforeAuthentication before loginWithRedirect', async () => {
const callOrder: string[] = [];
mockClient.getUser.mockResolvedValue(undefined);
mockClient.loginWithRedirect.mockImplementationOnce(async ()=>{ callOrder.push('loginWithRedirect') });
mockClient.loginWithRedirect.mockImplementationOnce(async () => {
callOrder.push('loginWithRedirect');
});
const MyComponent = (): JSX.Element => <>Private</>;
const OnBeforeAuthentication = jest.fn().mockImplementationOnce(async ()=>{ callOrder.push('onBeforeAuthentication') });
const OnBeforeAuthentication = jest
.fn()
.mockImplementationOnce(async () => {
callOrder.push('onBeforeAuthentication');
});
const WrappedComponent = withAuthenticationRequired(MyComponent, {
onBeforeAuthentication: OnBeforeAuthentication,
});
Expand All @@ -81,9 +99,15 @@ describe('withAuthenticationRequired', () => {
</Auth0Provider>
);

await waitFor(() => expect(OnBeforeAuthentication).toHaveBeenCalledTimes(1));
await waitFor(() => expect(mockClient.loginWithRedirect).toHaveBeenCalledTimes(1));
await waitFor(() => expect(callOrder).toEqual(['onBeforeAuthentication', 'loginWithRedirect']));
await waitFor(() =>
expect(OnBeforeAuthentication).toHaveBeenCalledTimes(1)
);
await waitFor(() =>
expect(mockClient.loginWithRedirect).toHaveBeenCalledTimes(1)
);
await waitFor(() =>
expect(callOrder).toEqual(['onBeforeAuthentication', 'loginWithRedirect'])
);
});

it('should pass additional options on to loginWithRedirect', async () => {
Expand Down

0 comments on commit c24ddea

Please sign in to comment.