-
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.
feat: #1334 polymorphic base component
- Loading branch information
1 parent
8dc71f5
commit ab3a05e
Showing
3 changed files
with
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import { describe, expect, test, afterEach } from 'vitest'; | ||
import { cleanup, render, screen } from '@testing-library/react'; | ||
|
||
import Component from './index'; | ||
|
||
const setup = (Component: React.ReactElement) => render(Component); | ||
|
||
afterEach(cleanup); | ||
|
||
describe('Component', () => { | ||
describe('as prop', () => { | ||
test('it falls back to div', () => { | ||
setup(<Component>Hello world</Component>); | ||
expect(screen.getByTestId('Component').nodeName).toBe('DIV'); | ||
}); | ||
|
||
test('it works with HTML element', () => { | ||
setup(<Component as="button">Hello world</Component>); | ||
expect(screen.getByTestId('Component').nodeName).toBe('BUTTON'); | ||
expect(screen.getByRole('button')).toBeTruthy(); | ||
}); | ||
|
||
test('it works with React component', () => { | ||
const TestComponent = ({ status }: { status: string }) => ( | ||
<p>test {status}</p> | ||
); | ||
setup(<Component as={TestComponent} status="info" />); | ||
expect(screen.getByText('test info')).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('testId prop', () => { | ||
test('it works with default testId', () => { | ||
setup(<Component>Hello world</Component>); | ||
expect(screen.getByTestId('Component')).toBeTruthy(); | ||
}); | ||
|
||
test('it works with specified testId', () => { | ||
setup(<Component data-testid="TestId">Hello world</Component>); | ||
expect(screen.getByTestId('TestId')).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
test('it works with className', () => { | ||
setup(<Component className="test">Hello world</Component>); | ||
expect( | ||
screen.getByTestId('Component').classList.contains('test') | ||
).toBeTruthy(); | ||
}); | ||
}); |
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,26 @@ | ||
import React, { ComponentPropsWithoutRef, ElementType, ReactNode } from 'react'; | ||
|
||
export type CommonComponentProps<T extends ElementType> = { | ||
as?: T; | ||
children?: ReactNode; | ||
className?: string; | ||
testId?: string; | ||
}; | ||
|
||
const Component = <T extends ElementType = 'div'>({ | ||
as, | ||
children, | ||
testId = 'Component', | ||
...props | ||
}: CommonComponentProps<T> & | ||
Omit<ComponentPropsWithoutRef<T>, keyof CommonComponentProps<T>>) => { | ||
const PolymorphicComponent = as || 'div'; | ||
return ( | ||
<PolymorphicComponent data-testid={testId} {...props}> | ||
{children} | ||
</PolymorphicComponent> | ||
); | ||
}; | ||
|
||
Component.displayName = 'Component'; | ||
export default Component; |