React Testing Library
RTL ka, CORE, PHILOSOPHY hai — "The more your tests resemble the way your software is used, the more confidence they can give you". render(), COMPONENT, KO, EK, VIRTUAL, DOM, MEIN, RENDER, KARTA, hai. screen.getByText(), screen.getByRole() JAISE, QUERIES, SE, ELEMENTS, "USER, KI, TARAH" DHOONDHE, JAATE, HAIN.
fireEvent, YA, userEvent (MORE, REALISTIC), SE, USER, INTERACTIONS, (CLICK, TYPE) SIMULATE, KIYE, JAATE, HAIN. Internal, STATE, VARIABLES, YA, COMPONENT, METHODS, KO, DIRECTLY, TEST, KARNA, RTL, KI, PHILOSOPHY, KE, KHILAAF, hai.
import { render, screen, fireEvent } from "@testing-library/react";
import Counter from "./Counter";
test("increments count when button clicked", () => {
render(<Counter />);
const button = screen.getByRole("button", { name: /increment/i });
fireEvent.click(button);
expect(screen.getByText("Count: 1")).toBeInTheDocument();
});- RTL, philosophy = component, ko, "USER, ki, tarah", TEST, karo
- render() + screen.getByRole/getByText = ELEMENTS, ko, DHOONDO
- Internal, state/methods, ko, DIRECTLY, test, karna, RTL, ke, khilaaf, hai
AGAR, COMPONENTS, ko, Context Providers (jaise, ThemeProvider) ki, ZAROORAT, hai, EK, CUSTOM, render FUNCTION, BANANA, COMMON, hai, JO, RTL, ke, render() KO, WRAP, KARTA, hai — HAR, TEST, MEIN, MANUALLY, Providers, WRAP, KARNE, ki, ZAROORAT, NAHI.
function renderWithProviders(ui) {
return render(<ThemeProvider>{ui}</ThemeProvider>);
}findByText(), findByRole() JAISI, QUERIES, EK, Promise, RETURN, KARTI, HAIN — ELEMENT, KE, APPEAR, HONE, TAK, WAIT, KARTI, HAIN (INTERNALLY, waitFor, USE, KARTI, HAIN). ASYNC, DATA, (jaise, API, RESPONSE, KE, BAAD, DIKHNE, WALA, TEXT) KE, LIYE, IDEAL.