Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cypress #24

Merged
merged 3 commits into from
Feb 12, 2024
Merged
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
37 changes: 22 additions & 15 deletions cypress/e2e/error_spec.cy.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
describe('error page', () => {
// it('should display an error page if failed to get fact', () => {
// cy.intercept('https://catfact.ninja/fact', {
// statusCode: 500
// })
// cy.visit('https://cats-meow.vercel.app/')
// cy.get('button[class=fact-btn]').click()
// cy.get('h2').contains('Looks like there was a problem. Please try again later')
// })
it('should display an error page if failed to get fact', () => {
cy.intercept('https://catfact.ninja/fact', {
statusCode: 500
})
cy.visit('http://localhost:3000/')
cy.get('button[class=fact-btn]').click()
cy.get('h2').contains("Looks like something is wrong. Please try again later.")
})

// it('should display message if no favorited facts', () => {
// cy.visit('https://cats-meow.vercel.app/')
// cy.get('button[class=fav-btn]').click()
// cy.get('h2').contains('No saved ideas yet')
// })
it('should be able to visit home page', () => {
cy.visit('https://cats-meow.vercel.app/')
it('should display message if no favorited facts', () => {
cy.visit('https://cats-meow.vercel.app/')
cy.get('button[class=fav-btn]').click()
cy.get('h2').contains('No saved ideas yet')
})
it('should be able to navigate back to home page', () => {
cy.intercept('https://catfact.ninja/fact', {
statusCode: 500
})
cy.visit('http://localhost:3000/')
cy.get('button[class=fact-btn]').click()
cy.get('h2').contains("Looks like something is wrong. Please try again later.")
cy.get('h3').click()
cy.get('h1').contains('Cats Meow')
})
})
31 changes: 5 additions & 26 deletions src/components/App/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import './App.css';
import React, { useState } from 'react';
import Home from '../Home/Home';
// import Header from '../Header/Header';
import Fact, { factLoader } from '../Fact/Fact';
import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import Favorites from '../Favorites/Favorites';
Expand All @@ -10,7 +9,6 @@ import RootLayout from '../../utilities/RootLayout';

const App = () => {
const [favorites, setFavorites] = useState(JSON.parse(sessionStorage.getItem('allFavorites')) || []);
const [err, setErr] = useState('');

const favoriteFact = (currentFact) => {
const noDuplicate = favorites.every(fact => fact.fact !== currentFact.fact)
Expand All @@ -27,15 +25,12 @@ const App = () => {
setFavorites(newFavs)
sessionStorage.setItem('allFavorites', JSON.stringify(newFavs))
}

const removeErr = () => {
setErr('')
}

const routes = createBrowserRouter([
{
path: "/",
element: <RootLayout />,
errorElement: <Error err="Looks like something is wrong. Please try again later." />,
children: [
{
path: "/",
Expand All @@ -49,30 +44,14 @@ const App = () => {
{
path: "/favorites",
element: <Favorites removeFav={removeFav} favs={favorites} />
},
{
path: "/*",
element: <Error removeErr={removeErr} err="Page does not exist" />
}
]
},

]
)


if (err) {
return (
<div className="error-page">
<Error removeErr={removeErr} err={err} />
</div>
)
} else {
return (
<RouterProvider router={routes} />
)
}
])

return (
<RouterProvider router={routes} />
)
}

export default App;
7 changes: 3 additions & 4 deletions src/components/Error/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
import './Error.css';

const Error = ({ err, removeErr }) => {
const Error = ({ err }) => {
return (
<section className="error-page">
<h2 className="error-message">{err}</h2>
<img className="error-img" src="https://images.unsplash.com/photo-1498579687545-d5a4fffb0a9e?ixlib=rb-4.0.3&ixid
=M3wxMjA3fDB8MHxzZWFyY2h8NXx8c2FkJTIwY2F0fGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60" alt="sad cat in a box"/>
<Link to="/"><h3 onClick={() => removeErr()}>return home</h3></Link>
<Link to="/"><h3>return home</h3></Link>
</section>
)
}

export default Error;

Error.propTypes = {
err: PropTypes.string.isRequired,
removeErr: PropTypes.func.isRequired
err: PropTypes.string.isRequired
}
9 changes: 7 additions & 2 deletions src/components/Fact/Fact.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import PropTypes from 'prop-types';
import { useLoaderData } from 'react-router-dom';

const Fact = ({ favFact }) => {
const [currentFact, setCurrentFact] = useState(JSON.parse(sessionStorage.getItem('currentFact')) || {});
const [currentFact, setCurrentFact] = useState(JSON.parse(sessionStorage.getItem('currentFact')));
// const [err, setErr] = useState('');

const randFact = useLoaderData()

if (currentFact === null) {
setCurrentFact(randFact)
sessionStorage.setItem('currentFact', JSON.stringify({fact: randFact.fact}))
}

const setFact = (fact) => {
setCurrentFact({fact: fact});
sessionStorage.setItem('currentFact', JSON.stringify({fact: fact}))
Expand All @@ -29,7 +34,7 @@ const Fact = ({ favFact }) => {
return (
<section className="fact-page">
<div className="fact-display">
<p className="fact">{currentFact.fact || randFact.fact}</p>
<p className="fact">{currentFact.fact}</p>
<img className="fact-img" src="https://images.unsplash.com/photo-1526336024174-e58f5cdd8e13?ixlib
=rb-4.0.3&ixid
=M3wxMjA3fDB8MHxzZWFyY2h8MTJ8fGNhdHN8ZW58MHx8MHx
Expand Down