Skip to content

Commit

Permalink
#29 Se añaden tests unitarios a los componentes y pantallas, tambien …
Browse files Browse the repository at this point in the history
…se añade el covarage de estos componentes
  • Loading branch information
AlvaroGlezC committed Apr 27, 2024
1 parent 29141c5 commit a8ba435
Show file tree
Hide file tree
Showing 18 changed files with 421 additions and 54 deletions.
16 changes: 14 additions & 2 deletions syg-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"coverage": "npm test -- --coverage --watchAll"
},
"eslintConfig": {
"extends": [
Expand All @@ -52,5 +53,16 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"jest": {
"coveragePathIgnorePatterns": [
"/node_modules/",
"/src/App.tsx",
"/src/modules/content/Content.tsx",
"/src/index.tsx",
"/src/reportWebVitals.ts",
"/src/secure/",
"/src/backend/dataSource.ts"
]
}
}
}
9 changes: 0 additions & 9 deletions syg-frontend/src/App.test.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion syg-frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import './App.scss';
function App() {

return (
<Content/>
<Content data-testid='app-content'/>
);
}

Expand Down
89 changes: 89 additions & 0 deletions syg-frontend/src/components/aside/Aside.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { screen, render, fireEvent, act } from "@testing-library/react"
import Aside from "./Aside"
import i18n from '../../translation/i18n';
import { MemoryRouter } from "react-router-dom";

describe('Header', () => {
beforeAll(() => {
i18n.changeLanguage('Spain');
});

test('Aside renders correctly', () => {
render(<Aside elements={[]} username={"Alvaro"} />)

const element = screen.getByTestId('syg-aside-container')
expect(element).toBeInTheDocument();
})

test('Header components renders correctly', () => {
render(<Aside elements={[]} username={"Alvaro"} />)

const asideNavigationMenu = screen.getByTestId('syg-aside-navigation-menu')
expect(asideNavigationMenu).toBeInTheDocument();

const asideMenu = screen.getByTestId('syg-aside-login-menu')
expect(asideMenu).toBeInTheDocument();

const asideUsernameContent = screen.getByTestId('syg-aside-username')
expect(asideUsernameContent).toBeInTheDocument();

const asideUsernameInitial = screen.getByTestId('syg-aside-username-initial')
expect(asideUsernameInitial).toBeInTheDocument();

const asideUsername = screen.getByTestId('syg-aside-username-complete')
expect(asideUsername).toBeInTheDocument();

const asideLogout = screen.getByTestId('syg-aside-logout')
expect(asideLogout).toBeInTheDocument();

const asideLogoutText = screen.getByTestId('syg-aside-logout-text')
expect(asideLogoutText).toBeInTheDocument();
})

test('Aside login user info text', () => {
render(<Aside elements={[]} username={"Alvaro"} />)

const asideUsernameInitial = screen.getByTestId('syg-aside-username-initial')
expect(asideUsernameInitial).toHaveTextContent("A");

const asideUsername = screen.getByTestId('syg-aside-username-complete')
expect(asideUsername).toHaveTextContent("Alvaro");
})

test('Aside logout user info text', () => {
render(<Aside elements={[]} username={"Alvaro"} />)

const asideLogoutText = screen.getByTestId('syg-aside-logout-text')
expect(asideLogoutText).toHaveTextContent("LOGOUT");
})

test('Aside change component option', () => {
const onClickAsideElementMockOption1 = jest.fn();
const onClickAsideElementMockOption2 = jest.fn();

render(<Aside elements={[
{
text:"Juego",
icon: <></>,
onClickAsideElement: onClickAsideElementMockOption1
},
{
text:"Historico",
icon: <></>,
onClickAsideElement: onClickAsideElementMockOption2
}
]} username={"Alvaro"} />)

const buttonOption2 = screen.getByText('HISTORICO');

fireEvent.click(buttonOption2);

expect(onClickAsideElementMockOption2).toHaveBeenCalledTimes(1);

const buttonOption1 = screen.getByText('JUEGO');

fireEvent.click(buttonOption1);

expect(onClickAsideElementMockOption2).toHaveBeenCalledTimes(1);
})
})
20 changes: 9 additions & 11 deletions syg-frontend/src/components/aside/Aside.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React from 'react';
import { Button } from '@mui/material';
import { useNavigate } from 'react-router-dom';
import { logout } from '../../secure/keycloak';
import LogoutIcon from '@mui/icons-material/Logout';
import './Aside.scss';
import { useTranslation } from 'react-i18next';
import './Aside.scss';

