-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c69e5ca
commit 52cb630
Showing
5 changed files
with
197 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |