Skip to content

Commit

Permalink
Merge pull request #42 from TiagoM13/fix/tests
Browse files Browse the repository at this point in the history
fix tests and add new ones
  • Loading branch information
TiagoM13 authored May 28, 2023
2 parents 0fe866d + f3924cf commit ecdf47b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 7 deletions.
15 changes: 14 additions & 1 deletion _tests_/pages/pokedex.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ import { BrowserRouter } from 'react-router-dom';

import { Pokedex } from '@pages';
import { render, screen } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

describe('<Pokedex />', () => {
it('should render the page Pokedex', () => {
it('should render the page Pokedex', async () => {
const mockPokemons = [
{ id: 1, name: 'Pikachu' },
{ id: 2, name: 'Charmander' },
];

const mock = new MockAdapter(axios);
mock.onGet().reply(200, mockPokemons);

render(
<>
<BrowserRouter>
<Pokedex />
</BrowserRouter>
</>
);

await screen.findByText('Select your pokemon');

expect(screen.getByText('Select your pokemon')).toBeInTheDocument();
});
});
53 changes: 50 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@babel/preset-typescript": "^7.21.5",
"@jest/globals": "^29.5.0",
"axios": "^1.2.2",
"axios-mock-adapter": "^1.21.4",
"phosphor-react": "^1.4.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
36 changes: 36 additions & 0 deletions src/hooks/_tests_/usePokemons.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable import/extensions */
import { renderHook, act } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

import { useGetPokemonsData } from '../usePokemons';

const mock = new MockAdapter(axios);
describe('useGetPokemonsData', () => {
beforeEach(() => {
mock.reset(); // Reseta o mock antes de cada teste
});

it('should fetch and set pokemons data', async () => {
const mockPokemons = [
{ id: 1, name: 'Pikachu' },
{ id: 2, name: 'Charmander' },
// Adicione mais objetos de pokemons mockados, se necessário
];

// mock.onGet(process.env.VITE_URL_API).reply(200, mockPokemons);
const mock = new MockAdapter(axios);
mock.onGet(process.env.VITE_URL_API).reply(200, mockPokemons);

const { result } = renderHook(() => useGetPokemonsData());

expect(result.current.loading).toBe(true);

await act(async () => {
await result.current.getPokemons(); // Chame a função getPokemons diretamente
});

expect(result.current.loading).toBe(false);
expect(mockPokemons).toEqual(mockPokemons);
});
});
2 changes: 1 addition & 1 deletion src/hooks/usePokemons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ export const useGetPokemonsData = () => {
return setPokemons(filteredPokemons);
};

return { pokemons, loading, FilterPokemon };
return { pokemons, loading, FilterPokemon, getPokemons };
};
17 changes: 15 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,15 @@
"resolved" "https://registry.npmjs.org/axe-core/-/axe-core-4.6.2.tgz"
"version" "4.6.2"

"axios@^1.2.2":
"axios-mock-adapter@^1.21.4":
"integrity" "sha512-ztnENm28ONAKeRXC/6SUW6pcsaXbThKq93MRDRAA47LYTzrGSDoO/DCr1NHz7jApEl95DrBoGPvZ0r9xtSbjqw=="
"resolved" "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.21.4.tgz"
"version" "1.21.4"
dependencies:
"fast-deep-equal" "^3.1.3"
"is-buffer" "^2.0.5"

"axios@^1.2.2", "axios@>= 0.17.0":
"integrity" "sha512-bz/J4gS2S3I7mpN/YZfGFTqhXTYzRho8Ay38w2otuuDR322KzFIWm/4W2K6gIwvWaws5n+mnb7D1lN9uD+QH6Q=="
"resolved" "https://registry.npmjs.org/axios/-/axios-1.2.2.tgz"
"version" "1.2.2"
Expand Down Expand Up @@ -3225,7 +3233,7 @@
"jest-message-util" "^29.5.0"
"jest-util" "^29.5.0"

"fast-deep-equal@^3.1.1":
"fast-deep-equal@^3.1.1", "fast-deep-equal@^3.1.3":
"integrity" "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="
"resolved" "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz"
"version" "3.1.3"
Expand Down Expand Up @@ -3761,6 +3769,11 @@
"call-bind" "^1.0.2"
"has-tostringtag" "^1.0.0"

"is-buffer@^2.0.5":
"integrity" "sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ=="
"resolved" "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.5.tgz"
"version" "2.0.5"

"is-callable@^1.1.3", "is-callable@^1.1.4", "is-callable@^1.2.7":
"integrity" "sha512-1BC0BVFhS/p0qtw6enp8e+8OD0UrK0oFLztSjNzhcKA3WDuJxxAPXzPuPtKkjEY9UUoEWlX/8fgKeu2S8i9JTA=="
"resolved" "https://registry.npmjs.org/is-callable/-/is-callable-1.2.7.tgz"
Expand Down

0 comments on commit ecdf47b

Please sign in to comment.