-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for components to check rendering and expected behavi…
…our. docs: update README. chore: move CSS modules into folders with components. having tests and CSS modules in the same folder as the components increases ease of maintainability. this ticket fixes #15, although the subtasks will be completed separately: #25 will test the hook, which has some issues that need to be resolved. #26 will implement e2e testing. #10 is tested here, along with ensuring the helper function to randomly select Pokemon IDs do not return any ids that are duplicates (that would be bad for gameplay). README was updated to increase size of the logo, update the screenshot to have hover effect apparent, and added more explanation around using Vitest and RTL. Removed acknowledgement for an old loading image (Pokeball) as we are now just using the Masterball that Muhammad Jazman created.
- Loading branch information
1 parent
f049d10
commit cfee34d
Showing
18 changed files
with
200 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import Card from "."; | ||
|
||
describe("Card component", () => { | ||
const MOCK_POKEMON_DATA = { id: 404, name: "Pokemon", imageUrl: "" }; | ||
|
||
it("should render a clickable card with an image and text", () => { | ||
render( | ||
<Card | ||
pokemonData={MOCK_POKEMON_DATA} | ||
handleCardSelection={() => {}} | ||
isLoading={false} | ||
/> | ||
); | ||
|
||
const cardElement = screen.getByRole("button", { name: /Pokemon/i }); | ||
const pokemonNameElement = screen.getByText(/Pokemon/i); | ||
const pokemonImageElement = screen.getByRole("img", { name: /Pokemon/i }); | ||
|
||
expect(cardElement).toBeInTheDocument(); | ||
expect(pokemonNameElement).toBeInTheDocument(); | ||
expect(pokemonImageElement).toBeInTheDocument(); | ||
}); | ||
|
||
/* NB: I have opted not to test that event handler _has_ been clicked on event, because this will handled in testing functionality once clicked at the App.test.jsx level */ | ||
|
||
it("should not call event handler prop if mousedown event has not happened, nor when Card component is loading", async () => { | ||
const mockHandlerFunction = vi.fn(); | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<Card | ||
handleCardSelection={mockHandlerFunction} | ||
pokemonData={MOCK_POKEMON_DATA} | ||
isLoading={true} | ||
/> | ||
); | ||
|
||
expect(mockHandlerFunction).not.toHaveBeenCalled(); | ||
|
||
const cardElement = screen.getByRole("button", { name: /Pokemon/i }); | ||
await user.click(cardElement); | ||
expect(mockHandlerFunction).not.toHaveBeenCalled(); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { describe, it, vi, expect } from "vitest"; | ||
import { render, screen } from "@testing-library/react"; | ||
import Gameboard from "."; | ||
import useAllPokemon from "../../hooks/useAllPokemon"; | ||
|
||
vi.mock("../../hooks/useAllPokemon", () => ({ default: vi.fn() })); | ||
|
||
describe("Gameboard component", () => { | ||
it("displays the loading screen when data is loading", () => { | ||
useAllPokemon.mockReturnValue({ | ||
pokemons: [], | ||
error: null, | ||
isLoading: true, | ||
}); | ||
|
||
render(<Gameboard incrementScore={() => {}} resetScore={() => {}} />); | ||
|
||
const loadingScreenElement = screen.getByRole("status", { | ||
name: /Loading/i, | ||
}); | ||
|
||
expect(loadingScreenElement).toBeInTheDocument(); | ||
}); | ||
|
||
it("does not display Card components when data is loading", () => { | ||
useAllPokemon.mockReturnValue({ | ||
pokemons: [], | ||
error: null, | ||
isLoading: true, | ||
}); | ||
|
||
render(<Gameboard incrementScore={() => {}} resetScore={() => {}} />); | ||
|
||
const cardElements = screen.queryByRole("button", { | ||
name: /Select Pokemon/i, | ||
}); | ||
|
||
expect(cardElements).toBeNull(); | ||
}); | ||
|
||
it("displays Card components of Pokemons once useAllPokemon hook has finished loading", () => { | ||
const MOCK_POKEMON_DATA_ARRAY = [ | ||
{ id: 1, name: "Pokemon1", imageUrl: "https://pokemon1.png" }, | ||
{ id: 2, name: "Pokemon2", imageUrl: "https://pokemon2.jpg" }, | ||
]; | ||
useAllPokemon.mockReturnValue({ | ||
pokemons: MOCK_POKEMON_DATA_ARRAY, | ||
error: null, | ||
isLoading: false, | ||
}); | ||
|
||
render(<Gameboard incrementScore={() => {}} resetScore={() => {}} />); | ||
|
||
expect( | ||
screen.getAllByRole("button", { name: /Select Pokemon/i }) | ||
).toHaveLength(MOCK_POKEMON_DATA_ARRAY.length); | ||
expect( | ||
screen.getByRole("button", { name: /Pokemon1/i }) | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByRole("button", { name: /Pokemon2/i }) | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
// selecting Pokemon card should update state and regenerate Pokemon displayed | ||
// selecting previously-selected Pokemon should reset score and board | ||
// verify error handling logic | ||
// mock useAllPokemon hook behaviour (simulate various states) |
File renamed without changes.
10 changes: 5 additions & 5 deletions
10
src/components/Gameboard.jsx → src/components/Gameboard/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { render, screen } from "@testing-library/react"; | ||
import LoadingScreen from "."; | ||
|
||
describe("Loading screen component", () => { | ||
it("renders the container article", () => { | ||
render(<LoadingScreen />); | ||
expect( | ||
screen.getByRole("status", { name: /Loading/i }) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders the loading image", () => { | ||
render(<LoadingScreen />); | ||
|
||
const loadingImage = screen.getByRole("img", { | ||
name: "Loading animation", | ||
}); | ||
|
||
expect(loadingImage).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders text to advise loading state", () => { | ||
render(<LoadingScreen />); | ||
expect(screen.getByText(/.../)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import masterBall from "../../assets/masterball.png"; | ||
import styles from "./loadingScreen.module.css"; | ||
|
||
const LoadingScreen = () => ( | ||
<article | ||
className={styles.loadingScreen} | ||
role="status" | ||
aria-busy="true" | ||
aria-label="Loading Pokémon data" | ||
> | ||
<img | ||
className={styles.masterball} | ||
src={masterBall} | ||
alt="Loading animation" | ||
/> | ||
<p className={styles.text}>Catching them all...</p> | ||
</article> | ||
); | ||
|
||
export default LoadingScreen; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { getRandomPokemonIds } from "."; | ||
|
||
describe.each(Array.from({ length: 5 }, () => getRandomPokemonIds()))( | ||
"Helper function to generate random Pokemon IDs", | ||
() => { | ||
it("never generates any duplicate IDs", (randomPokemonIds) => { | ||
const isArrayWithUniqueElements = (arr) => | ||
Array.isArray(arr) && new Set(arr).size === arr.length; | ||
|
||
expect(isArrayWithUniqueElements(randomPokemonIds)).toBeTruthy; | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters