Skip to content

Instantly share code, notes, and snippets.

@nemrosim
Created March 27, 2022 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemrosim/9ce1cb06a56f7ad50a4beb8eefe40ba6 to your computer and use it in GitHub Desktop.
Save nemrosim/9ce1cb06a56f7ad50a4beb8eefe40ba6 to your computer and use it in GitHub Desktop.
import React from 'react';
import { render, screen } from '@testing-library/react';
import { App } from './App';
it('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
it('should render root div', () => {
const { container } = render(<App />);
const divElement = container.firstChild as HTMLDivElement;
expect(divElement.classList.contains('App')).toBe(true);
expect(divElement.classList.contains('App')).toBeTruthy();
expect(divElement).toHaveClass('App');
});
it('should render header', () => {
const { container } = render(<App />);
// Why banner? https://www.w3.org/TR/html-aria/#docconformance
const header = screen.getByRole('banner');
expect(header).toBeInTheDocument();
expect(header).toHaveClass('App-header');
const parentDiv = container.firstChild as HTMLDivElement;
expect(header.parentElement).toContainElement(parentDiv);
const childImage = screen.getByRole('img');
expect(header.firstElementChild).toContainElement(childImage);
});
it('should render image', () => {
render(<App />);
const img = screen.getByRole('img');
expect(img).toBeInTheDocument();
expect(img).toHaveClass('App-logo');
expect(img).toHaveAttribute('src', 'logo.svg');
expect(img).toHaveAttribute('alt', 'logo');
const sameImage = screen.getByAltText('logo');
expect(sameImage).toBeInTheDocument();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment