Skip to content

Commit

Permalink
fix(memoization): add memo to table, card & update test on fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Facundo Ledesma authored and Facundo Ledesma committed Dec 30, 2022
1 parent 28f15cc commit db33d8e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ledesma-app",
"homepage": "https://faculedesma.github.io/ledesma-app",
"private": true,
"version": "1.0.0",
"version": "1.0.2",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
9 changes: 7 additions & 2 deletions src/components/home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import HomeTable from './HomeTable';
import HomeCard from './HomeCard';
import './home.scss';

const Home: React.FC = (): JSX.Element => {
const [selectedPokemonId, setSelectedPokemonId] = useState<string>('');

const handleSelectPokemonId = (id: string): void => setSelectedPokemonId(id);
const handleSelectPokemonId = useMemo(
() =>
(id: string): void =>
setSelectedPokemonId(id),
[]
);

return (
<div className="home">
Expand Down
7 changes: 4 additions & 3 deletions src/components/home/HomeCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { memo } from 'react';
import Card from '@components/card/Card';
import { useSinglePokemon } from '@components/hooks/useSinglePokemon';

Expand Down Expand Up @@ -32,7 +32,8 @@ const HomeCard: React.FC<IPokemonCardProps> = ({ pokemonId }): JSX.Element => {
}

if (isError) {
return <div>Error!</div>;
console.error('Failed to load pokemon');
return <LoadingCard />;
}

return (
Expand All @@ -42,4 +43,4 @@ const HomeCard: React.FC<IPokemonCardProps> = ({ pokemonId }): JSX.Element => {
);
};

export default HomeCard;
export default memo(HomeCard);
11 changes: 8 additions & 3 deletions src/components/home/HomeTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { memo, useState } from 'react';
import {
Table,
TableRow,
Expand Down Expand Up @@ -37,7 +37,12 @@ const HomeTable: React.FC<IHomeTableProps> = ({
}

if (isError) {
return <div>Error!</div>;
console.error('Failed to load pokemons');
return (
<div className="home-table">
<TableLoading />
</div>
);
}

const handleSelectRow = (id: string): void => setPokemonId(id);
Expand Down Expand Up @@ -96,4 +101,4 @@ const HomeTable: React.FC<IHomeTableProps> = ({
);
};

export default HomeTable;
export default memo(HomeTable);
8 changes: 5 additions & 3 deletions test/Home/HomeCard.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const homeCardProps = {
pokemonId: '1'
};

describe('<HomeTable />', () => {
describe('<HomeCard />', () => {
it('fetches pokemon 1', async () => {
jest.spyOn(hooks, 'useSinglePokemon').mockImplementation(() => ({
data: mockedData,
Expand Down Expand Up @@ -55,7 +55,9 @@ describe('<HomeTable />', () => {
isError: true,
isLoading: false
}));
const { getByText } = await render(<HomeCard {...homeCardProps} />);
expect(getByText('Error!')).toBeInTheDocument();
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const { container } = await render(<HomeCard {...homeCardProps} />);
expect(container.firstChild).toHaveClass('card-loading');
expect(errorSpy).toHaveBeenCalledTimes(1);
});
});
6 changes: 4 additions & 2 deletions test/Home/HomeTable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ describe('<HomeTable />', () => {
isError: true,
isLoading: false
}));
const { getByText } = await render(<HomeTable {...homeTableProps} />);
expect(getByText('Error!')).toBeInTheDocument();
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const { container } = await render(<HomeTable {...homeTableProps} />);
expect(container.firstChild?.firstChild).toHaveClass('table-skeleton');
expect(errorSpy).toHaveBeenCalledTimes(1);
});
});

0 comments on commit db33d8e

Please sign in to comment.