Skip to content

Commit d1fb40f

Browse files
angelocordonlpatmo
andcommitted
Add Axios to utility query
Add query utility function using Axios. Co-authored-by: Linda Peng <linda.peng@alumni.duke.edu>
1 parent 4804ff3 commit d1fb40f

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/components/Resources/ResourcePage.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import { useQuery } from 'react-query';
44
import { Link } from 'react-router-dom';
5+
import { getResource } from '../../utils/queries';
56
import PersonalMenu from '../PersonalMenu';
67
import { Grid, Breadcrumbs, Typography, Chip, Box } from '@material-ui/core';
78
import NavigateNextIcon from '@material-ui/icons/NavigateNext';
@@ -15,13 +16,9 @@ const useStyles = makeStyles({
1516
});
1617

1718
function ResourcePage({ matchProps }) {
19+
const resourceId = matchProps.match.params.guid;
1820
// TODO: Handle Error cases
19-
const { isLoading, data } = useQuery('resourceData', () =>
20-
fetch(`/api/v1/resources/${matchProps.match.params.guid}`).then(res =>
21-
res.json()
22-
)
23-
);
24-
21+
const { isLoading, data } = useQuery(['resource', resourceId], getResource);
2522
const classes = useStyles();
2623

2724
if (isLoading) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import ResourcePage from './ResourcePage';
4+
5+
describe('ResourcePage', () => {});

src/utils/queries.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import axios from 'axios';
2+
3+
const API_URL = '/api/v1';
4+
5+
const getResource = async (_key, id) => {
6+
const { data } = await axios.get(`${API_URL}/resources/${id}`);
7+
return data;
8+
};
9+
10+
export { getResource };

src/utils/queries.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { getResource } from './queries';
2+
import axios from 'axios';
3+
4+
jest.mock('axios');
5+
6+
describe('getResource', () => {
7+
it('returns `data` object when successfully fetched', async () => {
8+
const data = {
9+
title: 'Just You Wait',
10+
url: 'www.manythingstodo.com',
11+
author: 'Alexander Hamilton',
12+
media_type: 'song',
13+
description: '3 hour epic ballad',
14+
user: 'harrypotter321',
15+
date_published: '10-11-2020',
16+
};
17+
18+
axios.get.mockImplementationOnce(() => Promise.resolve({ data }));
19+
20+
await expect(getResource('resourceQuery', 'resource-url')).resolves.toEqual(
21+
data
22+
);
23+
});
24+
25+
it('returns `error` object when unable to fetch', async () => {
26+
const errorMessage = 'No such resource';
27+
axios.get.mockImplementationOnce(() =>
28+
Promise.reject(new Error(errorMessage))
29+
);
30+
await expect(
31+
getResource('resourceQuery', 'a-very-wrong-url')
32+
).rejects.toThrow(errorMessage);
33+
});
34+
});

0 commit comments

Comments
 (0)