Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from "react";
import "./App.scss";
import { Link, Route, BrowserRouter as Router, Routes } from "react-router";
import ProfilePage from "./components/ProfilePage";
import PhotoOfTheDay from "./Components/photoOfTheDay/photoOfTheDay";
// import ProfilePage from "./Components/ProfilePage";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments!

// import PhotoOfTheDay from "./Components/photoOfTheDay/photoOfTheDay";
import RoverImages from "./Components/RoverImages/RoverImages";

function App() {
return (
<Router>
<Routes>
<Route path="/" element={<PhotoOfTheDay />} />
<Route path="/profile" element={<ProfilePage />} />
{/* <Route path="/" element={<PhotoOfTheDay />} />
<Route path="/profile" element={<ProfilePage />} /> */}
<Route path="/" element={<RoverImages name="curiosity" />} />
<Route
path="*"
element={
Expand Down
55 changes: 53 additions & 2 deletions src/Components/RoverImages/RoverImage.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ const photo_mock_data = {
],
};

const photo_mock_data_cameras = {
photos: [
{
id: 1,
img_src: "https://mars.nasa.gov/3",
},
],
};

const manifest_mock_data = {
photo_manifest: {
photos: [
{
cameras: ["HAZ"],
cameras: ["HAZ", "CHEM"],
},
],
},
};

describe("Testing the rover image displayed on page load", () => {
// const mock_cameras = ["HAZ", "CHEM"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment


describe("Rover Image Tests", () => {
beforeEach(() => {
global.fetch = jest
.fn()
Expand All @@ -34,6 +45,9 @@ describe("Testing the rover image displayed on page load", () => {
})
.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue(photo_mock_data),
})
.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue(photo_mock_data_cameras),
});
});
test("Testing the rover image displayed on page load", async () => {
Expand All @@ -60,4 +74,41 @@ describe("Testing the rover image displayed on page load", () => {
expect(firstTestImageDiv).toHaveAttribute("aria-hidden", "true");
});
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these commented out because they don't work yet?

// test("Test the drop down values are updated when selected", async () => {
// render(<RoverImages name="curiosity" />);
// await waitFor(() => {
// const dropdown = document.querySelector("select") as HTMLSelectElement;
// //fireEvent.change(dropdown, { target: { value: mock_cameras[1] } });
// fireEvent.click(dropdown);
// const dropdownOptions = document.querySelectorAll('option');
// console.log('options: ' + dropdownOptions[1].value);

// fireEvent.click(dropdownOptions[1]);
// expect(dropdown.value).toEqual(dropdownOptions[1].value);

// });
// });

// test("Test the images are updated when different values are selected from the dropdown", async () => {
// render(<RoverImages name="curiosity" />);
// await waitFor(() => {
// const testImage = document.querySelector("img") as HTMLImageElement;
// console.log(testImage);
// const firstImageSourceUrl = testImage.src;
// console.log(firstImageSourceUrl)
// const dropdown = document.querySelector("select") as HTMLSelectElement;
// //fireEvent.change(dropdown, { target: { value: mock_cameras[1] } });
// fireEvent.click(dropdown);
// const dropdownOptions = document.querySelectorAll('option');
// console.log('options: ' + dropdownOptions[1].value);
// fireEvent.click(dropdownOptions[1]);

// // expect(dropdown.value).toBe(mock_cameras[1])
// const updatedImage = document.querySelector("img") as HTMLImageElement;
// const secondImageSourceUrl = updatedImage.src;
// console.log(secondImageSourceUrl)
// expect(firstImageSourceUrl).not.toEqual(secondImageSourceUrl);
// });
// });
});
44 changes: 37 additions & 7 deletions src/Components/RoverImages/RoverImages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ type RoverResponse = {
img_src: string;
};

type Cameras = Array<string>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you really need this, you can just use string[] throughout the code and it's just as clear


const api = "fCp5fNsscdDmov0Vw4lpU4bOkdMTCuCA9tnoKgYH";

function RoverImages(props: { name: string }) {
const [roverResponse, setRoverResponse] = useState<RoverResponse[]>();
const [error, setError] = useState<string>("");
const [isLoading, setLoading] = useState<boolean>(false);
const [latestCameras, setLatestCameras] = useState<Cameras>();
const [selectedValue, setSelectedValue] = useState<string>("");

const altText = `Photo taken by the Mars Rover ${props.name}`;

Expand All @@ -41,14 +45,29 @@ function RoverImages(props: { name: string }) {
`https://api.nasa.gov/mars-photos/api/v1/manifests/${props.name}?api_key=${api}`,
);
const manifestData = await manifestResponse.json();

const photoResponse = await fetch(
`https://api.nasa.gov/mars-photos/api/v1/rovers/${props.name}/photos?earth_date=${manifestData.photo_manifest.max_date}&api_key=${api}`,
setLatestCameras(
manifestData.photo_manifest.photos[
manifestData.photo_manifest.photos.length - 1
].cameras,
);
const photoData = await photoResponse.json();

setRoverResponse(photoData.photos);
setLoading(false);
if (!selectedValue) {
setLoading(true);
const photoResponse = await fetch(
`https://api.nasa.gov/mars-photos/api/v1/rovers/${props.name}/photos?earth_date=${manifestData.photo_manifest.max_date}&api_key=${api}`,
);
const photoData = await photoResponse.json();
setRoverResponse(photoData.photos);
setLoading(false);
} else {
setLoading(true);
const photoResponse = await fetch(
`https://api.nasa.gov/mars-photos/api/v1/rovers/${props.name}/photos?earth_date=${manifestData.photo_manifest.max_date}&camera=${selectedValue}&api_key=${api}`,
);
const photoData = await photoResponse.json();
setRoverResponse(photoData.photos);
setLoading(false);
}
} catch (error) {
setLoading(false);
setError(
Expand All @@ -58,7 +77,11 @@ function RoverImages(props: { name: string }) {
};

fetchData();
}, [props.name]);
}, [props.name, selectedValue]);

const handleChange: React.ChangeEventHandler<HTMLSelectElement> = (e) => {
setSelectedValue(e.target.value);
};

if (error) {
return (
Expand Down Expand Up @@ -90,6 +113,13 @@ function RoverImages(props: { name: string }) {
<img key={image.id} src={image.img_src} alt={altText} />
))}
</Slider>
<select id="dropdown" value={selectedValue} onChange={handleChange}>
{latestCameras?.map((camera, index) => (
<option key={index} value={camera}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to unselect a camera once one has been selected? Can I just show all photos?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that isn't in place yet. It defaults to one angle on page load and there are just the options to change angles.

{camera}
</option>
))}
</select>
</div>
);
}
Expand Down