Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added testing for app #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/app.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import App from './App';


const mockWeatherData = {
weather: [{ description: 'Clear sky' }],
};

beforeAll(() => {

global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve(mockWeatherData),
});
});

afterAll(() => {

global.fetch.mockRestore();
});

test('renders weather information', async () => {
render(<App />);


await waitFor(() => expect(screen.getByText(/The current weather is/i)).toBeInTheDocument());
expect(screen.getByText(/Clear sky/i)).toBeInTheDocument();
});

test('fetches weather data from API', async () => {
render(<App />);


await waitFor(() => expect(global.fetch).toHaveBeenCalled());
expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('api.openweathermap.org'));
});

test('displays loading indicator while fetching weather data', async () => {
render(<App />);

expect(screen.getByText(/loading/i)).toBeInTheDocument();

await waitFor(() => expect(screen.queryByText(/loading/i)).not.toBeInTheDocument());
});

test('handles API fetch error', async () => {
global.fetch.mockResolvedValueOnce({ ok: false });

render(<App />);

await waitFor(() => expect(screen.getByText(/Failed to fetch weather data/i)).toBeInTheDocument());
});