forked from janhq/jan
-
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.
test: finished all ui components test (janhq#3588)
* test: finished all ui components test * chore: update naming of mock handle change modal test
- Loading branch information
Showing
15 changed files
with
800 additions
and
9 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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react' | ||
import { render, screen, fireEvent } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { Checkbox } from './index' | ||
|
||
// Mock the styles | ||
jest.mock('./styles.scss', () => ({})) | ||
|
||
describe('@joi/core/Checkbox', () => { | ||
it('renders correctly with label', () => { | ||
render(<Checkbox id="test-checkbox" label="Test Checkbox" />) | ||
expect(screen.getByLabelText('Test Checkbox')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders with helper description', () => { | ||
render(<Checkbox id="test-checkbox" helperDescription="Helper text" />) | ||
expect(screen.getByText('Helper text')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders error message when provided', () => { | ||
render(<Checkbox id="test-checkbox" errorMessage="Error occurred" />) | ||
expect(screen.getByText('Error occurred')).toBeInTheDocument() | ||
}) | ||
|
||
it('calls onChange when clicked', () => { | ||
const mockOnChange = jest.fn() | ||
render( | ||
<Checkbox | ||
id="test-checkbox" | ||
label="Test Checkbox" | ||
onChange={mockOnChange} | ||
/> | ||
) | ||
|
||
fireEvent.click(screen.getByLabelText('Test Checkbox')) | ||
expect(mockOnChange).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('applies custom className', () => { | ||
render(<Checkbox id="test-checkbox" className="custom-class" />) | ||
expect(screen.getByRole('checkbox').parentElement).toHaveClass( | ||
'custom-class' | ||
) | ||
}) | ||
|
||
it('disables the checkbox when disabled prop is true', () => { | ||
render(<Checkbox id="test-checkbox" label="Disabled Checkbox" disabled />) | ||
expect(screen.getByLabelText('Disabled Checkbox')).toBeDisabled() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react' | ||
import { render, screen, fireEvent } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { Input } from './index' | ||
|
||
// Mock the styles import | ||
jest.mock('./styles.scss', () => ({})) | ||
|
||
describe('@joi/core/Input', () => { | ||
it('renders correctly', () => { | ||
render(<Input placeholder="Test input" />) | ||
expect(screen.getByPlaceholderText('Test input')).toBeInTheDocument() | ||
}) | ||
|
||
it('applies custom className', () => { | ||
render(<Input className="custom-class" />) | ||
expect(screen.getByRole('textbox')).toHaveClass('custom-class') | ||
}) | ||
|
||
it('aligns text to the right when textAlign prop is set', () => { | ||
render(<Input textAlign="right" />) | ||
expect(screen.getByRole('textbox')).toHaveClass('text-right') | ||
}) | ||
|
||
it('renders prefix icon when provided', () => { | ||
render(<Input prefixIcon={<span data-testid="prefix-icon">Prefix</span>} />) | ||
expect(screen.getByTestId('prefix-icon')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders suffix icon when provided', () => { | ||
render(<Input suffixIcon={<span data-testid="suffix-icon">Suffix</span>} />) | ||
expect(screen.getByTestId('suffix-icon')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders clear icon when clearable is true', () => { | ||
render(<Input clearable />) | ||
expect(screen.getByTestId('cross-2-icon')).toBeInTheDocument() | ||
}) | ||
|
||
it('calls onClick when input is clicked', () => { | ||
const onClick = jest.fn() | ||
render(<Input onClick={onClick} />) | ||
fireEvent.click(screen.getByRole('textbox')) | ||
expect(onClick).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('calls onClear when clear icon is clicked', () => { | ||
const onClear = jest.fn() | ||
render(<Input clearable onClear={onClear} />) | ||
fireEvent.click(screen.getByTestId('cross-2-icon')) | ||
expect(onClear).toHaveBeenCalledTimes(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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react' | ||
import { render, screen, fireEvent } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { Modal } from './index' | ||
|
||
// Mock the styles | ||
jest.mock('./styles.scss', () => ({})) | ||
|
||
describe('Modal', () => { | ||
it('renders the modal with trigger and content', () => { | ||
render( | ||
<Modal | ||
trigger={<button>Open Modal</button>} | ||
content={<div>Modal Content</div>} | ||
/> | ||
) | ||
|
||
expect(screen.getByText('Open Modal')).toBeInTheDocument() | ||
fireEvent.click(screen.getByText('Open Modal')) | ||
expect(screen.getByText('Modal Content')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the modal with title', () => { | ||
render( | ||
<Modal | ||
trigger={<button>Open Modal</button>} | ||
content={<div>Modal Content</div>} | ||
title="Modal Title" | ||
/> | ||
) | ||
|
||
fireEvent.click(screen.getByText('Open Modal')) | ||
expect(screen.getByText('Modal Title')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders full page modal', () => { | ||
render( | ||
<Modal | ||
trigger={<button>Open Modal</button>} | ||
content={<div>Modal Content</div>} | ||
fullPage | ||
/> | ||
) | ||
|
||
fireEvent.click(screen.getByText('Open Modal')) | ||
expect(screen.getByRole('dialog')).toHaveClass('modal__content--fullpage') | ||
}) | ||
|
||
it('hides close button when hideClose is true', () => { | ||
render( | ||
<Modal | ||
trigger={<button>Open Modal</button>} | ||
content={<div>Modal Content</div>} | ||
hideClose | ||
/> | ||
) | ||
|
||
fireEvent.click(screen.getByText('Open Modal')) | ||
expect(screen.queryByLabelText('Close')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('calls onOpenChange when opening and closing the modal', () => { | ||
const onOpenChangeMock = jest.fn() | ||
render( | ||
<Modal | ||
trigger={<button>Open Modal</button>} | ||
content={<div>Modal Content</div>} | ||
onOpenChange={onOpenChangeMock} | ||
/> | ||
) | ||
|
||
fireEvent.click(screen.getByText('Open Modal')) | ||
expect(onOpenChangeMock).toHaveBeenCalledWith(true) | ||
|
||
fireEvent.click(screen.getByLabelText('Close')) | ||
expect(onOpenChangeMock).toHaveBeenCalledWith(false) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react' | ||
import { render, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { Progress } from './index' | ||
|
||
// Mock the styles | ||
jest.mock('./styles.scss', () => ({})) | ||
|
||
describe('@joi/core/Progress', () => { | ||
it('renders with default props', () => { | ||
render(<Progress value={50} />) | ||
const progressElement = screen.getByRole('progressbar') | ||
expect(progressElement).toBeInTheDocument() | ||
expect(progressElement).toHaveClass('progress') | ||
expect(progressElement).toHaveClass('progress--medium') | ||
expect(progressElement).toHaveAttribute('aria-valuenow', '50') | ||
}) | ||
|
||
it('applies custom className', () => { | ||
render(<Progress value={50} className="custom-class" />) | ||
const progressElement = screen.getByRole('progressbar') | ||
expect(progressElement).toHaveClass('custom-class') | ||
}) | ||
|
||
it('renders with different sizes', () => { | ||
const { rerender } = render(<Progress value={50} size="small" />) | ||
let progressElement = screen.getByRole('progressbar') | ||
expect(progressElement).toHaveClass('progress--small') | ||
|
||
rerender(<Progress value={50} size="large" />) | ||
progressElement = screen.getByRole('progressbar') | ||
expect(progressElement).toHaveClass('progress--large') | ||
}) | ||
|
||
it('sets the correct transform style based on value', () => { | ||
render(<Progress value={75} />) | ||
const progressElement = screen.getByRole('progressbar') | ||
const indicatorElement = progressElement.firstChild as HTMLElement | ||
expect(indicatorElement).toHaveStyle('transform: translateX(-25%)') | ||
}) | ||
|
||
it('handles edge cases for value', () => { | ||
const { rerender } = render(<Progress value={0} />) | ||
let progressElement = screen.getByRole('progressbar') | ||
let indicatorElement = progressElement.firstChild as HTMLElement | ||
expect(indicatorElement).toHaveStyle('transform: translateX(-100%)') | ||
expect(progressElement).toHaveAttribute('aria-valuenow', '0') | ||
|
||
rerender(<Progress value={100} />) | ||
progressElement = screen.getByRole('progressbar') | ||
indicatorElement = progressElement.firstChild as HTMLElement | ||
expect(indicatorElement).toHaveStyle('transform: translateX(-0%)') | ||
expect(progressElement).toHaveAttribute('aria-valuenow', '100') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react' | ||
import { render, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { ScrollArea } from './index' | ||
|
||
declare const global: typeof globalThis | ||
|
||
// Mock the styles | ||
jest.mock('./styles.scss', () => ({})) | ||
|
||
class ResizeObserverMock { | ||
observe() {} | ||
unobserve() {} | ||
disconnect() {} | ||
} | ||
|
||
global.ResizeObserver = ResizeObserverMock | ||
|
||
describe('@joi/core/ScrollArea', () => { | ||
it('renders children correctly', () => { | ||
render( | ||
<ScrollArea> | ||
<div data-testid="child">Test Content</div> | ||
</ScrollArea> | ||
) | ||
|
||
const child = screen.getByTestId('child') | ||
expect(child).toBeInTheDocument() | ||
expect(child).toHaveTextContent('Test Content') | ||
}) | ||
|
||
it('applies custom className', () => { | ||
const { container } = render(<ScrollArea className="custom-class" />) | ||
|
||
const root = container.firstChild as HTMLElement | ||
expect(root).toHaveClass('scroll-area__root') | ||
expect(root).toHaveClass('custom-class') | ||
}) | ||
|
||
it('forwards ref to the Viewport component', () => { | ||
const ref = React.createRef<HTMLDivElement>() | ||
render(<ScrollArea ref={ref} />) | ||
|
||
expect(ref.current).toBeInstanceOf(HTMLDivElement) | ||
expect(ref.current).toHaveClass('scroll-area__viewport') | ||
}) | ||
}) |
Oops, something went wrong.