- {loading ? (
+ {isLoading ? (
) : (
diff --git a/src/pages/details/details.test.tsx b/src/pages/details/details.test.tsx
index 1a278679..622c5680 100644
--- a/src/pages/details/details.test.tsx
+++ b/src/pages/details/details.test.tsx
@@ -1,4 +1,5 @@
import axios from '@src/utils/axios';
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { act, render } from '@testing-library/react';
import MockAdapter from 'axios-mock-adapter';
import { AuthProvider } from 'react-oidc-context';
@@ -15,11 +16,14 @@ jest.mock('react-router-dom', () => ({
}));
describe('Details', () => {
+ const queryClient = new QueryClient();
const componentWrapper = (
-
+
+
+
@@ -28,17 +32,19 @@ describe('Details', () => {
const mock = new MockAdapter(axios);
beforeAll(() => {
mock.reset();
+ queryClient.setDefaultOptions({
+ queries: {
+ retry: false, // Disable retries for tests
+ },
+ });
});
- test('should render successfully', async () => {
- const { baseElement } = render(componentWrapper);
- await act(async () => {
- expect(baseElement).toBeTruthy();
- });
+ beforeEach(() => {
+ queryClient.clear();
});
- test('should render with mock data', async () => {
- mock.onGet().reply(200, launchData[0]);
+ test('should render successfully', async () => {
+ mock.onGet(new RegExp('/*/?format=json')).reply(200, launchData[0]);
jest.spyOn(useAuthMock, 'default').mockReturnValue({
isSignedIn: true,
currentUserData: {} as User,
@@ -57,7 +63,7 @@ describe('Details', () => {
});
test('should render with error', async () => {
- mock.onGet().networkError();
+ mock.onGet(new RegExp('/*/?format=json')).networkError();
const { baseElement } = render(componentWrapper);
await act(async () => {
expect(baseElement).toBeTruthy();
diff --git a/src/pages/details/details.tsx b/src/pages/details/details.tsx
index e7da8037..9ac5f1d5 100644
--- a/src/pages/details/details.tsx
+++ b/src/pages/details/details.tsx
@@ -1,29 +1,24 @@
import { Spinner } from '@metrostar/comet-extras';
import { Card } from '@metrostar/comet-uswds';
-import React, { useEffect, useState } from 'react';
+import axios from '@src/utils/axios';
+import { useQuery } from '@tanstack/react-query';
+import React from 'react';
import { useParams } from 'react-router-dom';
import ErrorNotification from '../../components/error-notification/error-notification';
-import useApi from '../../hooks/use-api';
import useAuth from '../../hooks/use-auth';
import { Launch } from '../../types/launch';
export const Details = (): React.ReactElement => {
const { id } = useParams();
const { isSignedIn } = useAuth();
- const { getItem, item, loading, error } = useApi();
- const [data, setData] = useState
(null);
-
- useEffect(() => {
- if (isSignedIn && id) {
- getItem(id);
- }
- }, [isSignedIn, id, getItem]);
-
- useEffect(() => {
- if (item) {
- setData(item);
- }
- }, [item]);
+ const { isLoading, error, data } = useQuery({
+ queryKey: ['launches', id],
+ queryFn: () =>
+ axios.get(`/${id}/?format=json`).then((response) => {
+ return response.data;
+ }),
+ enabled: isSignedIn && !!id,
+ });
return (
@@ -36,13 +31,13 @@ export const Details = (): React.ReactElement => {
{error && (
)}
- {loading ? (
+ {isLoading ? (