|
| 1 | +import React from "react"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import App from "./App"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | + |
| 6 | +describe("Some HTML Elements are added.", () => { |
| 7 | + test("(2 pts) There is a header", () => { |
| 8 | + render(<App />); |
| 9 | + const header = screen.getByRole("heading"); |
| 10 | + expect(header).toBeInTheDocument(); |
| 11 | + }); |
| 12 | + |
| 13 | + test("(2 pts) There is an image with alt text", () => { |
| 14 | + render(<App />); |
| 15 | + const image = screen.getByRole("img"); |
| 16 | + expect(image).toBeInTheDocument(); |
| 17 | + expect(image).toHaveAttribute("alt"); |
| 18 | + }); |
| 19 | + |
| 20 | + test("(2 pts) There is a list with at least three elements", () => { |
| 21 | + render(<App />); |
| 22 | + const list = screen.getByRole("list"); |
| 23 | + expect(list).toBeInTheDocument(); |
| 24 | + expect(list.children.length).toBeGreaterThanOrEqual(3); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +describe("(2 pts) Some basic CSS is added.", () => { |
| 29 | + test("The background color of the header area is different", () => { |
| 30 | + render(<App />); |
| 31 | + const banner = screen.getByRole("banner"); |
| 32 | + expect(banner).not.toHaveStyle({ |
| 33 | + "background-color": "rgb(40, 44, 52)", |
| 34 | + }); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("(2 pts) Some Bootstrap Elements are added", () => { |
| 39 | + test("There is one bootstrap button with the text 'Log Hello World'", () => { |
| 40 | + render(<App />); |
| 41 | + const button = screen.getByRole("button", { name: /Log Hello World/i }); |
| 42 | + expect(button).toBeInTheDocument(); |
| 43 | + expect(button).toHaveClass("btn"); |
| 44 | + expect(button).toHaveClass("btn-primary"); |
| 45 | + }); |
| 46 | + |
| 47 | + test("(2 pts) Not clicking the bootstrap button does not logs 'Hello World!'", () => { |
| 48 | + const consoleSpy = jest.spyOn(console, "log"); |
| 49 | + render(<App />); |
| 50 | + expect(consoleSpy).not.toHaveBeenCalledWith("Hello World!"); |
| 51 | + }); |
| 52 | + |
| 53 | + test("(2 pts) Clicking the bootstrap button logs 'Hello World!'", () => { |
| 54 | + const consoleSpy = jest.spyOn(console, "log"); |
| 55 | + render(<App />); |
| 56 | + const button = screen.getByRole("button", { name: /Log Hello World/i }); |
| 57 | + userEvent.click(button); |
| 58 | + expect(consoleSpy).toHaveBeenCalledWith("Hello World!"); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("Some additional CSS was added", () => { |
| 63 | + test("(2 pts) checks if any element has a background color of red", () => { |
| 64 | + const { container } = render(<App />); |
| 65 | + // Get all elements in the rendered container |
| 66 | + const elements = container.querySelectorAll("*"); |
| 67 | + |
| 68 | + // Check if any element has a background color of red |
| 69 | + let foundRedBackground = false; |
| 70 | + |
| 71 | + elements.forEach((element) => { |
| 72 | + const style = getComputedStyle(element); |
| 73 | + if ( |
| 74 | + style.backgroundColor === "red" || |
| 75 | + style.backgroundColor === "rgb(255, 0, 0)" |
| 76 | + ) { |
| 77 | + foundRedBackground = true; |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + expect(foundRedBackground).toBe(true); |
| 82 | + }); |
| 83 | +}); |
0 commit comments