interface AsideProps {
elements: {
Expand All @@ -27,8 +25,8 @@ const Aside: React.FC<AsideProps> = (props: AsideProps) => {
}

return (
<div id='syg-aside-container'>
<div id='syg-aside-navigation-menu'>
<div id='syg-aside-container' data-testid='syg-aside-container'>
<div id='syg-aside-navigation-menu' data-testid='syg-aside-navigation-menu'>
{props.elements.map((element) => (
<div className='syg-aside-navigation-menu-element' onClick={()=>handleOnClickAsideElement(element.onClickAsideElement)}>
{element.icon}
Expand All @@ -38,14 +36,14 @@ const Aside: React.FC<AsideProps> = (props: AsideProps) => {
</div>
))}
</div>
<div id='syg-aside-login-menu'>
<div id='syg-aside-username'>
<div id='syg-aside-username-initial'>{props.username.substring(0, 1).toUpperCase()}</div>
<span id='syg-aside-username-complete'>{props.username}</span>
<div id='syg-aside-login-menu' data-testid='syg-aside-login-menu'>
<div id='syg-aside-username' data-testid='syg-aside-username'>
<div id='syg-aside-username-initial' data-testid='syg-aside-username-initial'>{props.username.substring(0, 1).toUpperCase()}</div>
<span id='syg-aside-username-complete' data-testid='syg-aside-username-complete'>{props.username}</span>
</div>
<div id='syg-aside-logout' onClick={handleOnClickLogoutElement}>
<div id='syg-aside-logout' onClick={handleOnClickLogoutElement} data-testid='syg-aside-logout'>
<LogoutIcon/>
<div className='aside-element'>{t('aside.logout')}</div>
<div id='syg-aside-logout-text' className='aside-element' data-testid='syg-aside-logout-text'>{t('aside.logout')}</div>
</div>
</div>
</div>
Expand Down
46 changes: 46 additions & 0 deletions syg-frontend/src/components/card/UserInfoCard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { screen, render, fireEvent } from "@testing-library/react"
import UserInfoCard from "./UserInfoCard";
import i18n from '../../translation/i18n';

describe('UserInfoCard', () => {
beforeAll(() => {
i18n.changeLanguage('Spain');
});

test('UserInfoCard renders correctly', () => {
render(<UserInfoCard title={"TotalGames"} info={"4"}/>)

const element = screen.getByTestId('syg-historic-card-container')
expect(element).toBeInTheDocument();
})

test('UserInfoCard components renders correctly', () => {
render(<UserInfoCard title={"TotalGames"} info={"4"}/>)

const historicHeaderCard = screen.getByTestId('syg-historic-card-header')
expect(historicHeaderCard).toBeInTheDocument();

const historicCardHeaderContent = screen.getByTestId('syg-historic-card-header-content')
expect(historicCardHeaderContent).toBeInTheDocument();

const historicCardHeaderTitle = screen.getByTestId('syg-historic-card-header-title')
expect(historicCardHeaderTitle).toBeInTheDocument();

const historicCardContent = screen.getByTestId('syg-historic-card-content')
expect(historicCardContent).toBeInTheDocument();

const historicCardContentInfo = screen.getByTestId('syg-historic-card-content-info')
expect(historicCardContentInfo).toBeInTheDocument();
})

test('UserInfoCard header and content text', () => {
render(<UserInfoCard title={"Juegos totales"} info={"4"}/>)

const historicHeader = screen.getByTestId('syg-historic-card-header-title')
expect(historicHeader).toHaveTextContent("Juegos totales");

const historicCardContent = screen.getByTestId('syg-historic-card-content-info')
expect(historicCardContent).toHaveTextContent("4");

})
})
11 changes: 6 additions & 5 deletions syg-frontend/src/components/card/UserInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ interface UserInfoCardProps {

const UserInfoCard: React.FC<UserInfoCardProps> = (props: UserInfoCardProps) => {
return (
<Card id='syg-historic-card-container'>
<Card id='syg-historic-card-container' data-testid='syg-historic-card-container'>
<CardHeader
id='syg-historic-card-header'
data-testid='syg-historic-card-header'
title ={
<div id='syg-historic-card-header-content' >
<div id='syg-historic-card-header-content' data-testid='syg-historic-card-header-content'>
{props.iconItem}
<span>{props.title}</span>
<span data-testid='syg-historic-card-header-title'>{props.title}</span>
</div>
}
/>
<CardContent id='syg-historic-card-content'>
<span>{props.info}</span>
<CardContent id='syg-historic-card-content' data-testid='syg-historic-card-content'>
<span data-testid='syg-historic-card-content-info'>{props.info}</span>
</CardContent>
</Card>
);
Expand Down
71 changes: 71 additions & 0 deletions syg-frontend/src/components/header/Header.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { screen, render, fireEvent } from "@testing-library/react"
import Header from "./Header"
import i18n from '../../translation/i18n';

describe('Header', () => {
beforeAll(() => {
i18n.changeLanguage('Spain');
});

test('Header renders correctly', () => {
render(<Header info={"game"}/>)

const element = screen.getByTestId('syg-header-container')
expect(element).toBeInTheDocument();
})

test('Header components renders correctly', () => {
render(<Header info={"historics"}/>)

const headerContainer = screen.getByTestId('syg-header-logo-container')
expect(headerContainer).toBeInTheDocument();

const headerLogo = screen.getByTestId('syg-header-logo')
expect(headerLogo).toBeInTheDocument();

const headerLogoLetters = screen.getByTestId('syg-header-logo-letters')
expect(headerLogoLetters).toBeInTheDocument();

const laguageSwitch = screen.getByTestId('syg-header-language-switch')
expect(laguageSwitch).toBeInTheDocument();

const subHeaderText = screen.getByTestId('syg-header-info')
expect(subHeaderText).toBeInTheDocument();
})

test('Header active language', () => {
render(<Header info={"game"}/>)

const languageActive = screen.getByTestId('syg-header-language-switch-element-active')
expect(languageActive).toHaveTextContent('Español');
})

test('Header subtitle text', () => {
render(<Header info={"ranking"}/>)

const subHeader = screen.getByTestId('syg-header-info')
expect(subHeader).toHaveTextContent('Ranking');
})

test('Header subtitle text in another language', () => {
i18n.changeLanguage('English');

render(<Header info={"game"}/>)

const subHeader = screen.getByTestId('syg-header-info')
expect(subHeader).toHaveTextContent('Game');
})

test('Header button chagne language', () => {
i18n.changeLanguage('Spain');

render(<Header info={"game"}/>)

const button = screen.getByText('Ingles'); // Encuentra el botón que cambia el idioma

fireEvent.click(button);

const languageActive = screen.getByText('English');
expect(languageActive).toBeInTheDocument();
})
})
15 changes: 8 additions & 7 deletions syg-frontend/src/components/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ const Header: React.FC<HeaderProps> = (props: HeaderProps) => {
const { t, i18n } = useTranslation();

return (
<div id='syg-header-container'>
<div id='syg-header-logo-container'>
<div id='syg-header-logo'>
<div id='syg-header-container' data-testid='syg-header-container'>
<div id='syg-header-logo-container' data-testid='syg-header-logo-container'>
<div id='syg-header-logo' data-testid='syg-header-logo'>
<img src="/images/syg_logo.png" alt="Logo de syg_alvaro" />
</div>
<div id='syg-header-logo-letters'>
<div id='syg-header-language-switch'>
<div id='syg-header-logo-letters' data-testid='syg-header-logo-letters'>
<div id='syg-header-language-switch' data-testid='syg-header-language-switch'>
{Object.keys(lngs).map((lng) => (
<Button className={`syg-header-language-switch-element ${i18n.resolvedLanguage === lng ? 'active': ''}`} key={lng} onClick={() => i18n.changeLanguage(lng)}>
<Button className={`syg-header-language-switch-element ${i18n.resolvedLanguage === lng ? 'active': ''}`} key={lng} onClick={() => i18n.changeLanguage(lng)}
data-testid={`syg-header-language-switch-element-${i18n.resolvedLanguage === lng ? 'active': ''}`}>
{t(`header.language.${lng}`)}
</Button>
))}
</div>
<img src="/images/syg_logo_letters.png" alt="Letrero de syg_alvaro" />
</div>
</div>
<div id='syg-header-info'>
<div id='syg-header-info' data-testid='syg-header-info'>
{t(`header.subtitle.${props.info}`)}
</div>
</div>
Expand Down
Loading

0 comments on commit a8ba435

Please sign in to comment.