Skip to content

Commit 062e127

Browse files
authored
Merge pull request realstoman#19 from realstoman/add-tests
Feat: Add unit testing initial parts
2 parents 550bd02 + 2524768 commit 062e127

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+643
-266
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ A simple portfolio starter theme built with React and Tailwind CSS. This is the
1414
- [Tailwind CSS v3](https://tailwindcss.com)
1515
- Context API For State Management
1616
- Custom Hooks
17+
- Unit Testing
1718
- Framer Motion transitions & animations
1819
- Reusable components
1920
- Dark mode
@@ -68,6 +69,12 @@ npm install --global yarn
6869
yarn start
6970
```
7071

72+
6. ##### Run tests:
73+
74+
```
75+
yarn test
76+
```
77+
7178
## Notes
7279

7380
- Always run `yarn install` after pulling new changes

package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@testing-library/jest-dom": "^5.11.4",
7-
"@testing-library/react": "^11.1.0",
8-
"@testing-library/user-event": "^12.1.10",
6+
"@testing-library/jest-dom": "^5.16.5",
7+
"@testing-library/react": "^13.4.0",
8+
"@testing-library/user-event": "^14.4.3",
99
"framer-motion": "4.1.17",
10+
"postcss-cli": "^10.1.0",
1011
"react": "^18.1.0",
1112
"react-countup": "^6.1.1",
1213
"react-dom": "^18.1.0",
@@ -18,7 +19,7 @@
1819
"web-vitals": "^1.0.1"
1920
},
2021
"scripts": {
21-
"start": "yarn run build:css && react-scripts start",
22+
"start": "react-scripts --openssl-legacy-provider start",
2223
"build": "yarn run react-scripts build",
2324
"test": "react-scripts test",
2425
"eject": "react-scripts eject",
@@ -44,8 +45,9 @@
4445
},
4546
"devDependencies": {
4647
"@tailwindcss/forms": "^0.5.2",
48+
"@testing-library/dom": "^8.20.0",
4749
"autoprefixer": "^10.4.7",
48-
"postcss": "^8.4.14",
50+
"postcss": "^8.4.21",
4951
"tailwindcss": "^3.0.24"
5052
}
5153
}

src/App.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import AppFooter from './components/shared/AppFooter';
44
import AppHeader from './components/shared/AppHeader';
55
import './css/App.css';
66
import About from './pages/AboutMe';
7-
import Contact from './pages/Contact';
7+
import Contact from './pages/Contact.jsx';
88
import Home from './pages/Home';
99
import Projects from './pages/Projects';
10-
import ProjectSingle from './pages/ProjectSingle';
10+
import ProjectSingle from './pages/ProjectSingle.jsx';
1111
import { AnimatePresence } from 'framer-motion';
1212
import UseScrollToTop from './hooks/useScrollToTop';
1313

src/__tests__/Banner.test.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { render, screen } from '@testing-library/react';
2+
import AppBanner from '../components/shared/AppBanner';
3+
import userEvent from '@testing-library/user-event';
4+
5+
// This runs before each test. This is good instead of having the component render in each test case
6+
const setup = () => render(<AppBanner />);
7+
8+
// Get user event
9+
function setupUserEvent(jsx) {
10+
return {
11+
user: userEvent.setup(),
12+
...render(jsx),
13+
};
14+
}
15+
16+
test('it shows the title in the banner', () => {
17+
setup();
18+
// We expect that the title 'Hi, Iam Stoman' is in the banner component
19+
expect(screen.getByText(/Hi, Iam Stoman/i)).toBeInTheDocument();
20+
});
21+
22+
test('can download cv when clicked on download cv button', async () => {
23+
const { user } = setupUserEvent(<AppBanner />);
24+
25+
const downloadCV = screen.getByText(/Download CV/i);
26+
27+
expect(downloadCV).toBeInTheDocument();
28+
29+
const downloadCVButton = downloadCV.parentElement.parentElement;
30+
31+
expect(downloadCVButton).toBeInTheDocument();
32+
33+
await user.click(downloadCVButton);
34+
35+
// const downloadLink = {
36+
// click: await user.click(downloadCVButton),
37+
// };
38+
// jest.spyOn(document, 'createElement').mockImplementation(
39+
// () => downloadLink
40+
// );
41+
42+
// expect(downloadLink.download).toEqual('Stoman-Resume.pdf');
43+
// expect(downloadLink.href).toEqual('/files/Stoman-Resume.pdf');
44+
// expect(downloadLink.click).toHaveBeenCalledTimes(1);
45+
});

src/__tests__/Modal.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { render, screen } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
import HireMeModal from '../components/HireMeModal';
4+
5+
// Get user event
6+
function setupUserEvent(jsx) {
7+
return {
8+
user: userEvent.setup(),
9+
...render(jsx),
10+
};
11+
}
12+
13+
test('modal shows the children and a close button', async () => {
14+
const { user } = setupUserEvent(<HireMeModal />);
15+
16+
expect(
17+
screen.getByText(/What project are you looking for?/i)
18+
).toBeInTheDocument();
19+
20+
const closeModal = screen.getByText(/Close/i);
21+
expect(closeModal).toBeInTheDocument();
22+
23+
const closeModalButton = closeModal.parentElement;
24+
expect(closeModalButton).toBeInTheDocument();
25+
});
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/components/about/AboutCounter.js

-53
This file was deleted.

src/components/about/AboutCounter.jsx

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useCountUp } from 'react-countup';
2+
import CounterItem from './CounterItem';
3+
4+
const AboutCounter = () => {
5+
useCountUp({ ref: 'experienceCounter', end: 12, duration: 2 });
6+
useCountUp({ ref: 'githubStarsCounter', end: 20, duration: 2 });
7+
useCountUp({ ref: 'feedbackCounter', end: 92, duration: 2 });
8+
useCountUp({ ref: 'projectsCounter', end: 77, duration: 2 });
9+
10+
return (
11+
<div className="mt-10 sm:mt-20 bg-primary-light dark:bg-ternary-dark shadow-sm">
12+
<div className="font-general-medium container mx-auto py-20 block sm:flex sm:justify-between items-center">
13+
<CounterItem
14+
title="Years of experience"
15+
counter={<span id="experienceCounter" />}
16+
measurement=""
17+
/>
18+
19+
<CounterItem
20+
title="Stars on GitHub"
21+
counter={<span id="githubStarsCounter" />}
22+
measurement="k+"
23+
/>
24+
25+
<CounterItem
26+
title="Positive feedback"
27+
counter={<span id="feedbackCounter" />}
28+
measurement="%"
29+
/>
30+
31+
<CounterItem
32+
title="Projects completed"
33+
counter={<span id="projectsCounter" />}
34+
measurement="%"
35+
/>
36+
</div>
37+
</div>
38+
);
39+
};
40+
41+
export default AboutCounter;

src/components/about/CounterItem.jsx

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
3+
const CounterItem = ({ title, counter, measurement }) => {
4+
return (
5+
<div className="mb-20 sm:mb-0">
6+
<h2 className="text-4xl text-center text-secondary-dark dark:text-secondary-light mb-2">
7+
{counter} {measurement}
8+
</h2>
9+
<span className="font-general-regular block text-md text-center text-ternary-dark dark:text-ternary-light">
10+
{title}
11+
</span>
12+
</div>
13+
);
14+
};
15+
16+
export default CounterItem;

src/components/contact/ContactForm.js

-98
This file was deleted.

0 commit comments

Comments
 (0)