diff --git a/workshop/src/App.tsx b/workshop/src/App.tsx index 47a75ad2f12e557d80ea626a3a9c1b1c209f2226..e26dc3d3930e9a9ea082ea199d4c92adb60e1899 100644 --- a/workshop/src/App.tsx +++ b/workshop/src/App.tsx @@ -2,7 +2,7 @@ import './App.css'; import AmountPicker from './components/AmountPicker'; const App = () => { - return <AmountPicker></AmountPicker> + return (<AmountPicker></AmountPicker>) } export default App; diff --git a/workshop/src/__tests__/App.test.tsx b/workshop/src/__tests__/AmountPicker.test.tsx similarity index 71% rename from workshop/src/__tests__/App.test.tsx rename to workshop/src/__tests__/AmountPicker.test.tsx index 021ed52607c24ea2d2df12d06fbf151b7dc9b643..46bbc9c619910dab2a116ab7f75c36a935757cb7 100644 --- a/workshop/src/__tests__/App.test.tsx +++ b/workshop/src/__tests__/AmountPicker.test.tsx @@ -1,6 +1,5 @@ import { render, screen } from '@testing-library/react'; import AmountPicker from '../components/AmountPicker'; -import App from '../App'; test('Testing + button rendering', () => { render(<AmountPicker />) @@ -12,4 +11,13 @@ test('Testing - button rendering', () => { render(<AmountPicker />) const decreaseButton = screen.getByRole('button',{name: '-'}) 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 :) diff --git a/workshop/src/components/AmountPicker.tsx b/workshop/src/components/AmountPicker.tsx index 9b94c1bf6c9a330783116b794c49214d19b98aa6..10bc4b36de9bc7451d29e3e42f28f7e5af96d9b5 100644 --- a/workshop/src/components/AmountPicker.tsx +++ b/workshop/src/components/AmountPicker.tsx @@ -1,31 +1,83 @@ import Button from "@mui/material/Button" import { useState } from "react" -import { ButtonGroup, TextField } from "@mui/material" +import { TextField } from "@mui/material" import { Stack } from "@mui/system" 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 = () => { - setCount(count + 1) + if (count + 1 < max) { + setCount(count + 1) + } else { + setCount(max) + } } const decreaseCount = () => { - setCount(count - 1) + if (count - 1 > min) { + setCount(count - 1) + } else { + setCount(min) + } + } + + const withDraw = () => { + setwithDrawn(withDrawn + count) } return ( <div> - <Stack spacing={3}> - <Button onClick={increaseCount} variant="contained">+</Button> - <div> - {count} - </div> + <Stack direction="row" + spacing={1} + alignItems='center' + justifyContent='center' + sx={{ + border: 1 + }}> <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> </div> ) } -export default AmountPicker \ No newline at end of file +export default AmountPicker