Skip to content

Commit

Permalink
[#7] Add some tests for Input componenet and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstevens111 committed Feb 15, 2023
1 parent 4b9fe58 commit b150416
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
11 changes: 2 additions & 9 deletions src/components/Button/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@ import Button from '.';
const handleButtonClick = jest.fn();

describe('Button', () => {
test('renders a button on the page', () => {
render(<Button text="Click me" type="button" onButtonClick={handleButtonClick} />);
test('renders a button of type button on the page with the given class', () => {
render(<Button text="Click me" type="button" className="primary" onButtonClick={handleButtonClick} />);

const buttonComponent = screen.getByRole('button', { name: 'Click me' });

expect(buttonComponent).toBeInTheDocument();
expect(buttonComponent).toHaveAttribute('type', 'button');
});

test('renders a button with the given class', () => {
render(<Button text="Click me" type="button" className="primary" onButtonClick={handleButtonClick} />);

const buttonComponent = screen.getByRole('button', { name: 'Click me' });

expect(buttonComponent).toHaveClass('primary');
});

Expand Down
41 changes: 41 additions & 0 deletions src/components/Input/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import Input from '.';

const handleInputChange = jest.fn();

describe('Input', () => {
test('renders an Input of type Text on the page with the correct class', () => {
render(<Input name="email" label="email" type="text" className="input-class" onInputChange={handleInputChange} />);

const inputComponent = screen.getByRole('textbox', { name: 'email' });

expect(inputComponent).toBeInTheDocument();
expect(inputComponent).toHaveAttribute('type', 'text');
expect(inputComponent).toHaveClass('input-class');
});

test('renders an Input of type Text on the page with a label element', () => {
render(<Input name="email" label="email-label" type="text" onInputChange={handleInputChange} />);

const inputComponent = screen.getByRole('textbox', { name: 'email-label' });
const labelElement = screen.getByLabelText('email-label');

expect(inputComponent).toBeInTheDocument();
expect(labelElement).toBeInTheDocument();
expect(inputComponent).toHaveAttribute('type', 'text');
});

test('when text has changed, calls onInputChange handler', async () => {
render(<Input name="email" label="email-label" type="text" className="input-class" onInputChange={handleInputChange} />);

const user = userEvent.setup();

const inputComponent = screen.getByRole('textbox', { name: 'email-label' });

await user.type(inputComponent, 'test');

expect(handleInputChange).toBeCalledTimes(4);
});
});
6 changes: 4 additions & 2 deletions src/components/Input/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { HTMLInputTypeAttribute } from 'react';

import styles from './Input.module.scss';

type InputProps = {
name: string;
label: string;
type: string;
className: string;
type?: HTMLInputTypeAttribute;
className?: string;
onInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
};

Expand Down

0 comments on commit b150416

Please sign in to comment.