Skip to content

Commit

Permalink
fix: fixed event aborting in firefox due to page redirect (#1336)
Browse files Browse the repository at this point in the history
* fix: fixed event aborting in firefox due to page redirect

* fix: fixed test cases

* refactor: created a util for redirect
  • Loading branch information
ayesha-waris authored Oct 23, 2024
1 parent ceb4897 commit 62508e3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/common-components/RedirectLogistration.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AUTHN_PROGRESSIVE_PROFILING, RECOMMENDATIONS, REDIRECT,
} from '../data/constants';
import setCookie from '../data/utils/cookies';
import { redirectWithDelay } from '../data/utils/dataUtils';

const RedirectLogistration = (props) => {
const {
Expand Down Expand Up @@ -81,8 +82,7 @@ const RedirectLogistration = (props) => {
/>
);
}

window.location.href = finalRedirectUrl;
redirectWithDelay(finalRedirectUrl);
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion src/common-components/SocialAuthProviders.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
LOGIN_PAGE, REGISTER_PAGE, SUPPORTED_ICON_CLASSES,
} from '../data/constants';
import { setCookie } from '../data/utils';
import { redirectWithDelay } from '../data/utils/dataUtils';

const SocialAuthProviders = (props) => {
const { formatMessage } = useIntl();
Expand All @@ -36,7 +37,7 @@ const SocialAuthProviders = (props) => {
setCookie('marketingEmailsOptIn', registrationFields?.configurableFormFields?.marketingEmailsOptIn);
}
const url = e.currentTarget.dataset.providerUrl;
window.location.href = getConfig().LMS_BASE_URL + url;
redirectWithDelay(getConfig().LMS_BASE_URL + url);
}

const socialAuth = socialAuthProviders.map((provider, index) => (
Expand Down
6 changes: 6 additions & 0 deletions src/data/utils/dataUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ export const isHostAvailableInQueryParams = () => {
const queryParams = getAllPossibleQueryParams();
return 'host' in queryParams;
};

export const redirectWithDelay = (redirectUrl) => {
setTimeout(() => {
window.location.href = redirectUrl;
}, 400);
};
24 changes: 16 additions & 8 deletions src/login/tests/LoginPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ describe('LoginPage', () => {

// ******** test redirection ********

it('should redirect to url returned by login endpoint after successful authentication', () => {
it('should redirect to url returned by login endpoint after successful authentication', async () => {
const dashboardURL = 'https://test.com/testing-dashboard/';
store = mockStore({
...initialState,
Expand All @@ -531,10 +531,12 @@ describe('LoginPage', () => {
delete window.location;
window.location = { href: getConfig().BASE_URL };
render(reduxWrapper(<IntlLoginPage {...props} />));
expect(window.location.href).toBe(dashboardURL);
await waitFor(() => {
expect(window.location.href).toBe(dashboardURL);
});
});

it('should redirect to finishAuthUrl upon successful login via SSO', () => {
it('should redirect to finishAuthUrl upon successful login via SSO', async () => {
const authCompleteUrl = '/auth/complete/google-oauth2/';
store = mockStore({
...initialState,
Expand All @@ -558,10 +560,12 @@ describe('LoginPage', () => {
window.location = { href: getConfig().BASE_URL };

render(reduxWrapper(<IntlLoginPage {...props} />));
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + authCompleteUrl);
await waitFor(() => {
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + authCompleteUrl);
});
});

it('should redirect to social auth provider url on SSO button click', () => {
it('should redirect to social auth provider url on SSO button click', async () => {
store = mockStore({
...initialState,
commonComponents: {
Expand All @@ -582,10 +586,12 @@ describe('LoginPage', () => {
'',
{ selector: '#oa2-apple-id' },
));
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + ssoProvider.loginUrl);
await waitFor(() => {
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + ssoProvider.loginUrl);
});
});

it('should redirect to finishAuthUrl upon successful authentication via SSO', () => {
it('should redirect to finishAuthUrl upon successful authentication via SSO', async () => {
const finishAuthUrl = '/auth/complete/google-oauth2/';
store = mockStore({
...initialState,
Expand All @@ -606,7 +612,9 @@ describe('LoginPage', () => {
window.location = { href: getConfig().BASE_URL };

render(reduxWrapper(<IntlLoginPage {...props} />));
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + finishAuthUrl);
await waitFor(() => {
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + finishAuthUrl);
});
});

// ******** test hinted third party auth ********
Expand Down
12 changes: 8 additions & 4 deletions src/progressive-profiling/tests/ProgressiveProfiling.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { identifyAuthenticatedUser, sendTrackEvent } from '@edx/frontend-platfor
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { configure, injectIntl, IntlProvider } from '@edx/frontend-platform/i18n';
import {
fireEvent, render, screen,
fireEvent, render, screen, waitFor,
} from '@testing-library/react';
import { MemoryRouter, mockNavigate, useLocation } from 'react-router-dom';
import configureStore from 'redux-mock-store';
Expand Down Expand Up @@ -299,7 +299,9 @@ describe('ProgressiveProfilingTests', () => {
const nextButton = container.querySelector('button.btn-brand');
expect(nextButton.textContent).toEqual('Submit');

expect(window.location.href).toEqual(redirectUrl);
await waitFor(() => {
expect(window.location.href).toEqual(redirectUrl);
});
});
});

Expand Down Expand Up @@ -414,7 +416,7 @@ describe('ProgressiveProfilingTests', () => {
expect(window.location.href).toBe(DASHBOARD_URL);
});

it('should redirect to provided redirect url', () => {
it('should redirect to provided redirect url', async () => {
const redirectUrl = 'https://redirect-test.com';
delete window.location;
window.location = {
Expand Down Expand Up @@ -444,7 +446,9 @@ describe('ProgressiveProfilingTests', () => {
render(reduxWrapper(<IntlProgressiveProfilingPage />));
const submitButton = screen.getByText('Submit');
fireEvent.click(submitButton);
expect(window.location.href).toBe(redirectUrl);
await waitFor(() => {
expect(window.location.href).toBe(redirectUrl);
});
});
});
});
14 changes: 9 additions & 5 deletions src/register/RegistrationPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { sendPageEvent, sendTrackEvent } from '@edx/frontend-platform/analytics'
import {
configure, getLocale, injectIntl, IntlProvider,
} from '@edx/frontend-platform/i18n';
import { fireEvent, render } from '@testing-library/react';
import { fireEvent, render, waitFor } from '@testing-library/react';
import { mockNavigate, BrowserRouter as Router } from 'react-router-dom';
import configureStore from 'redux-mock-store';

Expand Down Expand Up @@ -508,7 +508,7 @@ describe('RegistrationPage', () => {
expect(document.cookie).toMatch(`${getConfig().USER_RETENTION_COOKIE_NAME}=true`);
});

it('should redirect to url returned in registration result after successful account creation', () => {
it('should redirect to url returned in registration result after successful account creation', async () => {
const dashboardURL = 'https://test.com/testing-dashboard/';
store = mockStore({
...initialState,
Expand All @@ -523,10 +523,12 @@ describe('RegistrationPage', () => {
delete window.location;
window.location = { href: getConfig().BASE_URL };
render(routerWrapper(reduxWrapper(<IntlRegistrationPage {...props} />)));
expect(window.location.href).toBe(dashboardURL);
await waitFor(() => {
expect(window.location.href).toBe(dashboardURL);
});
});

it('should redirect to dashboard if features flags are configured but no optional fields are configured', () => {
it('should redirect to dashboard if features flags are configured but no optional fields are configured', async () => {
mergeConfig({
ENABLE_PROGRESSIVE_PROFILING_ON_AUTHN: true,
});
Expand All @@ -550,7 +552,9 @@ describe('RegistrationPage', () => {
delete window.location;
window.location = { href: getConfig().BASE_URL };
render(routerWrapper(reduxWrapper(<IntlRegistrationPage {...props} />)));
expect(window.location.href).toBe(dashboardUrl);
await waitFor(() => {
expect(window.location.href).toBe(dashboardUrl);
});
});

it('should redirect to progressive profiling page if optional fields are configured', () => {
Expand Down
14 changes: 9 additions & 5 deletions src/register/components/tests/ThirdPartyAuth.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getConfig, mergeConfig } from '@edx/frontend-platform';
import {
configure, getLocale, injectIntl, IntlProvider,
} from '@edx/frontend-platform/i18n';
import { fireEvent, render } from '@testing-library/react';
import { fireEvent, render, waitFor } from '@testing-library/react';
import { BrowserRouter as Router } from 'react-router-dom';
import configureStore from 'redux-mock-store';

Expand Down Expand Up @@ -342,7 +342,7 @@ describe('ThirdPartyAuth', () => {
expect(headingElement).toBeTruthy();
});

it('should redirect to social auth provider url on SSO button click', () => {
it('should redirect to social auth provider url on SSO button click', async () => {
const registerUrl = '/auth/login/apple-id/?auth_entry=register&next=/dashboard';
store = mockStore({
...initialState,
Expand All @@ -368,10 +368,12 @@ describe('ThirdPartyAuth', () => {
const ssoButton = container.querySelector('button#oa2-apple-id');
fireEvent.click(ssoButton);

expect(window.location.href).toBe(getConfig().LMS_BASE_URL + registerUrl);
await waitFor(() => {
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + registerUrl);
});
});

it('should redirect to finishAuthUrl upon successful registration via SSO', () => {
it('should redirect to finishAuthUrl upon successful registration via SSO', async () => {
const authCompleteUrl = '/auth/complete/google-oauth2/';
store = mockStore({
...initialState,
Expand All @@ -394,7 +396,9 @@ describe('ThirdPartyAuth', () => {
window.location = { href: getConfig().BASE_URL };

render(routerWrapper(reduxWrapper(<IntlRegistrationPage {...props} />)));
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + authCompleteUrl);
await waitFor(() => {
expect(window.location.href).toBe(getConfig().LMS_BASE_URL + authCompleteUrl);
});
});

// ******** test alert messages ********
Expand Down

0 comments on commit 62508e3

Please sign in to comment.