Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thais Martins committed Sep 6, 2023
1 parent 95f9988 commit afa254c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
12 changes: 6 additions & 6 deletions app/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ export default function Select({ options, onChange }: SelectProps) {

return (
<div className="relative h-10 w-96 rounded bg-white border border-gray-300">
<div
<button
className="flex items-center px-4 w-full h-full"
onClick={toggleSelect}
aria-expanded={isOpen}
aria-controls="listbox"
role="combobox"
>
{hasOption ? selectedOption.label : "Selecione"}
</div>
</button>
{isOpen && (
<ul
className="absolute mt-1 py-4 top-full left-0 right-0 bg-white z-50 shadow rounded"
role="listbox"
>
<ul className="absolute mt-1 py-4 top-full left-0 right-0 bg-white z-50 shadow rounded">
{options.map((option) => (
<li
key={option.value}
Expand Down
4 changes: 4 additions & 0 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface Pokemon {
}[];
}

if (process.env.NEXT_PUBLIC_API_MOCKING) {
import("@/lib/mocks");
}

export default function Home() {
const [pokemonName, setPokemonName] = useState("");
const [pokemonInfo, setPokemonInfo] = useState({} as Pokemon);
Expand Down
4 changes: 3 additions & 1 deletion lib/mocks/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const handlers = [];
import { pokemonHandlers } from "./pokemon";

export const handlers = [...pokemonHandlers];
2 changes: 1 addition & 1 deletion lib/mocks/handlers/pokemons/bulbasaur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const bulbasaur = {
order: 1,
past_types: [],
species: {
name: "bulbasaur",
name: "pikachu",
url: "https://pokeapi.co/api/v2/pokemon-species/1/",
},
sprites: {
Expand Down
30 changes: 30 additions & 0 deletions test/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { render, screen, act, waitFor } from "@testing-library/react";
import user from "@testing-library/user-event";
import RootLayout from "@/app/layout";
import Page from "@/app/page";

describe("Index page", () => {
it("render", () => {
render(
<RootLayout>
<Page />
</RootLayout>
);

expect(screen.getByRole("combobox")).toBeInTheDocument();
});

it("displays Bulbasaur pokemon when selecting grass type", async () => {
render(<Page />);

user.click(screen.getByRole("combobox"));

await waitFor(async () => {
user.click(screen.getByRole("option", { name: /grama/i }));
});

await waitFor(async () => {
expect(screen.getByText(/pikachu/i)).toBeInTheDocument();
});
});
});
11 changes: 11 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import "@testing-library/jest-dom/extend-expect";
import { cleanup } from "@testing-library/react";
import { server } from "../lib/mocks/server";
import { vi } from "vitest";

vi.mock("next/font/google", () => {
return {
Inter: vi.fn().mockReturnValue({
className: "className",
variable: "variable",
style: { fontFamily: "fontFamily" },
}),
};
});

beforeAll(() => {
cleanup();
Expand Down

0 comments on commit afa254c

Please sign in to comment.