-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Persona): extend text property (#876)
- Loading branch information
Showing
10 changed files
with
149 additions
and
38 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,60 @@ | ||
import React from 'react'; | ||
|
||
import {queryByAttribute, render, screen} from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import {Persona} from '../Persona'; | ||
import i18n from '../i18n'; | ||
import type {PersonaProps} from '../types'; | ||
import {extractTextValue, getTwoLetters} from '../utils'; | ||
|
||
const MOCKED_TEXT = 'text'; | ||
const MOCKED_TEXT_NODE_CONTENT_VALUE = 'Some view'; | ||
const MOCKED_TEXT_NODE: PersonaProps['text'] = { | ||
value: MOCKED_TEXT, | ||
content: <div>{MOCKED_TEXT_NODE_CONTENT_VALUE}</div>, | ||
}; | ||
|
||
describe('Persona', () => { | ||
describe('text property', () => { | ||
test.each<PersonaProps['text']>([MOCKED_TEXT, MOCKED_TEXT_NODE])( | ||
'should return text value as onClick argument', | ||
async (text) => { | ||
const onClick = jest.fn(); | ||
render(<Persona text={text} onClick={onClick} />); | ||
const user = userEvent.setup(); | ||
const textValue = extractTextValue(text); | ||
const twoLetters = getTwoLetters(textValue); | ||
const personaNode = screen.getByText(twoLetters); | ||
await user.click(personaNode); | ||
expect(onClick).toBeCalledWith(textValue); | ||
}, | ||
); | ||
test.each<PersonaProps['text']>([MOCKED_TEXT, MOCKED_TEXT_NODE])( | ||
'should return text value as onClose argument', | ||
async (text) => { | ||
const onClose = jest.fn(); | ||
const {container} = render(<Persona text={text} onClose={onClose} />); | ||
const user = userEvent.setup(); | ||
const textValue = extractTextValue(text); | ||
const ariaLabelValue = i18n('label_remove-button', {persona: textValue}); | ||
const closeButtonNode = queryByAttribute('aria-label', container, ariaLabelValue); | ||
|
||
if (!closeButtonNode) { | ||
throw new Error('There is no close button in dom'); | ||
} | ||
|
||
await user.click(closeButtonNode); | ||
expect(onClose).toBeCalledWith(textValue); | ||
}, | ||
); | ||
test('should render text as string', () => { | ||
render(<Persona text={MOCKED_TEXT} />); | ||
screen.getByText(MOCKED_TEXT); | ||
}); | ||
test('should render text as react node', () => { | ||
render(<Persona text={MOCKED_TEXT_NODE} />); | ||
screen.getByText(MOCKED_TEXT_NODE_CONTENT_VALUE); | ||
}); | ||
}); | ||
}); |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"label_remove-button": "Remove {{persona}}" | ||
} |
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,7 @@ | ||
import {addComponentKeysets} from '../../utils/addComponentKeysets'; | ||
import {NAMESPACE_NEW} from '../../utils/cn'; | ||
|
||
import en from './en.json'; | ||
import ru from './ru.json'; | ||
|
||
export default addComponentKeysets({en, ru}, `${NAMESPACE_NEW}persona-remove-button`); |
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,3 @@ | ||
{ | ||
"label_remove-button": "Удалить {{persona}}" | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './Persona'; | ||
export {Persona} from './Persona'; | ||
export type {PersonaProps} from './types'; |
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 @@ | ||
export type PersonaText = string | {value: string; content: React.ReactNode}; | ||
|
||
export type PersonaProps = { | ||
/** Visible text node */ | ||
text: PersonaText; | ||
/** Image source */ | ||
image?: string; | ||
/** | ||
* Visual appearance (with or without border) | ||
* @deprecated Use `hasBorder` prop instead | ||
*/ | ||
theme?: 'default' | 'clear'; | ||
/** Display border */ | ||
hasBorder?: boolean; | ||
/** Avatar appearance */ | ||
type?: 'person' | 'email' | 'empty'; | ||
/** Text size */ | ||
size?: 's' | 'n'; | ||
/** Handle click on button with cross */ | ||
onClose?: (text: string) => void; | ||
/** Handle click on component itself */ | ||
onClick?: (text: string) => void; | ||
/** Custom CSS class for root element */ | ||
className?: string; | ||
style?: React.CSSProperties; | ||
}; |
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,24 @@ | ||
import _ from 'lodash'; | ||
|
||
import type {PersonaText} from './types'; | ||
|
||
export const extractTextValue = (text: PersonaText = '') => { | ||
if (text && typeof text === 'object') { | ||
return text.value; | ||
} | ||
|
||
return text; | ||
}; | ||
|
||
export const extractTextView = (text: PersonaText = '') => { | ||
if (text && typeof text === 'object') { | ||
return text.content; | ||
} | ||
|
||
return text; | ||
}; | ||
|
||
export function getTwoLetters(text: string) { | ||
const words = text.split(' '); | ||
return [_.get(words, '[0][0]'), _.get(words, '[1][0]')].filter(Boolean).join(''); | ||
} |
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