successful test of nugget.

This commit is contained in:
Jordan 2024-02-23 07:57:42 -08:00
parent 5c3c8a0beb
commit 5d7c53dd3c
5 changed files with 33 additions and 2 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"nuxt.isNuxtApp": false
}

View File

@ -9,6 +9,7 @@
"@mui/icons-material": "^5.15.10",
"@mui/material": "^5.15.10",
"@reduxjs/toolkit": "^2.2.1",
"@testing-library/dom": "^9.3.4",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",

View File

@ -23,6 +23,9 @@ dependencies:
'@reduxjs/toolkit':
specifier: ^2.2.1
version: 2.2.1(react-redux@9.1.0)(react@18.2.0)
'@testing-library/dom':
specifier: ^9.3.4
version: 9.3.4
'@testing-library/jest-dom':
specifier: ^5.17.0
version: 5.17.0

View File

@ -0,0 +1,24 @@
import React from 'react';
import { render, fireEvent, screen} from '@testing-library/react';
import Nugget from './Nugget';
test('renders Nugget component', () => {
const result = render(<Nugget text="Hello, world!" />);
expect(result.container.querySelector(".text")?.textContent).toContain("Hello, world!");
});
test('updates score when up arrow is clicked', () => {
const result = render(<Nugget text="Hello, world!" initialScore={0} />);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const upButton = result.container.querySelector(".incScore");
if (upButton) fireEvent.click(upButton);
expect(result.container.querySelector(".score")?.textContent).toBe("+1");
});
test('updates score when down arrow is clicked', () => {
const result = render(<Nugget text="Hello, world!" initialScore={0} />);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const downButton = result.container.querySelector(".decScore");
if (downButton) fireEvent.click(downButton);
expect(result.container.querySelector(".score")?.textContent).toBe("-1");
});

View File

@ -14,10 +14,10 @@ export default function Nugget({ text, initialScore }: { text: string, initialSc
<span className='score'>{score > 0 ? "+" + score : score}</span>
<span className='buttons'>
<ButtonGroup size="small" orientation='vertical'>
<Button onClick={() => setScore(score + 1)}>
<Button onClick={() => setScore(score + 1)} className='incScore'>
<KeyboardArrowUpIcon />
</Button>
<Button onClick={() => setScore(score - 1)}>
<Button onClick={() => setScore(score - 1)} className='decScore'>
<KeyboardArrowDownIcon />
</Button>
</ButtonGroup>