feat(client): ts-migration for layout index file, Intro Component and header test file (#43094)

This commit is contained in:
Ismail Tlemcani
2021-08-26 23:17:25 +01:00
committed by GitHub
parent 5cc76bdc88
commit 67ee31cbfc
3 changed files with 51 additions and 38 deletions

View File

@ -1,6 +1,10 @@
import React from 'react'; /* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import React, { Ref } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { create } from 'react-test-renderer'; import { create, ReactTestRendererJSON } from 'react-test-renderer';
import ShallowRenderer from 'react-test-renderer/shallow'; import ShallowRenderer from 'react-test-renderer/shallow';
import envData from '../../../../config/env.json'; import envData from '../../../../config/env.json';
@ -15,8 +19,9 @@ jest.mock('../../analytics');
describe('<UniversalNav />', () => { describe('<UniversalNav />', () => {
const UniversalNavProps = { const UniversalNavProps = {
displayMenu: false, displayMenu: false,
menuButtonRef: {}, menuButtonRef: {} as Ref<HTMLButtonElement> | undefined,
searchBarRef: {}, searchBarRef: {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
toggleDisplayMenu: function () {}, toggleDisplayMenu: function () {},
pathName: '/', pathName: '/',
fetchState: { fetchState: {
@ -24,14 +29,15 @@ describe('<UniversalNav />', () => {
} }
}; };
it('renders to the DOM', () => { it('renders to the DOM', () => {
const shallow = new ShallowRenderer(); const utils = ShallowRenderer.createRenderer();
shallow.render(<UniversalNav {...UniversalNavProps} />); utils.render(<UniversalNav {...UniversalNavProps} />);
const view = shallow.getRenderOutput(); const view = utils.getRenderOutput();
expect(view).toBeTruthy(); expect(view).toBeTruthy();
}); });
}); });
describe('<NavLinks />', () => { describe('<NavLinks />', () => {
const { t } = useTranslation();
it('has expected navigation links when not signed in', () => { it('has expected navigation links when not signed in', () => {
const landingPageProps = { const landingPageProps = {
fetchState: { fetchState: {
@ -45,12 +51,12 @@ describe('<NavLinks />', () => {
i18n: { i18n: {
language: 'en' language: 'en'
}, },
toggleNightMode: theme => theme toggleNightMode: (theme: string) => theme,
t: t
}; };
const shallow = new ShallowRenderer(); const utils = ShallowRenderer.createRenderer();
shallow.render(<NavLinks {...landingPageProps} />); utils.render(<NavLinks {...landingPageProps} />);
const view = shallow.getRenderOutput(); const view = utils.getRenderOutput();
expect( expect(
hasDonateNavItem(view) && hasDonateNavItem(view) &&
hasSignInNavItem(view) && hasSignInNavItem(view) &&
@ -74,12 +80,12 @@ describe('<NavLinks />', () => {
i18n: { i18n: {
language: 'en' language: 'en'
}, },
t: useTranslation.t, t: t,
toggleNightMode: theme => theme toggleNightMode: (theme: string) => theme
}; };
const shallow = new ShallowRenderer(); const utils = ShallowRenderer.createRenderer();
shallow.render(<NavLinks {...landingPageProps} />); utils.render(<NavLinks {...landingPageProps} />);
const view = shallow.getRenderOutput(); const view = utils.getRenderOutput();
expect( expect(
hasDonateNavItem(view) && hasDonateNavItem(view) &&
hasCurriculumNavItem(view) && hasCurriculumNavItem(view) &&
@ -104,12 +110,12 @@ describe('<NavLinks />', () => {
i18n: { i18n: {
language: 'en' language: 'en'
}, },
t: useTranslation.t, t: t,
toggleNightMode: theme => theme toggleNightMode: (theme: string) => theme
}; };
const shallow = new ShallowRenderer(); const utils = ShallowRenderer.createRenderer();
shallow.render(<NavLinks {...landingPageProps} />); utils.render(<NavLinks {...landingPageProps} />);
const view = shallow.getRenderOutput(); const view = utils.getRenderOutput();
expect( expect(
hasThanksForDonating(view) && hasThanksForDonating(view) &&
hasCurriculumNavItem(view) && hasCurriculumNavItem(view) &&
@ -192,39 +198,43 @@ describe('<AuthOrProfile />', () => {
}); });
}); });
const navigationLinks = (component, key) => { const navigationLinks = (component: JSX.Element, key: string) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const target = component.props.children.find( const target = component.props.children.find(
child => child && child.key === key (child: { key?: string }) => child && child.key === key
); );
return target.props; return target.props;
}; };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const profileNavItem = (component: any) => component.children[0];
const profileNavItem = component => component.children[0]; const hasDonateNavItem = (component: JSX.Element) => {
const hasDonateNavItem = component => {
const { children, to } = navigationLinks(component, 'donate'); const { children, to } = navigationLinks(component, 'donate');
return children === 'buttons.donate' && to === '/donate'; return children === 'buttons.donate' && to === '/donate';
}; };
const hasThanksForDonating = component => { const hasThanksForDonating = (component: JSX.Element) => {
const { children } = navigationLinks(component, 'donate'); const { children } = navigationLinks(component, 'donate');
return children[0].props.children === 'donate.thanks'; return children ? children[0].props.children === 'donate.thanks' : null;
}; };
const hasSignInNavItem = component => { const hasSignInNavItem = (component: JSX.Element) => {
const { children } = navigationLinks(component, 'signin'); const { children } = navigationLinks(component, 'signin');
return children === 'buttons.sign-in'; return children === 'buttons.sign-in';
}; };
const hasCurriculumNavItem = component => { const hasCurriculumNavItem = (component: JSX.Element) => {
const { children, to } = navigationLinks(component, 'learn'); const { children, to } = navigationLinks(component, 'learn');
return children === 'buttons.curriculum' && to === '/learn'; return children === 'buttons.curriculum' && to === '/learn';
}; };
const hasProfileAndSettingsNavItems = (component, username) => { const hasProfileAndSettingsNavItems = (
component: JSX.Element,
username: string
) => {
const fragment = navigationLinks(component, 'profile-settings'); const fragment = navigationLinks(component, 'profile-settings');
const profile = fragment.children[0].props; const profile = fragment ? fragment.children[0].props : null;
const settings = fragment.children[1].props; const settings = fragment.children[1].props;
const hasProfile = const hasProfile =
@ -235,7 +245,7 @@ const hasProfileAndSettingsNavItems = (component, username) => {
return hasProfile && hasSettings; return hasProfile && hasSettings;
}; };
const hasForumNavItem = component => { const hasForumNavItem = (component: JSX.Element) => {
const { children, to } = navigationLinks(component, 'forum'); const { children, to } = navigationLinks(component, 'forum');
// TODO: test compiled TFunction value // TODO: test compiled TFunction value
return ( return (
@ -243,14 +253,14 @@ const hasForumNavItem = component => {
); );
}; };
const hasNewsNavItem = component => { const hasNewsNavItem = (component: JSX.Element) => {
const { children, to } = navigationLinks(component, 'news'); const { children, to } = navigationLinks(component, 'news');
return ( return (
children[0].props.children === 'buttons.news' && to === 'links:nav.news' children[0].props.children === 'buttons.news' && to === 'links:nav.news'
); );
}; };
const hasRadioNavItem = component => { const hasRadioNavItem = (component: JSX.Element) => {
const { children, to } = navigationLinks(component, 'radio'); const { children, to } = navigationLinks(component, 'radio');
return ( return (
children[0].props.children === 'buttons.radio' && children[0].props.children === 'buttons.radio' &&
@ -258,7 +268,7 @@ const hasRadioNavItem = component => {
); );
}; };
const hasSignOutNavItem = component => { const hasSignOutNavItem = (component: JSX.Element) => {
const { children } = navigationLinks(component, 'signout-frag'); const { children } = navigationLinks(component, 'signout-frag');
const signOutProps = children[1].props; const signOutProps = children[1].props;
@ -273,7 +283,10 @@ const hasSignInButton = component =>
component.props.children[1].props.children === 'buttons.sign-in'; component.props.children[1].props.children === 'buttons.sign-in';
*/ */
const avatarHasClass = (componentTree, classes) => { const avatarHasClass = (
componentTree: ReactTestRendererJSON | ReactTestRendererJSON[] | null,
classes: string
) => {
return ( return (
profileNavItem(componentTree).props.className === profileNavItem(componentTree).props.className ===
'avatar-container ' + classes 'avatar-container ' + classes

View File

@ -6,7 +6,7 @@ import { Link, Spacer } from '../../helpers';
import '../intro.css'; import '../intro.css';
const { forumLocation } = envData; const { forumLocation } = envData;
function IntroDescription() { function IntroDescription(): JSX.Element {
const { t } = useTranslation(); const { t } = useTranslation();
return ( return (