Skip to content

Commit

Permalink
Fix launchpad infinite load edge cases (Automattic#72272)
Browse files Browse the repository at this point in the history
* Add launchpad redirection for user not logged in

* Add logic to handle unknown blog redirect

* Update launchpad index test cases

* Update launchpad test cases

* Check for site details fetch failures
  • Loading branch information
agrullon95 authored Jan 19, 2023
1 parent 679f8bb commit f20310c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { StepContainer } from '@automattic/onboarding';
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { useTranslate } from 'i18n-calypso';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import DocumentHead from 'calypso/components/data/document-head';
import FormattedHeader from 'calypso/components/formatted-header';
import { NavigationControls } from 'calypso/landing/stepper/declarative-flow/internals/types';
import { useSite } from 'calypso/landing/stepper/hooks/use-site';
import { useSiteSlugParam } from 'calypso/landing/stepper/hooks/use-site-slug-param';
import { SITE_STORE } from 'calypso/landing/stepper/stores';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { successNotice } from 'calypso/state/notices/actions';
import { useQuery } from '../../../../hooks/use-query';
import StepContent from './step-content';
import type { Step } from '../../types';

import './style.scss';

type LaunchpadProps = {
Expand All @@ -27,6 +31,17 @@ const Launchpad: Step = ( { navigation, flow }: LaunchpadProps ) => {
const site = useSite();
const launchpadScreenOption = site?.options?.launchpad_screen;
const dispatch = useDispatch();
const isLoggedIn = useSelector( isUserLoggedIn );

const fetchingSiteError = useSelect( ( select ) => select( SITE_STORE ).getFetchingSiteError() );

if ( ! isLoggedIn ) {
window.location.replace( `/home/${ siteSlug }` );
}

if ( ! siteSlug || fetchingSiteError?.error ) {
window.location.replace( '/home' );
}

useEffect( () => {
if ( verifiedParam ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ jest.mock( 'calypso/state/sites/hooks/use-premium-global-styles', () => ( {
// JSDOM doesn't support browser navigation, so we temporarily mock the
// window.location object
const replaceMock = jest.fn();
declare global {
interface Window {
initialReduxState: object;
}
}
const savedWindow = window;
global.window = Object.create( window );
Object.defineProperty( window, 'location', {
Expand All @@ -54,8 +59,14 @@ const user = {
email: 'testEmail@gmail.com',
};

function renderLaunchpad( props = {}, siteDetails = defaultSiteDetails ): void {
function renderLaunchpad(
props = {},
siteDetails = defaultSiteDetails,
initialReduxState = {},
siteSlug = ''
): void {
function TestLaunchpad( props ) {
window.initialReduxState = initialReduxState;
const initialState = getInitialState( initialReducer, user.ID );
const reduxStore = createReduxStore( initialState, initialReducer );
setStore( reduxStore, getStateFromCache( user.ID ) );
Expand Down Expand Up @@ -93,33 +104,75 @@ describe( 'Launchpad', () => {
/* eslint-enable @typescript-eslint/no-empty-function */
};

beforeEach( () => {
jest.clearAllMocks();
} );

afterAll( () => {
global.window = savedWindow;
} );

describe( 'when loading the Launchpad view', () => {
describe( 'and the site is launchpad enabled', () => {
it( 'does not redirect', () => {
renderLaunchpad( props );
const initialReduxState = { currentUser: { id: user.ID } };
renderLaunchpad( props, defaultSiteDetails, initialReduxState, siteSlug );
expect( replaceMock ).not.toBeCalled();
} );
} );

describe( 'and the site is not launchpad enabled', () => {
it( 'redirects to Calypso My Home', () => {
const initialReduxState = { currentUser: { id: user.ID } };
renderLaunchpad(
props,
buildSiteDetails( {
options: {
...defaultSiteDetails.options,
launchpad_screen: 'off',
},
} )
} ),
initialReduxState,
siteSlug
);

expect( replaceMock ).toBeCalledTimes( 1 );
expect( replaceMock ).toBeCalledWith( `/home/${ siteSlug }` );
} );
} );

describe( 'user is logged out', () => {
it( 'should redirect user to Calypso My Home', () => {
renderLaunchpad(
props,
buildSiteDetails( {
options: {
...defaultSiteDetails.options,
launchpad_screen: 'off',
},
} ),
{},
siteSlug
);
expect( replaceMock ).toBeCalledWith( `/home/${ siteSlug }` );
} );
} );

describe( 'site slug does not exist', () => {
it( 'should redirect user to Calypso My Home', () => {
const initialReduxState = { currentUser: { id: user.ID } };
renderLaunchpad(
props,
buildSiteDetails( {
options: {
...defaultSiteDetails.options,
launchpad_screen: 'off',
},
} ),
initialReduxState,
''
);
expect( replaceMock ).toBeCalledWith( `/home` );
} );
} );
} );
} );

0 comments on commit f20310c

Please sign in to comment.