Skip to content

Commit

Permalink
Added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbjensen committed Jan 26, 2025
1 parent c69e5ca commit 52cb630
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 25 deletions.
49 changes: 47 additions & 2 deletions src/components/input/Input.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
import React from 'react';
import '@testing-library/jest-dom';
import { render, fireEvent } from '@testing-library/react';
import Input from './Input';

describe('Input', () => {
Input;
test.todo('should do something');
test('renders correctly with default props', () => {
const { getByPlaceholderText } = render(<Input placeholder="Enter text" />);
const inputElement = getByPlaceholderText('Enter text');
expect(inputElement).toBeInTheDocument();
expect(inputElement).toHaveAttribute('type', 'text');
});

test('renders correctly with given props', () => {
const { getByPlaceholderText } = render(
<Input
type="password"
className="custom-class"
defaultValue="password123"
placeholder="Enter password"
name="password"
/>
);
const inputElement = getByPlaceholderText('Enter password');
expect(inputElement).toBeInTheDocument();
expect(inputElement).toHaveAttribute('type', 'password');
expect(inputElement).toHaveClass('custom-class');
expect(inputElement).toHaveAttribute('name', 'password');
expect(inputElement).toHaveValue('password123');
});

test('calls onChange when value changes', () => {
const handleChange = jest.fn();
const { getByPlaceholderText } = render(
<Input placeholder="Enter text" onChange={handleChange} />
);
const inputElement = getByPlaceholderText('Enter text');
fireEvent.change(inputElement, { target: { value: 'new value' } });
expect(handleChange).toHaveBeenCalledTimes(1);
expect(inputElement).toHaveValue('new value');
});

test('forwards ref to the input element', () => {
const ref = React.createRef();
const { getByPlaceholderText } = render(
<Input placeholder="Enter text" ref={ref} />
);
const inputElement = getByPlaceholderText('Enter text');
expect(ref.current).toBe(inputElement);
});
});
35 changes: 19 additions & 16 deletions src/components/mobile-menu/MobileMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@ const MobileMenu = ({ menuOpen, toggleMenu, links, loggedIn, Link }) => {
<CloseIcon width="20px" />
</div>
</div>
<ul>
{links
.filter((x) => x.hideOptions({ loggedIn }))
.map((link, i) => {
return (
<MenuItem
key={i}
{...link}
isMobile={true}
toggleMenu={toggleMenu}
i={i}
Link={Link}
/>
);
})}
</ul>
{menuOpen && (
<ul>
{links
.filter((x) => x.hideOptions({ loggedIn }))
.map((link, i) => {
return (
<MenuItem
key={i}
{...link}
isMobile={true}
toggleMenu={toggleMenu}
i={i}
Link={Link}
/>
);
})
}
</ul>)
}
</div>
);
};
Expand Down
83 changes: 80 additions & 3 deletions src/components/mobile-menu/MobileMenu.test.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,84 @@

import React from 'react';
import '@testing-library/jest-dom';
import { render, screen, fireEvent } from '@testing-library/react';
import MobileMenu from './MobileMenu';
import MenuItem from '../menu-item/MenuItem';
import CloseIcon from '../close-icon/CloseIcon';

jest.mock('../menu-item/MenuItem', () => jest.fn(() => <div>MenuItem</div>));
jest.mock('../close-icon/CloseIcon', () => jest.fn(() => <div>CloseIcon</div>));

describe('MobileMenu', () => {
MobileMenu;
test.todo('should do something');
const mockToggleMenu = jest.fn();
const mockLinks = [
{ hideOptions: jest.fn(() => true), name: 'Link1' },
{ hideOptions: jest.fn(() => false), name: 'Link2' },
];
const mockLinkComponent = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
});

test('renders correctly when menu is open', () => {
render(
<MobileMenu
menuOpen={true}
toggleMenu={mockToggleMenu}
links={mockLinks}
loggedIn={true}
Link={mockLinkComponent}
/>
);

expect(screen.getByText('CloseIcon')).toBeInTheDocument();
expect(screen.getByText('MenuItem')).toBeInTheDocument();
expect(screen.getByText('MenuItem')).toBeInTheDocument();
});

test('renders correctly when menu is closed', () => {
render(
<MobileMenu
menuOpen={false}
toggleMenu={mockToggleMenu}
links={mockLinks}
loggedIn={true}
Link={mockLinkComponent}
/>
);

expect(screen.getByText('CloseIcon')).toBeInTheDocument();
expect(screen.queryByText('MenuItem')).not.toBeInTheDocument();
});

test('calls toggleMenu when close icon is clicked', () => {
render(
<MobileMenu
menuOpen={true}
toggleMenu={mockToggleMenu}
links={mockLinks}
loggedIn={true}
Link={mockLinkComponent}
/>
);

fireEvent.click(screen.getByText('CloseIcon'));
expect(mockToggleMenu).toHaveBeenCalledTimes(1);
});

test('filters links based on hideOptions', () => {
render(
<MobileMenu
menuOpen={true}
toggleMenu={mockToggleMenu}
links={mockLinks}
loggedIn={true}
Link={mockLinkComponent}
/>
);

expect(mockLinks[0].hideOptions).toHaveBeenCalledWith({ loggedIn: true });
expect(mockLinks[1].hideOptions).toHaveBeenCalledWith({ loggedIn: true });
expect(screen.getAllByText('MenuItem')).toHaveLength(1);
});
});
14 changes: 12 additions & 2 deletions src/components/page/Page.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import React from 'react';
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import Page from './Page';

describe('Page', () => {
Page;
test.todo('should do something');
test('renders children correctly', () => {
const { getByText } = render(<Page>Test Content</Page>);
expect(getByText('Test Content')).toBeInTheDocument();
});

test('has the correct className', () => {
const { container } = render(<Page>Test Content</Page>);
expect(container.firstChild).toHaveClass('page');
});
});
41 changes: 39 additions & 2 deletions src/components/select/Select.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
import React from 'react';
import '@testing-library/jest-dom';
import { render, fireEvent } from '@testing-library/react';
import Select from './Select';

describe('Select', () => {
Select;
test.todo('should do something');
const options = [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
];

test('renders without crashing', () => {
const { getByRole } = render(<Select options={options} />);
expect(getByRole('combobox')).toBeInTheDocument();
});

test('renders options correctly', () => {
const { getByRole } = render(<Select options={options} />);
const select = getByRole('combobox');
expect(select.children.length).toBe(2);
expect(select.children[0].value).toBe('option1');
expect(select.children[1].value).toBe('option2');
});

test('calls onChange when an option is selected', () => {
const handleChange = jest.fn();
const { getByRole } = render(
<Select options={options} onChange={handleChange} />
);
const select = getByRole('combobox');
fireEvent.change(select, { target: { value: 'option2' } });
expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange).toHaveBeenCalledWith(expect.any(Object));
});

test('sets default value correctly', () => {
const { getByRole } = render(
<Select options={options} defaultValue="option2" />
);
const select = getByRole('combobox');
expect(select.value).toBe('option2');
});
});

0 comments on commit 52cb630

Please sign in to comment.