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

Testing components #6

Merged
merged 1 commit into from
May 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 79 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"react-icons": "^4.3.1",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-test-renderer": "^18.1.0",
"uuid": "^8.3.2",
"web-vitals": "^2.1.4"
},
Expand Down
53 changes: 53 additions & 0 deletions src/components/calculator/__test__/Calculator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { render, fireEvent, screen } from '@testing-library/react';
import renderer from 'react-test-renderer';
import Calculator from '../Calculator';

it('renders "AC" on Quotes link', () => {
render(<Calculator />);
const linkElement = screen.getByText("Let's do some Maths");
expect(linkElement).toBeTruthy();
});

it('renders correctly', () => {
const tree = renderer.create(<Calculator />).toJSON();
expect(tree).toMatchSnapshot();
});

it('contains exactly 20 buttons', () => {
render(<Calculator />);

const buttons = screen.getAllByRole('button');

expect(buttons.length).toBe(20);
});

it('returns the correct result after a {2, +, 2, =} operation', () => {
render(<Calculator />);
const calcscreen = screen.getByTestId(/calc-screen/);
const twoButton = screen.getByText(/2/);
const plusButton = screen.getByText(/^[+]$/);
const equalButton = screen.getByText(/=/);

fireEvent.click(twoButton);
fireEvent.click(plusButton);
fireEvent.click(twoButton);
fireEvent.click(equalButton);

expect(calcscreen.textContent).toBe('4');
});

it('returns the correct result after a {4, ÷, 2, =} operation', () => {
render(<Calculator />);
const calcscreen = screen.getByTestId(/calc-screen/);
const oneButton = screen.getByText(/4/);
const plusButton = screen.getByText(/^[÷]$/);
const twoButton = screen.getByText(/2/);
const equalButton = screen.getByText(/=/);

fireEvent.click(oneButton);
fireEvent.click(plusButton);
fireEvent.click(twoButton);
fireEvent.click(equalButton);

expect(calcscreen.textContent).toBe('2');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders correctly 1`] = `
<div
className="calc"
>
<div
className="calc-animation"
>
<h3
className="animate__animated animate__pulse animate__infinite"
>
Let's do some Maths
</h3>
<svg
className="arrow"
fill="currentColor"
height="1em"
stroke="currentColor"
strokeWidth="0"
style={
Object {
"color": undefined,
}
}
viewBox="0 0 24 24"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M0 0h24v24H0z"
fill="none"
/>
<path
d="M15.5 5H11l5 7-5 7h4.5l5-7z"
/>
<path
d="M8.5 5H4l5 7-5 7h4.5l5-7z"
/>
</svg>
</div>
<ul
className="calc-body"
>
<li
className="calc-screen"
data-testid="calc-screen"
>
0
</li>
<li
className="calc-keyboard"
>
<div
className="calc-digits"
>
<button
id="AC"
onClick={[Function]}
type="button"
>
AC
</button>
<button
id="+/-"
onClick={[Function]}
type="button"
>
+/-
</button>
<button
id="%"
onClick={[Function]}
type="button"
>
%
</button>
<button
id="7"
onClick={[Function]}
type="button"
>
7
</button>
<button
id="8"
onClick={[Function]}
type="button"
>
8
</button>
<button
id="9"
onClick={[Function]}
type="button"
>
9
</button>
<button
id="4"
onClick={[Function]}
type="button"
>
4
</button>
<button
id="5"
onClick={[Function]}
type="button"
>
5
</button>
<button
id="6"
onClick={[Function]}
type="button"
>
6
</button>
<button
id="1"
onClick={[Function]}
type="button"
>
1
</button>
<button
id="2"
onClick={[Function]}
type="button"
>
2
</button>
<button
id="3"
onClick={[Function]}
type="button"
>
3
</button>
<button
id="0"
onClick={[Function]}
type="button"
>
0
</button>
<button
id="00"
onClick={[Function]}
type="button"
>
00
</button>
<button
id="."
onClick={[Function]}
type="button"
>
.
</button>
</div>
<div
className="calc-operators"
>
<button
onClick={[Function]}
type="button"
>
÷
</button>
<button
onClick={[Function]}
type="button"
>
x
</button>
<button
onClick={[Function]}
type="button"
>
-
</button>
<button
onClick={[Function]}
type="button"
>
+
</button>
<button
onClick={[Function]}
type="button"
>
=
</button>
</div>
</li>
</ul>
</div>
`;
14 changes: 14 additions & 0 deletions src/components/homePage/__test__/Home.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { render, screen } from '@testing-library/react';
import renderer from 'react-test-renderer';
import Home from '../Home';

test('renders "The history of mathematics" on homepage link', () => {
render(<Home />);
const linkElement = screen.getByText(/The history of mathematics/i);
expect(linkElement).toBeTruthy();
});

it('renders correctly', () => {
const tree = renderer.create(<Home />).toJSON();
expect(tree).toMatchSnapshot();
});