Stub web project
This commit is contained in:
25
web/src/components/App.jsx
Normal file
25
web/src/components/App.jsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import React from 'react'
|
||||
import { add } from '../utils/calculator.js' // Note the .js extension
|
||||
|
||||
function App() {
|
||||
const [result, setResult] = React.useState(0)
|
||||
|
||||
const handleCalculate = () => {
|
||||
setResult(add(5, 3))
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-4">
|
||||
<h1 className="text-2xl font-bold mb-4">React App</h1>
|
||||
<button
|
||||
className="bg-blue-500 text-white px-4 py-2 rounded"
|
||||
onClick={handleCalculate}
|
||||
>
|
||||
Calculate 5 + 3
|
||||
</button>
|
||||
<p className="mt-4">Result: {result}</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default App
|
||||
15
web/src/components/App.test.jsx
Normal file
15
web/src/components/App.test.jsx
Normal file
@@ -0,0 +1,15 @@
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import App from './App'
|
||||
|
||||
describe('App', () => {
|
||||
it('renders without crashing', () => {
|
||||
render(<App />)
|
||||
expect(screen.getByText(/React App/i)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('calculates correctly when button is clicked', () => {
|
||||
render(<App />)
|
||||
fireEvent.click(screen.getByText(/Calculate/i))
|
||||
expect(screen.getByText(/Result: 8/i)).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
9
web/src/index.jsx
Normal file
9
web/src/index.jsx
Normal file
@@ -0,0 +1,9 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom/client'
|
||||
import App from './components/App'
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>
|
||||
)
|
||||
7
web/src/utils/calculator.js
Normal file
7
web/src/utils/calculator.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export const add = (a, b) => a + b
|
||||
export const subtract = (a, b) => a - b
|
||||
export const multiply = (a, b) => a * b
|
||||
export const divide = (a, b) => {
|
||||
if (b === 0) throw new Error('Division by zero')
|
||||
return a / b
|
||||
}
|
||||
16
web/src/utils/calculator.test.js
Normal file
16
web/src/utils/calculator.test.js
Normal file
@@ -0,0 +1,16 @@
|
||||
// calculator.test.js
|
||||
import { add, subtract, multiply, divide } from './calculator.js' // Note the .js extension
|
||||
|
||||
describe('Calculator', () => {
|
||||
describe('add', () => {
|
||||
it('adds two numbers correctly', () => {
|
||||
expect(add(2, 3)).toBe(5)
|
||||
})
|
||||
})
|
||||
|
||||
describe('divide', () => {
|
||||
it('throws error on division by zero', () => {
|
||||
expect(() => divide(1, 0)).toThrow('Division by zero')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user