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

Adding Testing with Vite #1908

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2,017 changes: 1,957 additions & 60 deletions projects/2023TemplateWithVite/client/package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions projects/2023TemplateWithVite/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can i provide here

"dev": "vite",
"build": "vite build",
"preview": "vite preview"
"preview": "vite preview",
"test": "vitest"
},
"dependencies": {
"bootstrap": "^5.2.3",
Expand All @@ -18,9 +19,12 @@
"react-router-dom": "^6.9.0"
},
"devDependencies": {
"@testing-library/react": "^14.0.0",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
"vite": "^4.2.0"
"jsdom": "^21.1.1",
"vite": "^4.2.0",
"vitest": "^0.29.8"
}
}
4 changes: 4 additions & 0 deletions projects/2023TemplateWithVite/client/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ button.btn.btn-primary {
margin-left: 2em;
margin-right: 2em;
}

#contact{
padding: 5em;
}
17 changes: 12 additions & 5 deletions projects/2023TemplateWithVite/client/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import './App.css'
import 'bootstrap/dist/css/bootstrap.min.css';
import MyNavBar from './components/Navbar'
import MyNavBar from './routes/Navbar'
Copy link
Collaborator

Choose a reason for hiding this comment

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

I personally would leave NavBar in the components folder because that's what NavBar is, a shared component used on multiple pages; a route would be a page/view*.

* At least in the traditional/beginner setup

import ListStudents from './components/ListStudents'

import React, { useState } from 'react'
import { Outlet } from 'react-router-dom';

function App() {

const [isClicked, setIsClicked] = useState(false);

const handleMe = () =>{
setIsClicked(true);
}


return (
<div className="App">
<MyNavBar />
<ListStudents />

<MyNavBar handleMe={handleMe} />
Copy link
Collaborator

Choose a reason for hiding this comment

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

React convention is that prop names that are callbacks start with the word "on" and then a description of what event it is. So since this callback is called whenever the account menu item is clicked, I'd rename this prop to onAccountMenuItemClicked or something along those lines.

And then handleMe can be renamed to handleAccountMenuItemClicked since "handle" is the conventional suffix used for implementations of callback props

{isClicked ? <Outlet /> : <ListStudents /> }
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const MyForm = ({ onSaveStudent, editingStudent, onUpdateStudent }) => {
};

return (
<Form className='form-students' onSubmit={handleSubmit}>
<Form data-testid="eventForm" className='form-students' onSubmit={handleSubmit}>
<Form.Group>
<Form.Label>First Name</Form.Label>
<input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react'
import * as ioicons from 'react-icons/io5'
import MyForm from './Form';
import Student from './Student';
import { Outlet} from "react-router-dom";

const ListStudents = () => {

Expand Down Expand Up @@ -68,7 +68,7 @@ const ListStudents = () => {
return <li key={student.id}> <Student student={student} toDelete={onDelete} toUpdate={onUpdate} /></li>
})}
</ul>
</div>
</div>
<MyForm key={editingStudent ? editingStudent.id : null} onSaveStudent={onSaveStudent} editingStudent={editingStudent} onUpdateStudent={updateStudent} />
</div>
);
Expand Down
49 changes: 49 additions & 0 deletions projects/2023TemplateWithVite/client/src/components/contact.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@


export default function Contact() {
const contact = {
first: "Cristina",
last: "Rodriguez",
avatar: "https://placekitten.com/g/200/200",
twitter: "yosola",
notes: "Some notes",
favorite: true,
};

return (
<div id="contact">
<div>
<img
key={contact.avatar}
Copy link
Collaborator

Choose a reason for hiding this comment

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

A key prop is not necessary unless you're in a loop OR are abusing the key prop to explicitly trigger re-renders (which doesn't look to be the case here)

src={contact.avatar || null}
/>
</div>

<div>
<h1>
{contact.first || contact.last ? (
<>
{contact.first} {contact.last}
</>
) : (
<i>No Name</i>
)}{" "}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
)}{" "}
)}

</h1>

{contact.twitter && (
<p>
<a
target="_blank"
href={`https://twitter.com/${contact.twitter}`}
>
{contact.twitter}
</a>
</p>
)}

{contact.notes && <p>{contact.notes}</p>}

