-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Wrong union description with TypeScript #1621
- Loading branch information
Showing
10 changed files
with
277 additions
and
90 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
20 changes: 20 additions & 0 deletions
20
src/client/rsg-components/ComplexType/ComplexType.spec.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,20 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import ComplexType from './ComplexTypeRenderder'; | ||
|
||
function renderComponent(name = 'color', raw = 'red | blue') { | ||
return render(<ComplexType name={name} raw={raw} />); | ||
} | ||
|
||
describe('ComplexType', () => { | ||
test('should render name', () => { | ||
const { getByRole } = renderComponent(); | ||
expect(getByRole('button')).toHaveTextContent('color'); | ||
}); | ||
|
||
test('should render raw text in the tooltip', () => { | ||
const { container, getByRole } = renderComponent(); | ||
fireEvent.focus(getByRole('button')); | ||
expect(container.querySelector('[data-tippy-root]')).toHaveTextContent('red | blue'); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
src/client/rsg-components/ComplexType/ComplexTypeRenderder.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,36 @@ | ||
import React from 'react'; | ||
import Styled, { JssInjectedProps } from 'rsg-components/Styled'; | ||
import { MdInfoOutline } from 'react-icons/md'; | ||
import Text from 'rsg-components/Text'; | ||
import Tooltip from 'rsg-components/Tooltip'; | ||
import * as Rsg from '../../../typings'; | ||
|
||
export const styles = ({ space }: Rsg.Theme) => ({ | ||
complexType: { | ||
alignItems: 'center', | ||
cursor: 'pointer', | ||
display: 'inline-flex', | ||
'& span': { | ||
marginRight: space[0], | ||
cursor: 'pointer', | ||
}, | ||
}, | ||
}); | ||
|
||
export interface ComplexTypeProps extends JssInjectedProps { | ||
name: string; | ||
raw: string; | ||
} | ||
|
||
function ComplexTypeRenderer({ classes, name, raw }: ComplexTypeProps) { | ||
return ( | ||
<Tooltip placement="right" content={raw}> | ||
<span className={classes.complexType}> | ||
<Text>{name}</Text> | ||
<MdInfoOutline /> | ||
</span> | ||
</Tooltip> | ||
); | ||
} | ||
|
||
export default Styled<ComplexTypeProps>(styles)(ComplexTypeRenderer); |
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 @@ | ||
export { default } from 'rsg-components/ComplexType/ComplexTypeRenderder'; |
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,74 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor } from '@testing-library/react'; | ||
import Tooltip, { TooltipPlacement } from './TooltipRenderer'; | ||
|
||
function renderComponent(content = 'tooltip', placement?: TooltipPlacement) { | ||
return render( | ||
<Tooltip content={content} placement={placement}> | ||
<div data-testid="child" /> | ||
</Tooltip> | ||
); | ||
} | ||
|
||
describe('Tooltip', () => { | ||
test('should render child component as is', () => { | ||
const { container, getByTestId } = renderComponent(); | ||
expect(container).toContainElement(getByTestId('child')); | ||
}); | ||
|
||
test('should the child component be wrapped by "role=button" element', () => { | ||
const { getByTestId } = renderComponent(); | ||
expect(getByTestId('child').closest('span')).toHaveAttribute('role', 'button'); | ||
}); | ||
|
||
test('should render content in the tooltop body', () => { | ||
const { container, getByRole } = renderComponent(); | ||
fireEvent.focus(getByRole('button')); | ||
expect(container.querySelector('[data-tippy-root]')).toHaveTextContent('tooltip'); | ||
}); | ||
|
||
test('should show the tooltip by focus in', async () => { | ||
const { container, getByRole } = renderComponent(); | ||
fireEvent.focus(getByRole('button')); | ||
await waitFor(() => | ||
expect(container.querySelector('[data-state="visible"]')).toBeInTheDocument() | ||
); | ||
expect(container.querySelector('[data-state]')).toHaveAttribute('data-state', 'visible'); | ||
}); | ||
|
||
test('should show the tooltip by click', async () => { | ||
const { container, getByRole } = renderComponent(); | ||
fireEvent.click(getByRole('button')); | ||
await waitFor(() => | ||
expect(container.querySelector('[data-state="visible"]')).toBeInTheDocument() | ||
); | ||
expect(container.querySelector('[data-state]')).toHaveAttribute('data-state', 'visible'); | ||
}); | ||
|
||
test('should show the tooltip by mouse enter', async () => { | ||
const { container, getByRole } = renderComponent(); | ||
fireEvent.mouseEnter(getByRole('button')); | ||
await waitFor(() => | ||
expect(container.querySelector('[data-state="visible"]')).toBeInTheDocument() | ||
); | ||
expect(container.querySelector('[data-state]')).toHaveAttribute('data-state', 'visible'); | ||
}); | ||
|
||
describe.each([['top'], ['right'], ['left'], ['bottom']])( | ||
'Test placement attribute', | ||
placement => { | ||
test(`should have ${placement} in data-placement attribute`, async () => { | ||
// @ts-ignore | ||
const { container, getByRole } = renderComponent(undefined, placement); | ||
fireEvent.focus(getByRole('button')); | ||
await waitFor(() => | ||
expect(container.querySelector('[data-state="visible"]')).toBeInTheDocument() | ||
); | ||
expect(container.querySelector('[data-placement]')).toHaveAttribute( | ||
'data-placement', | ||
placement | ||
); | ||
}); | ||
} | ||
); | ||
}); |
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 Tippy from '@tippyjs/react'; | ||
import Styled, { JssInjectedProps } from 'rsg-components/Styled'; | ||
import * as Rsg from '../../../typings'; | ||
|
||
export const styles = ({ space, color, borderRadius, fontSize }: Rsg.Theme) => ({ | ||
tooltip: { | ||
'&.tippy-box': { | ||
transitionProperty: [['opacity']], | ||
'&[data-state="hidden"]': { | ||
opacity: 0, | ||
}, | ||
}, | ||
'& .tippy-content': { | ||
padding: space[0], | ||
border: `1px ${color.border} solid`, | ||
borderRadius, | ||
background: color.baseBackground, | ||
boxShadow: [[0, 2, 4, 'rgba(0,0,0,.15)']], | ||
fontSize: fontSize.small, | ||
color: color.type, | ||
}, | ||
}, | ||
}); | ||
|
||
export type TooltipPlacement = 'top' | 'right' | 'bottom' | 'left'; | ||
|
||
export interface TooltipProps extends JssInjectedProps { | ||
children: React.ReactNode; | ||
content: React.ReactNode; | ||
placement?: TooltipPlacement; | ||
} | ||
|
||
function TooltipRenderer({ classes, children, content, placement = 'top' }: TooltipProps) { | ||
return ( | ||
<Tippy | ||
content={content} | ||
className={classes.tooltip} | ||
interactive | ||
placement={placement} | ||
trigger="click mouseenter focus" | ||
arrow={false} | ||
> | ||
<span role="button" tabIndex={0}> | ||
{children} | ||
</span> | ||
</Tippy> | ||
); | ||
} | ||
|
||
export default Styled<TooltipProps>(styles)(TooltipRenderer); |
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 @@ | ||
export { default } from 'rsg-components/Tooltip/TooltipRenderer'; |