Play with time

This commit is contained in:
Steve Kinney
2024-10-02 16:26:35 -05:00
parent 8fb10ac976
commit a9e36ee3c8
6 changed files with 69 additions and 15 deletions

View File

@@ -1,7 +1,10 @@
import { useState } from 'react';
export const AlertButton = ({}) => {
const [message, setMessage] = useState('Alert!');
export const AlertButton = ({
onSubmit = () => {},
defaultMessage = 'Alert!',
}) => {
const [message, setMessage] = useState(defaultMessage);
return (
<div>
@@ -14,7 +17,7 @@ export const AlertButton = ({}) => {
/>
</label>
<button onClick={() => alert(message)}>Trigger Alert</button>
<button onClick={() => onSubmit(message)}>Trigger Alert</button>
</div>
);
};

View File

@@ -11,17 +11,20 @@ describe('AlertButton', () => {
it('should render an alert button', async () => {});
it.only('should trigger an alert', async () => {
// const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
const handleSubmit = vi.fn();
render(<AlertButton onSubmit={handleSubmit} message="Default Message" />);
render(<AlertButton />);
const input = screen.getByLabelText('Message');
const button = screen.getByRole('button', { name: /trigger alert/i });
await act(async () => {
await userEvent.clear(input);
await userEvent.type(input, 'Hello');
await userEvent.click(button);
});
// Click on the button.
// Look and see if alert() was called with the message.
expect(handleSubmit).toHaveBeenCalled();
expect(handleSubmit).toHaveBeenCalledWith('Hello');
});
});