Skip to content

Commit 23aa655

Browse files
author
Hannah Dunsmore
committed
added unit tests
1 parent 85ffbc3 commit 23aa655

File tree

4 files changed

+97
-30
lines changed

4 files changed

+97
-30
lines changed

src/App.test.tsx

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React from "react";
22
import "./App.scss";
33
import {useState} from 'react';
4-
import MissionManifest from "./Components/MissionManifest/MissionManifest";
4+
import MissionManifest, { rovers } from "./Components/MissionManifest/MissionManifest";
55

66

77

88
function App() {
9-
const [rover, setRover] = useState("spirit");
9+
const [rover, setRover] = useState<rovers>(rovers.SPIRIT);
1010

1111
return (
12-
<MissionManifest roverName={rover} />
13-
);
12+
<MissionManifest roverType={rover} />
13+
)
1414
}
1515

1616
export default App;
Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,82 @@
11
import React from "react";
2-
import { render, screen } from "@testing-library/react";
2+
import { getByText, render, screen, waitFor } from "@testing-library/react";
33
import App from "../../App";
4-
import MissionManifest from "./MissionManifest";
4+
import MissionManifest, { rovers } from "./MissionManifest";
55

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+
})

src/Components/MissionManifest/MissionManifest.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ import { config } from 'dotenv';
44
import { json } from 'stream/consumers';
55
import "./MissionManifest.scss";
66

7-
type RoverType = {
8-
roverName: string;
7+
export enum rovers {
8+
CURIOSITY = 'curiosity',
9+
OPP = 'opportunity',
10+
SPIRIT = 'spirit'
11+
};
12+
13+
export interface MissionManifestProps {
14+
roverType: rovers;
915
}
1016

1117
type manifestData = {
@@ -18,15 +24,14 @@ type manifestData = {
1824
totalPhotos: number
1925
}
2026

21-
const rovers = ['curiosity', 'opportunity', 'spirit'];
2227

23-
function MissionManifest(props: RoverType) {
28+
29+
function MissionManifest(props: MissionManifestProps) {
2430
const apiKey = process.env.REACT_APP_API_KEY;
25-
const [manifestData, setManifestData] = useState({} as manifestData);
31+
const [manifestData, setManifestData] = useState<manifestData | null>(null);
2632

2733
useEffect(() => {
28-
if (rovers.includes(props.roverName.toLowerCase())) {
29-
fetch(`https://api.nasa.gov/mars-photos/api/v1/manifests/${props.roverName}?api_key=${apiKey}`).then(response => response.json()).then((response) => {
34+
fetch(`https://api.nasa.gov/mars-photos/api/v1/manifests/${props.roverType}?api_key=${apiKey}`).then(response => response.json()).then((response) => {
3035
let data = response.photo_manifest;
3136
setManifestData({
3237
name: data.name,
@@ -38,15 +43,14 @@ function MissionManifest(props: RoverType) {
3843
totalPhotos: data.total_photos
3944
})
4045
});
41-
}
42-
}, [props.roverName]);
46+
}, [props.roverType]);
4347

4448

4549
if (!manifestData) {
4650
return <p>Loading...</p>
4751
} else {
4852
return (
49-
<div id='mission-manifest-container'>
53+
<div id='mission-manifest-container' data-testid="mission-manifest-container">
5054
<h2>MISSION MANIFEST</h2>
5155
<div id='rover-mission-info-container'>
5256
<p className='manifest-fieldname'>Rover Name: </p>

0 commit comments

Comments
 (0)