Skip to content

Commit

Permalink
Merge pull request #5 from ShadowDraco/finishing
Browse files Browse the repository at this point in the history
fix quiet unhandled rejection errors
  • Loading branch information
Ethan Storm committed Jul 12, 2023
2 parents e92c60b + dac2871 commit 94bc235
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 38 deletions.
9 changes: 7 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CartPage from './components/CartPage';
import StoreFront from './components/StoreFront';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';

function App() {
function App({ testProducts, testCategories }) {
return (
<Box>
<Header />
Expand All @@ -19,7 +19,12 @@ function App() {
<Routes>
<Route
path='/'
element={<StoreFront />}
element={
<StoreFront
testProducts={testProducts}
testCategories={testCategories}
/>
}
/>
<Route
path='/cart'
Expand Down
4 changes: 2 additions & 2 deletions src/components/ActiveProduct/ActiveProduct.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ test('renders selected product card', () => {
const product = {
name: 'TV',
description: 'See all the things',
url: `${import.meta.env.VITE_UNSPLASH_URL}tv`,
url: `https://source.unsplash.com/random?tv`,
price: 600,
inventory: Math.random() * 20 + 10,
inStock: Math.random() * 20 + 10,
};
render(
<Provider store={store}>
Expand Down
18 changes: 12 additions & 6 deletions src/components/ActiveProduct/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,25 @@ export default function ActiveProduct({ product }) {
const params = useParams();

useEffect(() => {
dispatch(setCurrentProduct()).then(data =>
dispatch(
getCurrentProduct({ results: data.payload.results, id: params.id })
)
);
if (typeof product === undefined) {
console.log('\n\nNO TEST ACTIVE PRODUCT ', product);
try {
dispatch(setCurrentProduct()).then(data =>
dispatch(
getCurrentProduct({ results: data.payload.results, id: params.id })
)
);
} catch (error) {
console.error('Error in ActiveProduct UseEffect', error);
}
}
}, [params.id]);

const currentProduct = product
? product
: useSelector(state => state.products.currentProduct);
const dispatch = useDispatch();


return (
currentProduct && (
<Box mb={10}>
Expand Down
7 changes: 2 additions & 5 deletions src/components/Categories/Categories.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { render, screen, fireEvent } from '@testing-library/react';
import Categories from '../Categories';
import store from '../../redux/store';
import { Provider } from 'react-redux';
import { initialCategories } from '../../redux/reducers/categories';

test('renders main title', () => {
const mockFunction = parameter => {
return parameter;
};

render(
<Provider store={store}>
<Categories setChosenCategory={mockFunction} />
<Categories testCategories={initialCategories} />
</Provider>
);

Expand Down
19 changes: 14 additions & 5 deletions src/components/Categories/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ import { ButtonGroup, Button, Box, Typography, Container } from '@mui/material';
import PropTypes from 'prop-types';
import { useEffect } from 'react';

export default function Categories() {
export default function Categories({ testCategories }) {
const chosenCategory = useSelector(state => state.chosenCategory);
const categories = useSelector(state => state.categories);
const categories = testCategories
? testCategories
: useSelector(state => state.categories);
const dispatch = useDispatch();

useEffect(() => {
dispatch(fetchCategories()).then(data => {
dispatch(updateCategories(data));
});
if (typeof testCategories === undefined) {
console.log('\n\nNO TEST CATEGORY ', testCategories);
try {
dispatch(fetchCategories()).then(data => {
dispatch(updateCategories(data));
});
} catch (error) {
console.error('Error in Categories UseEffect', error);
}
}
}, []);

return (
Expand Down
19 changes: 18 additions & 1 deletion src/components/Products/Products.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ import store from '../../redux/store';
import { Provider } from 'react-redux';

test('renders product card', () => {
const testProducts = [
{
name: 'Pizza',
description: 'See all the things',
category: 'food',
price: 600,
inStock: Math.random() * 20 + 10,
},
{
name: 'cookies',
description: 'See all the things',
category: 'food',
price: 600,

inStock: Math.random() * 20 + 10,
},
];
render(
<Provider store={store}>
<Products />
<Products testProducts={testProducts} />
</Provider>
);

Expand Down
18 changes: 14 additions & 4 deletions src/components/Products/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ import {
import { setCurrentProduct } from '../../redux/reducers/currentProduct';
import { addToCart } from '../../redux/reducers/cart';

export default function Products() {
export default function Products({ testCategories, testProducts }) {
useEffect(() => {
dispatch(fetchProducts()).then(data => dispatch(getProducts(data)));
if (typeof testProducts === undefined) {
try {
dispatch(fetchProducts()).then(data => dispatch(getProducts(data)));
} catch (error) {
console.error('Error in Products UseEffect', error);
}
}
}, []);

const categories = useSelector(state => state.categories);
const categories = testCategories
? testCategories
: useSelector(state => state.categories);
const chosenCategory = useSelector(state => state.chosenCategory);
const products = useSelector(state => state.products);
const products = testProducts
? testProducts
: useSelector(state => state.products);
const dispatch = useDispatch();

return (
Expand Down
6 changes: 3 additions & 3 deletions src/components/StoreFront/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { Container } from '@mui/material';

import Cart from '../Cart';

export default function StoreFront() {
export default function StoreFront({ testProducts, testCategories }) {
return (
<Container>
<Cart />
<Categories />
<Products />
<Categories testCategories={testCategories} />
<Products testProducts={testProducts} />
</Container>
);
}
4 changes: 4 additions & 0 deletions src/redux/reducers/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ export const chosenCategory = createReducer(initialChosenCategory, {
export const fetchCategories = createAsyncThunk(
'updateCategories',
async () => {
try {
const response = await axios.get(
`${import.meta.env.VITE_API_URL}/categories`
);
return response.data;
} catch(error) {
console.error('Error fetching Categories', error)
}
}
);
34 changes: 24 additions & 10 deletions src/redux/reducers/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,41 @@ export const products = createReducer(initialProducts, {
export const setCurrentProduct = createAsyncThunk(
getCurrentProduct,
async () => {
try {
const response = await axios.get(
`${import.meta.env.VITE_API_URL}/products`
);
return response.data;
} catch (error) {
console.error('Error setting current product', error);
}
}
);

export const fetchProducts = createAsyncThunk(getProducts, async () => {
try {
const response = await axios.get(
`${import.meta.env.VITE_API_URL}/products`
);
return response.data;
} catch (error) {
console.error('Error fetching Products', error);
}
);

export const fetchProducts = createAsyncThunk(getProducts, async () => {
const response = await axios.get(`${import.meta.env.VITE_API_URL}/products`);
return response.data;
});

export const asyncUpdateProduct = createAsyncThunk(
updateProduct,
async item => {
const response = await axios.put(
`${import.meta.env.VITE_API_URL}/products/${item._id}`,
{ inStock: item.inStock }
);
try {
const response = await axios.put(
`${import.meta.env.VITE_API_URL}/products/${item._id}`,
{ inStock: item.inStock }
);

return response.data;
return response.data;
} catch (error) {
console.error('Error Updating Product', error);
}
}
);

Expand Down
7 changes: 7 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ expect.extend(matchers);
afterEach(() => {
cleanup();
});

// FAIL LOUDLY on unhandled promise rejections / errors
process.on('unhandledRejection', reason => {
// eslint-disable-next-line no-console
console.log(`FAILED TO HANDLE PROMISE REJECTION`);
throw reason;
});

0 comments on commit 94bc235

Please sign in to comment.