What could be a simple test case to start with?
How about the empty bag?
const component = render(...)
expect(
component.getByText('No Pokemons yet.')
).not.toBeNull();
...make it green!
Add some props to the component
render(<PokeBag pokemons={['a', 'b']} />);
Remember, you can use regular JS inside JSX.
<ul>
{pokemons.map(p => (
<li>{pokemon}</li>
))}
</ul>
or simply change detection (CD)
This is an O(n3) algorithm, which is not suitable for web applications.
React uses heuristics to make it an O(n) algorithm.
The heuristic relevant for us is the key attribute.
It lets React know if an element is still the same or not.
<ul>
{pokemons.map(p => (
<li key={pokemon}>{pokemon}</li>
))}
</ul>
Add the new (and tested) pokebag component to the App and add Pokemons to it on a correct guess.
Where do we need to put the state containing the Pokemon?
The PokeBag component doesn't look very nice. Let's extract a component for the List.
<h3>Poke Bag</h3>
<PokeBagList pokemons={pokemons} />
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
App.tsx
const PokeBagContext = React.createContext({
pokemons: [],
addPokemon: () => {},
});
Wrap your component in App.tsx
<PokeBagContext.Provider
value={{
pokemons: ...,
}}
>
...
</PokeBagContext.Provider>
const { pokemons } = useContext(PokeBagContext);
We now no longer need to keep passing props to the components.