Skip to content

Commit

Permalink
fix: existing tests are working again
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarajohn committed Feb 20, 2024
1 parent a9617e6 commit 2b588d0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class CustomError extends Error {
import { ErrorBoundary, useErrorHandler } from 'react-error-boundary';

import {
createAuth0Client as mockedCreateAuth0,
fakeTokenData,
FAKE_TOKEN,
getNewFakeToken,
Expand All @@ -27,11 +26,12 @@ import {
import { defaultAuthenticationContextValues } from '../contexts/authentication';
import { useAuthentication } from '../hooks';
import { Authentication } from '../providers/Authentication';
import { Organizations } from '../providers/Organizations';
import { orfiumIdBaseInstance } from '../request';
import MockRequest from '../request/mock';
import useOrganization from '../store/organizations';
import useRequestToken from '../store/requestToken';
import { getAuth0Client, getTokenSilently, logoutAuth, onRedirectCallback } from '../utils/auth';
import { getTokenSilently, logoutAuth, onRedirectCallback } from '../utils/auth';

const TestingComponentSimple = () => {
const { user, isAuthenticated, isLoading } = useAuthentication();
Expand Down Expand Up @@ -79,8 +79,11 @@ const TestingComponent = () => {
describe('Context', () => {
const apiInstance = orfiumIdBaseInstance.instance;
const mock: MockRequest = new MockRequest(apiInstance);
// let client;

beforeEach(() => {
jest.resetModules();
// client = undefined;
mock.onGet('/products/').reply(200, [
{
name: 'string',
Expand Down Expand Up @@ -111,19 +114,19 @@ describe('Context', () => {
window.history.pushState({}, '', oldWindowLocation.pathname);
});

xtest('onRedirectCallback changes the url based on AppState passed', () => {
test('onRedirectCallback changes the url based on AppState passed', () => {
const targetUrl = 'www.test.com';

onRedirectCallback({ targetUrl });
expect(window.location.pathname).toBe(`/${targetUrl}`);
});

xtest('onRedirectCallback changes url without AppState targetUrl', () => {
test('onRedirectCallback changes url without AppState targetUrl', () => {
onRedirectCallback({});
expect(window.location.pathname).toBe(`/`);
});

xtest('handleRedirectCallback being called if code exists on url', async () => {
test('handleRedirectCallback being called if code exists on url', async () => {
window.history.pushState({}, '', '?code=test');

render(
Expand All @@ -135,7 +138,7 @@ describe('Context', () => {
await waitFor(() => expect(mockedHandleRedirectCallback).toBeCalledTimes(1));
});

xtest('handleRedirectCallback that triggers invalid state error', async () => {
test('handleRedirectCallback that triggers invalid state error', async () => {
window.history.pushState({}, '', '?code=test');

// @ts-ignore
Expand All @@ -156,7 +159,7 @@ describe('Context', () => {
await waitFor(() => expect(loginWithRedirect).toBeCalledTimes(1));
});

xtest('handleRedirectCallback that triggers error', async () => {
test('handleRedirectCallback that triggers error', async () => {
window.history.pushState({}, '', '?code=test');
const errorMsg = 'Invalid';

Expand All @@ -179,7 +182,7 @@ describe('Context', () => {
await waitFor(() => expect(screen.getByTestId('errorboundary').innerHTML).toBe(errorMsg));
}, 10000);

xtest('logoutAuth clears out data', async () => {
test('logoutAuth clears out data', async () => {
const { setToken } = useRequestToken.getState();
const { setOrganizations, setSelectedOrganization } = useOrganization.getState();
const testToken = 'testToken';
Expand Down Expand Up @@ -216,15 +219,15 @@ describe('Context', () => {
});

describe('getTokenSilently', () => {
xtest('without cached results', async () => {
test('without cached results', async () => {
mockedGetTokenSilently.mockResolvedValue(FAKE_TOKEN);
const { token, decodedToken } = await getTokenSilently();

expect(token).toBe(FAKE_TOKEN);
expect(decodedToken).toEqual(jwtDecode(token));
});

xtest('with cached results', async () => {
test('with cached results', async () => {
const NEW_FAKE_EXPIRED_TOKEN = getNewFakeToken();
const setToken = useRequestToken.getState().setToken;
const setOrganizations = useOrganization.getState().setOrganizations;
Expand Down Expand Up @@ -254,7 +257,7 @@ describe('Context', () => {
expect(decodedToken.org_id).toEqual(fakeTokenData.org_id); // the org_id of the token
});

xtest('that throws error and handles it outside exclusion of login_required', async () => {
test('that throws error and handles it outside exclusion of login_required', async () => {
const errorThrown = new CustomError('error', 'error');
mockedGetTokenSilently.mockRejectedValue(errorThrown);

Expand All @@ -266,7 +269,8 @@ describe('Context', () => {
});
});

xtest('Authentication contents', async () => {
test('Authentication contents', async () => {
mockedGetTokenSilently.mockResolvedValue(FAKE_TOKEN);
isAuthenticated.mockResolvedValue(true);
getUser.mockResolvedValue({
name: 'John Doe',
Expand All @@ -283,7 +287,7 @@ describe('Context', () => {
});

describe('Authentication calls loginWithRedirect success/error', () => {
xtest('loginWithRedirect when access token fails', async () => {
test('loginWithRedirect when access token fails', async () => {
const errorMsg = 'login_required';

mockedGetTokenSilently.mockRejectedValue(new CustomError(errorMsg, errorMsg));
Expand All @@ -302,7 +306,7 @@ describe('Context', () => {
await waitFor(() => expect(loginWithRedirect).toBeCalledTimes(1));
});

xtest('loginWithRedirect when access token fails and handle an error', async () => {
test('loginWithRedirect when access token fails and handle an error', async () => {
const errorMsg = 'login_with_popup_failed';

mockedGetTokenSilently.mockRejectedValue(new CustomError('login_required', 'login_required'));
Expand All @@ -328,7 +332,7 @@ describe('Context', () => {
}, 10000);
});

xtest('invitation redirect', async () => {
test('invitation redirect', async () => {
const invitation = 'wkhLzqInxdaXipRfBPyBtzcxs3wmoUDg';
const organization = 'org_lWF9avilXAry9Aid';

Expand Down Expand Up @@ -358,8 +362,9 @@ describe('Context', () => {
});
});

xtest('if error exists on the url with access_denied', async () => {
const { setOrganizations, setSelectedOrganization } = useOrganization.getState();
test('if error exists on the url with access_denied', async () => {
const { setOrganizations, setSelectedOrganization, selectedOrganization } =
useOrganization.getState();
const organizationList = [
{
org_id: 'testOrgId1',
Expand Down Expand Up @@ -399,13 +404,13 @@ describe('Context', () => {

isAuthenticated.mockResolvedValue(false);

await act(async () => {
render(
render(
<Organizations>
<Authentication>
<TestingComponentSimple />
</Authentication>
);
});
</Organizations>
);

await waitFor(() => expect(screen.getByTestId('isLoading').innerHTML).toBe('false'));
await waitFor(() => expect(loginWithRedirect).toBeCalledTimes(1));
Expand All @@ -417,43 +422,12 @@ describe('Context', () => {
});
}, 10000);

xtest('Context default functions', async () => {
test('Context default functions', async () => {
expect(await defaultAuthenticationContextValues.getAccessTokenSilently()).toEqual({
token: '',
decodedToken: {},
});
expect(await defaultAuthenticationContextValues.logout()).toBe('logged out');
expect(await defaultAuthenticationContextValues.loginWithRedirect()).toBe(undefined);
});

xtest('getAuth0Client failed process', async () => {
expect.assertions(1);
mockedCreateAuth0.mockImplementation(() => {
throw new Error();
});
// @ts-ignore
client = undefined;
try {
await getAuth0Client();
} catch (e) {
expect(e).toEqual(new Error(`getAuth0Client Error: Error`));
}
});

xtest('logoutAuth failed process', async () => {
expect.assertions(1);

// @ts-ignore
client = undefined;
// @ts-ignore make logout fail with no .logout property on client
mockedCreateAuth0.mockImplementation(() => {
return {};
});

try {
await logoutAuth();
} catch (e) {
expect(e).toEqual(new Error(`client_1.logout is not a function`));
}
});
});
File renamed without changes.
29 changes: 29 additions & 0 deletions src/__tests__/check-that-getAuth0Client-throws-properly.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createAuth0Client as mockedCreateAuth0 } from '../../__mocks__/@auth0/auth0-spa-js';
import { getAuth0Client, logoutAuth } from '../utils/auth';

test('getAuth0Client failed process', async () => {
expect.assertions(1);
mockedCreateAuth0.mockImplementation(() => {
throw new Error();
});

try {
await getAuth0Client();
} catch (e) {
expect(e).toEqual(new Error(`getAuth0Client Error: Error`));
}
});

test('logoutAuth failed process', async () => {
expect.assertions(1);
// @ts-ignore make logout fail with no .logout property on client
mockedCreateAuth0.mockImplementation(() => {
return {};
});

try {
await logoutAuth();
} catch (e) {
expect(e).toEqual(new Error(`client_1.logout is not a function`));
}
});

0 comments on commit 2b588d0

Please sign in to comment.