Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/hook-tests #53

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/components/App/App.test.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en la linea 10 del test, gramaticalmente esta mal planteado, debería ser
Then it should go to HomePage
el have sobra, puedes eliminarlo ??

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ import CharacarterWrapper from "../../features/characters/store/CharactersWrappe

describe("Given component App", () => {
describe("When it is render", () => {
test("It should have a HTMLElement main", async () => {
test("Then it should have go to HomePage", async () => {
render(
<CharacarterWrapper>
<ThemeProvider theme={mainTheme}>
<MemoryRouter initialEntries={[{ pathname: "/" }]}>
<MemoryRouter initialEntries={["/"]}>
<App />
</MemoryRouter>
</ThemeProvider>
</CharacarterWrapper>,
);
const homePageElement = screen.getByRole("heading", {

const headingElment = screen.getByRole("heading", {
name: "Characters",
});

expect(homePageElement).toBeInTheDocument();
expect(headingElment).toBeInTheDocument();
});
});
});
39 changes: 22 additions & 17 deletions src/components/CharacterList/CharaterList.test.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en la linea 32, lo mismo que el comentario anterior
cambia : it expect to have characters cards
por: It should have

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ThemeProvider } from "styled-components";
import mainTheme from "../../styles/mainTheme";
import CharacterList from "./CharacterList";
import { getAllByRole, render, screen } from "@testing-library/react";
import { render, screen, waitFor } from "@testing-library/react";
import CharacarterWrapper from "../../features/characters/store/CharactersWrapper";
import App from "../App/App";
import { MemoryRouter } from "react-router-dom";
import HomePage from "../../pages/HomePage/HomePage";

describe("Given the component CharacterList", () => {
describe("When CharacterList is initialize", () => {
Expand All @@ -29,25 +29,30 @@ describe("Given the component CharacterList", () => {
});

describe("When we recive data of characters", () => {
test("it expect to have characters cards", () => {
const expectedNames = ["Characters", "Link", "Yoshi"];

render(
<CharacarterWrapper>
<ThemeProvider theme={mainTheme}>
<MemoryRouter initialEntries={[{ pathname: "/" }]}>
<App />
</MemoryRouter>
</ThemeProvider>
</CharacarterWrapper>,
test("it expect to have characters cards", async () => {
const mario = "Mario";
const donkeyKong = "Donkey Kong";
await waitFor(() =>
render(
<CharacarterWrapper>
<ThemeProvider theme={mainTheme}>
<MemoryRouter initialEntries={["/"]}>
<HomePage />
</MemoryRouter>
</ThemeProvider>
</CharacarterWrapper>,
),
);

const mainElement = screen.getByRole("main");
const headingElements = getAllByRole(mainElement, "heading");
const marioElement = screen.getByRole("heading", {
name: mario,
});

headingElements.forEach((element, position) => {
expect(expectedNames[position]).toBe(element.textContent);
const donkeyKongElement = screen.getByRole("heading", {
name: donkeyKong,
});
expect(marioElement).toBeInTheDocument();
expect(donkeyKongElement).toBeInTheDocument();
});
});
});
10 changes: 1 addition & 9 deletions src/data/apiSmash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {
CharacterStructureApi,
} from "../features/characters/types";

const apiUrl = import.meta.env.VITE_API_URL;

export const characterStructureApiToCharacterStructure = ({
export const characterApiToCharacter = ({
id,
name,
availability,
Expand All @@ -27,9 +25,3 @@ export const characterStructureApiToCharacterStructure = ({

return characterStructure;
};

export const getCharacters = async (): Promise<Response> => {
const response = await fetch(`${apiUrl}/characters`);

return response;
};
21 changes: 10 additions & 11 deletions src/hooks/useCharactersApi.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { useCallback, useContext } from "react";
import {
characterStructureApiToCharacterStructure,
getCharacters,
} from "../data/apiSmash";
import { characterApiToCharacter } from "../data/apiSmash";
import { CharacterStructureApi } from "../features/characters/types";
import CharactersContext from "../features/characters/store/CharactersContext";

const apiUrl = import.meta.env.VITE_API_URL;

export const useCharactersApi = () => {
const { loadCharacters } = useContext(CharactersContext);

const loadCharactersApi = useCallback(async () => {
const response = await getCharacters();
const getCharacter = useCallback(async () => {
const response = await fetch(`${apiUrl}/characters`);

const charactersApi = (await response.json()) as CharacterStructureApi[];

const characters = charactersApi.map((characterApi) =>
characterStructureApiToCharacterStructure(characterApi),
loadCharacters(
charactersApi.map((characterApi) =>
characterApiToCharacter(characterApi),
),
);

loadCharacters(characters);
}, [loadCharacters]);

return { loadCharactersApi };
return { loadCharactersApi: getCharacter };
};
Loading