|
1 | 1 | import React from "react"; |
2 | | -import { render, screen } from "@testing-library/react"; |
| 2 | +import { getByText, render, screen, waitFor } from "@testing-library/react"; |
3 | 3 | import App from "../../App"; |
4 | | -import MissionManifest from "./MissionManifest"; |
| 4 | +import MissionManifest, { rovers } from "./MissionManifest"; |
5 | 5 |
|
6 | | -test("renders learn react link", () => { |
7 | | - render(<App />); |
8 | | - const linkElement = screen.getByText(/learn react/i); |
9 | | - expect(linkElement).toBeInTheDocument(); |
10 | | -}); |
| 6 | + |
| 7 | +const mockManifestResponse = { |
| 8 | + photo_manifest: { |
| 9 | + name: "Opportunity", |
| 10 | + landing_date: "2004-01-25", |
| 11 | + launch_date: "2003-07-07", |
| 12 | + status: "complete", |
| 13 | + max_sol: 5111, |
| 14 | + max_date: "2018-06-11", |
| 15 | + total_photos: 198439 |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + global.fetch = jest.fn().mockResolvedValue({ |
| 21 | + json: jest.fn().mockResolvedValue(mockManifestResponse), |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | +// Ensures component renders a loading state before data arrives |
| 26 | +test("renders loading message on initial render", () => { |
| 27 | + render(<MissionManifest roverType={rovers.SPIRIT}/>); |
| 28 | + expect(screen.getByText('Loading...')).toBeInTheDocument(); |
| 29 | +}) |
| 30 | + |
| 31 | +test("opportunity rover renders correct p elements", async () => { |
| 32 | + render(<MissionManifest roverType={rovers.OPP}/>); |
| 33 | + const expectedTexts = ["Rover Name: ", mockManifestResponse.photo_manifest.name, "Landing Date: ", mockManifestResponse.photo_manifest.landing_date, "Launch Date: ", mockManifestResponse.photo_manifest.launch_date, "Status: ", mockManifestResponse.photo_manifest.status, "Max Sol: ", mockManifestResponse.photo_manifest.max_sol.toString(), "Max Date: ", mockManifestResponse.photo_manifest.max_date, "Total photos: ", mockManifestResponse.photo_manifest.total_photos.toString()]; |
| 34 | + |
| 35 | + const missionManifestContainer = await screen.findByTestId('mission-manifest-container'); |
| 36 | + const pElements = missionManifestContainer.querySelectorAll('p'); |
| 37 | + |
| 38 | + expect(pElements.length).toBe(14); |
| 39 | + pElements.forEach((p, index) => { |
| 40 | + expect(p.textContent).toBe(expectedTexts[index]); |
| 41 | + console.log(p.textContent); |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +test("opportunity rover renders correct Heading", async () => { |
| 46 | + render(<MissionManifest roverType={rovers.OPP}/>); |
| 47 | + |
| 48 | + const missionManifestContainer = await screen.findByTestId('mission-manifest-container'); |
| 49 | + const heading = screen.getByText('MISSION MANIFEST'); |
| 50 | + |
| 51 | + expect(heading).toBeInTheDocument(); |
| 52 | +}) |
| 53 | + |
| 54 | +test("invalid rover renders correct p elements", async () => { |
| 55 | + |
| 56 | + const mockInvalidFetchResponse = { |
| 57 | + photo_manifest: { |
| 58 | + name: undefined, |
| 59 | + landing_date: undefined, |
| 60 | + launch_date: undefined, |
| 61 | + status: undefined, |
| 62 | + max_sol: undefined, |
| 63 | + max_date: undefined, |
| 64 | + total_photos: undefined |
| 65 | + } |
| 66 | + } |
| 67 | + global.fetch = jest.fn().mockResolvedValue({ |
| 68 | + json: jest.fn().mockResolvedValue(mockInvalidFetchResponse), |
| 69 | + }); |
| 70 | + |
| 71 | + render(<MissionManifest roverType={rovers.OPP}/>); |
| 72 | + |
| 73 | + const missionManifestContainer = await screen.findByTestId('mission-manifest-container'); |
| 74 | + const pElements = missionManifestContainer.querySelectorAll('p'); |
| 75 | + let NaCounter = 0; |
| 76 | + |
| 77 | + pElements.forEach((p) => { |
| 78 | + if(p.textContent === "N/A") NaCounter++ |
| 79 | + }) |
| 80 | + |
| 81 | + expect(NaCounter).toBe(7); |
| 82 | +}) |
0 commit comments