</div>
</div>
)
}
Comment on lines +46 to +49
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
</div>
</div>
)
}
</div>
</div>
)
}

16 changes: 16 additions & 0 deletions projects/2023TemplateWithVite/client/src/components/error-page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useRouteError } from "react-router-dom";

export default function ErrorPage() {
const error = useRouteError();
console.error(error);

return (
<div id="error-page">
<h1>Oops Techtonica template apologizes!!</h1>
<p>Sorry, an unexpected error has occurred.</p>
<p>
<i>{error.statusText || error.message}</i>
</p>
</div>
);
}
40 changes: 40 additions & 0 deletions projects/2023TemplateWithVite/client/src/example.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import React from 'react';
import {describe, expect, test} from 'vitest';
import { render, screen } from '@testing-library/react';
import Student from './components/Student';
import MyForm from './components/Form';

test("Show full name of a student", () =>{
const testName = {firstname: "Michelle", lastname: "Glausser"};
const studentElement = render(<Student student={testName} />)
const studentFullName = studentElement.getByText("Michelle Glausser");
expect(studentFullName).toBeDefined();
})

test("Show the form is rendering", () =>{
const formElement = render(<MyForm />);
const signUpForm = formElement.getByTestId('eventForm');
expect(signUpForm).toBeDefined();
})

/*
describe('event signup form', () => {
it('renders a form', () => {
render(<EventSignUpForm />);

const signUpForm = screen.getByTestId('event-form');
expect(signUpForm).toBeInTheDocument();
});
});


test('test that MyNavBar renders', () => {
// const navbarElement = render(<MyNavBar/>);
// const navbarId = navbarElement.getByTestId('navbar');
// expect(navbarId).toBeDefined();
// });



*/
Comment on lines +21 to +40
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/*
describe('event signup form', () => {
it('renders a form', () => {
render(<EventSignUpForm />);
const signUpForm = screen.getByTestId('event-form');
expect(signUpForm).toBeInTheDocument();
});
});
test('test that MyNavBar renders', () => {
// const navbarElement = render(<MyNavBar/>);
// const navbarId = navbarElement.getByTestId('navbar');
// expect(navbarId).toBeDefined();
// });
*/

Looks like unused code?

25 changes: 24 additions & 1 deletion projects/2023TemplateWithVite/client/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import { createBrowserRouter, createRoutesFromElements, Route, RouterProvider,} from "react-router-dom";
import './index.css'
import ErrorPage from "./components/error-page";
import Contact from "./components/contact";


// /*
// const router = createBrowserRouter(
// createRoutesFromElements(
// <Route path="/" element={<App />}>
// <Route path="login" element={<Contact />} />
// </Route>
// )
// );

// */
Comment on lines +10 to +19
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove unused code to prevent confusion OR write an explanation on what this commented out code does


const router = createBrowserRouter(
createRoutesFromElements(
<Route path="/" element={<App />}>
<Route path="login" element={<Contact />} />
</Route>
)
);

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
<RouterProvider router={router} />
</React.StrictMode>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import Container from 'react-bootstrap/Container';
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import Logo from '../assets/BlueTechtonicaWord.png'

import { Link } from "react-router-dom";

function MyNavBar(props) {

const handleClick = () =>{
props.handleMe();
}

return (
<>
<Navbar bg="dark" variant="dark" sticky="top">
<Navbar data-testid="navbar" bg="dark" variant="dark" sticky="top">
<Container>
<Navbar.Brand href="/">
<img
Expand All @@ -22,7 +26,7 @@ function MyNavBar(props) {
<Navbar.Toggle />
<Navbar.Collapse className="justify-content-end">
<Navbar.Text>
Signed in as: <a href="#login">Cristina Rodriguez</a>
Signed in as: <Link onClick={handleClick} to={`/login`}>Cristina Rodriguez</Link>
</Navbar.Text>
</Navbar.Collapse>
</Container>
Expand Down
5 changes: 5 additions & 0 deletions projects/2023TemplateWithVite/client/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
test: {
framework: 'vitest',
globals: true,
environment: 'jsdom',
}
})
13 changes: 13 additions & 0 deletions projects/2023TemplateWithVite/client/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
test: {
framework: 'vitest',
globals: true,
environment: 'jsdom',
}
})