import { useState, useEffect } from “react”; import axios from “axios”; const…
- User
import { useState, useEffect } from “react”; import axios from “axios”; const randomJokeApi = “https://api.chucknorris.io/jokes/random”; const JokePart = () => { const [joke, setJoke] = useState(); const randomJokeGenerator = async () => { try { const resp = await axios.get(randomJokeApi); setJoke(resp.data.value); } catch (err) { console.error(err); } }; useEffect(() => { randomJokeGenerator(); }, []); return (
export default JokePart; write test in jest, test for only showning the joke
- Assistant
import { render, screen, fireEvent } from '@testing-library/react'; import axiosMock from 'axios';
import JokePart from './JokePart';
jest.mock('axios');
describe('JokePart component', () => { beforeEach(() => { axiosMock.get.mockResolvedValueOnce({ data: { value: 'Chuck Norris can divide by zero.' } }); render(); });
test('displays the joke after loading', async () => { const jokeElement = await screen.findByText('Chuck Norris can divide by zero.'); expect(jokeElement).toBeInTheDocument(); });
test('fetches a new joke when "New Joke" button is clicked', async () => { axiosMock.get.mockResolvedValueOnce({ data: { value: 'Chuck Norris can do a wheelie on a unicycle.' } });
const button = screen.getByRole('button', { name: 'New Joke' }); fireEvent.click(button); const jokeElement = await screen.findByText('Chuck Norris can do a wheelie on a unicycle.'); expect(jokeElement).toBeInTheDocument();});
test('does not display the title', () => { const titleElement = screen.queryByText('Joke Generator which uses Axios'); expect(titleElement).not.toBeInTheDocument(); }); });
- User
but there are random jokes
- Assistant
You can write a test to check that the joke changes when the "New Joke" button is clicked. Here's an updated version of the previous test suite that includes this test:
import { render, screen, fireEvent } from '@testing-library/react'; import axiosMock from 'axios'; import JokePart from './JokePart'; jest.mock('axios'); describe('JokePart component', () => { beforeEach(() => { axiosMock.get.mockResolvedValueOnce({ data: { value: 'Chuck Norris can divide by zero.' } }); render(<JokePart />); }); test('displays the initial joke after loading', async () => { const jokeElement = await screen.findByText('Chuck Norris can divide by zero.'); expect(jokeElement).toBeInTheDocument(); }); test('fetches a new joke and displays it when "New Joke" button is clicked', async () => { axiosMock.get.mockResolvedValueOnce({ data: { value: 'Chuck Norris can do a wheelie on a unicycle.' } }); const button = screen.getByRole('button', { name: 'New Joke' }); fireEvent.click(button); const jokeElement = await screen.findByText('Chuck Norris can do a wheelie on a unicycle.'); expect(jokeElement).toBeInTheDocument(); }); test('does not display the title', () => { const titleElement = screen.queryByText('Joke Generator which uses Axios'); expect(titleElement).not.toBeInTheDocument(); }); test('changes the joke when "New Joke" button is clicked', async () => { axiosMock.get.mockResolvedValueOnce({ data: { value: 'Chuck Norris can hear sign language.' } }); const initialJoke = await screen.findByText('Chuck Norris can divide by zero.'); const button = screen.getByRole('button', { name: 'New Joke' }); fireEvent.click(button); const newJoke = await screen.findByText('Chuck Norris can hear sign language.'); expect(initialJoke).not.toBe(newJoke); }); });