Skip to content
Snippets Groups Projects
Commit ad3e6788 authored by Alexander Hammerman's avatar Alexander Hammerman
Browse files

Completed component as well as the tests

parent 07685bee
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ import './App.css'; ...@@ -2,7 +2,7 @@ import './App.css';
import AmountPicker from './components/AmountPicker'; import AmountPicker from './components/AmountPicker';
const App = () => { const App = () => {
return <AmountPicker></AmountPicker> return (<AmountPicker></AmountPicker>)
} }
export default App; export default App;
import { render, screen } from '@testing-library/react'; import { render, screen } from '@testing-library/react';
import AmountPicker from '../components/AmountPicker'; import AmountPicker from '../components/AmountPicker';
import App from '../App';
test('Testing + button rendering', () => { test('Testing + button rendering', () => {
render(<AmountPicker />) render(<AmountPicker />)
...@@ -12,4 +11,13 @@ test('Testing - button rendering', () => { ...@@ -12,4 +11,13 @@ test('Testing - button rendering', () => {
render(<AmountPicker />) render(<AmountPicker />)
const decreaseButton = screen.getByRole('button',{name: '-'}) const decreaseButton = screen.getByRole('button',{name: '-'})
expect(decreaseButton).toBeInTheDocument() expect(decreaseButton).toBeInTheDocument()
}); });
\ No newline at end of file
test('Testing count rendering', () => {
render(<AmountPicker />)
const count = screen.getByRole('spinbutton')
expect(count).toBeInTheDocument()
})
//Write your test cases below :)
import Button from "@mui/material/Button" import Button from "@mui/material/Button"
import { useState } from "react" import { useState } from "react"
import { ButtonGroup, TextField } from "@mui/material" import { TextField } from "@mui/material"
import { Stack } from "@mui/system" import { Stack } from "@mui/system"
function AmountPicker() { function AmountPicker() {
const [count, setCount] = useState(0) const min = 1
const max = 20
const [count, setCount] = useState(1)
const [withDrawn, setwithDrawn] = useState(0)
const increaseCount = () => { const increaseCount = () => {
setCount(count + 1) if (count + 1 < max) {
setCount(count + 1)
} else {
setCount(max)
}
} }
const decreaseCount = () => { const decreaseCount = () => {
setCount(count - 1) if (count - 1 > min) {
setCount(count - 1)
} else {
setCount(min)
}
}
const withDraw = () => {
setwithDrawn(withDrawn + count)
} }
return ( return (
<div> <div>
<Stack spacing={3}> <Stack direction="row"
<Button onClick={increaseCount} variant="contained">+</Button> spacing={1}
<div> alignItems='center'
{count} justifyContent='center'
</div> sx={{
border: 1
}}>
<Button onClick={decreaseCount} variant="contained">-</Button> <Button onClick={decreaseCount} variant="contained">-</Button>
<TextField type="number"
id="count"
value={count}
sx={{
input: { textAlign: 'center' }
}}
onChange={(newValue) => {
if (Number(newValue.target.value) == min) {
setCount(min)
} else if (Number(newValue.target.value) > max) {
setCount(max)
} else {
setCount(Number(newValue.target.value))
}
}
}
>
</TextField>
<Button onClick={increaseCount} variant="contained">+</Button>
</Stack>
<Stack
spacing={1}
alignItems='center'
justifyContent='center'>
<Button
variant="contained"
onClick={withDraw}
>
Withdraw current amount
</Button>
<div>
<p>You have withdrawn </p>
<p>{withDrawn}</p>
</div>
</Stack> </Stack>
</div> </div>
) )
} }
export default AmountPicker export default AmountPicker
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment