Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1270 Input button test #1634

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions products/statement-generator/src/__tests__/inputbutton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React, { useState } from 'react';
import { render, fireEvent } from '@testing-library/react';
import { ThemeProvider } from '@material-ui/core/styles';
import customMuiTheme from '../styles/customMuiTheme';
import Button from '../components/Button';
import Input from '../components/Input';

// Simple wrapper component that implements the required behavior
const TestComponent = () => {
const [formValues, setFormValues] = useState({
input1: '',
input2: '',
});

const handleInputChange = (inputId: string) => (
e: React.ChangeEvent<HTMLInputElement>
) => {
setFormValues((prev) => ({
...prev,
[inputId]: e.target.value,
}));
};

// Button is disabled unless both inputs have values
const isButtonDisabled = !formValues.input1 || !formValues.input2;

return (
<ThemeProvider theme={customMuiTheme}>
<Input
id="input1"
label="Input 1"
placeholder="Enter something"
type="text"
handleChange={handleInputChange('input1')}
/>
<Input
id="input2"
label="Input 2"
placeholder="Enter something"
type="text"
handleChange={handleInputChange('input2')}
/>
<Button buttonText="Submit" disabled={isButtonDisabled} />
</ThemeProvider>
);
};

describe('Input and Button interactions', () => {
let button: HTMLButtonElement;
let input1: HTMLInputElement;
let input2: HTMLInputElement;

beforeEach(() => {
const { getByRole } = render(<TestComponent />);

button = getByRole('button', { name: /Submit/i }) as HTMLButtonElement;
input1 = getByRole('textbox', { name: /Input 1/i }) as HTMLInputElement;
input2 = getByRole('textbox', { name: /Input 2/i }) as HTMLInputElement;
});

test('button starts disabled', () => {
expect(button).toBeDisabled();
});

test('filling some inputs keeps button disabled', () => {
// Fill only the first input
fireEvent.change(input1, { target: { value: 'Test input 1' } });
expect(button).toBeDisabled();

// Clear first input and fill only second input
fireEvent.change(input1, { target: { value: '' } });
fireEvent.change(input2, { target: { value: 'Test input 2' } });
expect(button).toBeDisabled();
});

test('filling all inputs enables the button', () => {
// Fill both inputs
fireEvent.change(input1, { target: { value: 'Test input 1' } });
fireEvent.change(input2, { target: { value: 'Test input 2' } });
expect(button).not.toBeDisabled();
});

test('removing an answer after filling all inputs disables the button', () => {
// Fill both inputs
fireEvent.change(input1, { target: { value: 'Test input 1' } });
fireEvent.change(input2, { target: { value: 'Test input 2' } });
expect(button).not.toBeDisabled();

// Remove an answer
fireEvent.change(input2, { target: { value: '' } });
expect(button).toBeDisabled();

// Fill it again to ensure it re-enables
fireEvent.change(input2, { target: { value: 'Test input 2' } });
expect(button).not.toBeDisabled();
});
});