-
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.
Merge pull request #22 from SAINIAbhishek/react_testing
testing setup and basic integration
- Loading branch information
Showing
20 changed files
with
507 additions
and
50 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
74 changes: 74 additions & 0 deletions
74
frontend/src/components/buttons/icon-btn/__tests__/indes.test.tsx
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,74 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { vi } from 'vitest'; | ||
import IconButton, { IconButtonProps } from '..'; | ||
|
||
describe('IconButton', () => { | ||
const renderButton = (props: IconButtonProps) => { | ||
render(<IconButton {...props} />); | ||
}; | ||
|
||
test('renders the button with children', () => { | ||
renderButton({ children: <span>Icon</span> }); | ||
expect(screen.getByText('Icon')).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders with disabled state', () => { | ||
renderButton({ children: <span>Icon</span>, isDisabled: true }); | ||
const button = screen.getByRole('button'); | ||
expect(button).toBeDisabled(); | ||
expect(button).toHaveClass('cursor-not-allowed'); | ||
}); | ||
|
||
test('renders with loading state', () => { | ||
renderButton({ children: <span>Icon</span>, isLoading: true }); | ||
const button = screen.getByRole('button'); | ||
expect(button).toHaveClass('cursor-not-allowed'); | ||
}); | ||
|
||
test('calls handleClick when clicked', () => { | ||
const handleClick = vi.fn(); | ||
renderButton({ children: <span>Icon</span>, handleClick }); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('does not call handleClick when disabled', () => { | ||
const handleClick = vi.fn(); | ||
renderButton({ | ||
children: <span>Icon</span>, | ||
handleClick, | ||
isDisabled: true, | ||
}); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(handleClick).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('applies the submit type', () => { | ||
renderButton({ children: <span>Icon</span>, type: 'submit' }); | ||
expect(screen.getByRole('button')).toHaveAttribute('type', 'submit'); | ||
}); | ||
|
||
test('applies the reset type', () => { | ||
renderButton({ children: <span>Icon</span>, type: 'reset' }); | ||
expect(screen.getByRole('button')).toHaveAttribute('type', 'reset'); | ||
}); | ||
|
||
test('applies additional classes from className prop', () => { | ||
renderButton({ | ||
children: <span>Icon</span>, | ||
className: 'extra-class', | ||
}); | ||
expect(screen.getByRole('button')).toHaveClass('extra-class'); | ||
}); | ||
|
||
test('translates the title', () => { | ||
renderButton({ | ||
children: <span>Icon</span>, | ||
title: 'translated.title', | ||
}); | ||
expect(screen.getByRole('button')).toHaveAttribute( | ||
'title', | ||
'translated.title', | ||
); | ||
}); | ||
}); |
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
56 changes: 56 additions & 0 deletions
56
frontend/src/components/buttons/link-btn/__tests__/index.test.tsx
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,56 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import LinkButton, { LinkButtonProps } from '..'; | ||
import { vi } from 'vitest'; | ||
|
||
describe('LinkButton', () => { | ||
const title = 'Title'; | ||
|
||
const renderButton = (props: LinkButtonProps) => { | ||
render(<LinkButton {...props} />); | ||
}; | ||
|
||
test('renders the button with title', () => { | ||
renderButton({ title: title }); | ||
expect(screen.getByText(title)).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders with disabled state', () => { | ||
renderButton({ title: title, isDisabled: true }); | ||
const button = screen.getByRole('button'); | ||
expect(button).toBeDisabled(); | ||
}); | ||
|
||
test('calls handleClick when clicked', () => { | ||
const handleClick = vi.fn(); | ||
renderButton({ title: title, handleClick }); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('does not call handleClick when disabled', () => { | ||
const handleClick = vi.fn(); | ||
renderButton({ title: title, handleClick, isDisabled: true }); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(handleClick).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('applies the reset type', () => { | ||
renderButton({ title: title, type: 'reset' }); | ||
expect(screen.getByRole('button')).toHaveAttribute('type', 'reset'); | ||
}); | ||
|
||
test('applies the submit type', () => { | ||
renderButton({ title: title, type: 'submit' }); | ||
expect(screen.getByRole('button')).toHaveAttribute('type', 'submit'); | ||
}); | ||
|
||
test('applies additional classes from className prop', () => { | ||
renderButton({ title: title, className: 'extra-class' }); | ||
expect(screen.getByRole('button')).toHaveClass('extra-class'); | ||
}); | ||
|
||
test('translates the title', () => { | ||
renderButton({ title: 'translated.title' }); | ||
expect(screen.getByText('translated.title')).toBeInTheDocument(); | ||
}); | ||
}); |
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
64 changes: 64 additions & 0 deletions
64
frontend/src/components/buttons/primary-btn/__tests__/index.test.tsx
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,64 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { vi } from 'vitest'; | ||
import PrimaryButton, { PrimaryButtonProps } from '..'; | ||
|
||
describe('PrimaryButton', () => { | ||
const title = 'Title'; | ||
|
||
const renderPrimaryButton = (props: PrimaryButtonProps) => { | ||
render(<PrimaryButton {...props} />); | ||
}; | ||
|
||
test('renders the button with title', () => { | ||
renderPrimaryButton({ title: title }); | ||
expect(screen.getByText(title)).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders with loading state', () => { | ||
renderPrimaryButton({ title: title, isLoading: true }); | ||
expect(screen.queryByText(title)).not.toBeInTheDocument(); | ||
expect(screen.getByTestId('spinner')).toBeInTheDocument(); | ||
expect(screen.getByTestId('spinner')).toHaveClass( | ||
'h-6', | ||
'w-6', | ||
'border-t-2', | ||
'border-white', | ||
); | ||
}); | ||
|
||
test('calls handleClick when clicked', () => { | ||
const handleClick = vi.fn(); | ||
renderPrimaryButton({ title: title, handleClick }); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('calls handleClick when clicked', () => { | ||
const handleClick = vi.fn(); | ||
renderPrimaryButton({ title: title, handleClick }); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('does not call handleClick when disabled', () => { | ||
const handleClick = vi.fn(); | ||
renderPrimaryButton({ title: title, handleClick, isDisabled: true }); | ||
fireEvent.click(screen.getByRole('button')); | ||
expect(handleClick).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('applies the correct type', () => { | ||
renderPrimaryButton({ title: title, type: 'submit' }); | ||
expect(screen.getByRole('button')).toHaveAttribute('type', 'submit'); | ||
}); | ||
|
||
test('applies additional classes from className prop', () => { | ||
renderPrimaryButton({ title: title, className: 'extra-class' }); | ||
expect(screen.getByRole('button')).toHaveClass('extra-class'); | ||
}); | ||
|
||
test('translates the title', () => { | ||
renderPrimaryButton({ title: 'translated.title' }); | ||
expect(screen.getByText('translated.title')).toBeInTheDocument(); | ||
}); | ||
}); |
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
Oops, something went wrong.