-
Notifications
You must be signed in to change notification settings - Fork 1
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 #15 from glassonion1/feature/as_to_aschild
Improve unit tests
- Loading branch information
Showing
6 changed files
with
146 additions
and
14 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
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,51 @@ | ||
import React from 'react' | ||
import { describe, it, expect } from 'vitest' | ||
import { render, screen } from '@testing-library/react' | ||
|
||
import { IconButton } from '../src' | ||
|
||
describe('given a IconButton with Slot', () => { | ||
describe('without asChild', () => { | ||
it('should render a button with icon', async () => { | ||
render(<IconButton icon="home">Button with icon</IconButton>) | ||
|
||
const button = screen.getByRole('button') | ||
expect(button).toBeInTheDocument() | ||
|
||
const text = screen.getByText(/home/) | ||
expect(text).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
describe('with asChild', () => { | ||
it('should render a link with icon', async () => { | ||
render( | ||
<IconButton icon="home" asChild> | ||
<a href="https://googl.com">Button with icon</a> | ||
</IconButton> | ||
) | ||
|
||
const button = screen.getByRole('link') | ||
expect(button).toBeInTheDocument() | ||
|
||
const text = screen.getByText(/home/) | ||
expect(text).toBeInTheDocument() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('given a IconButton', () => { | ||
it('should aria-hidden true', async () => { | ||
render(<IconButton icon="home">button</IconButton>) | ||
|
||
const text = screen.getByText(/home/) | ||
expect(text).toHaveAttribute('aria-hidden', 'true') | ||
}) | ||
|
||
it('should aria-hidden false', async () => { | ||
render(<IconButton icon="home" />) | ||
|
||
const text = screen.getByText(/home/) | ||
expect(text).toHaveAttribute('aria-hidden', '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,77 @@ | ||
import React from 'react' | ||
import { describe, beforeEach, it, expect, vi } from 'vitest' | ||
import { render, screen, fireEvent } from '@testing-library/react' | ||
|
||
import { Slot } from '../src' | ||
|
||
type TriggerProps = React.ComponentProps<'button'> & { as: React.ElementType } | ||
|
||
const Trigger = ({ as: Comp = 'button', ...props }: TriggerProps) => ( | ||
<Comp {...props} /> | ||
) | ||
|
||
describe('given a slotted Trigger', () => { | ||
describe('with onClick on itself', () => { | ||
const handleClick = vi.fn() | ||
|
||
beforeEach(() => { | ||
handleClick.mockReset() | ||
render( | ||
<Trigger as={Slot} onClick={handleClick}> | ||
<button type="button">Click me</button> | ||
</Trigger> | ||
) | ||
fireEvent.click(screen.getByRole('button')) | ||
}) | ||
|
||
it('should call the onClick passed to the Trigger', async () => { | ||
expect(handleClick).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
|
||
describe('with onClick on the child', () => { | ||
const handleClick = vi.fn() | ||
|
||
beforeEach(() => { | ||
handleClick.mockReset() | ||
render( | ||
<Trigger as={Slot}> | ||
<button type="button" onClick={handleClick}> | ||
Click me | ||
</button> | ||
</Trigger> | ||
) | ||
fireEvent.click(screen.getByRole('button')) | ||
}) | ||
|
||
it("should call the child's onClick", async () => { | ||
expect(handleClick).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
|
||
describe('with onClick on itself AND the child', () => { | ||
const handleTriggerClick = vi.fn() | ||
const handleChildClick = vi.fn() | ||
|
||
beforeEach(() => { | ||
handleTriggerClick.mockReset() | ||
handleChildClick.mockReset() | ||
render( | ||
<Trigger as={Slot} onClick={handleTriggerClick}> | ||
<button type="button" onClick={handleChildClick}> | ||
Click me | ||
</button> | ||
</Trigger> | ||
) | ||
fireEvent.click(screen.getByRole('button')) | ||
}) | ||
|
||
it("should not call the Trigger's onClick", async () => { | ||
expect(handleTriggerClick).not.toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it("should call the child's onClick", async () => { | ||
expect(handleChildClick).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |