This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MessageCard Emoji Survey variant (#1077)
- Loading branch information
Showing
5 changed files
with
217 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { MessageCardSurveyEmoji as EmojiSurvey } from './components/survey/MessageCard.Survey.Emoji' | ||
import { MessageCardSurveyFaces as FacesSurvey } from './components/survey/MessageCard.Survey.Faces' | ||
import { MessageCardSurveyThumbs as ThumbsSurvey } from './components/survey/MessageCard.Survey.Thumbs' | ||
import { MessageCardSurveyMultipleChoice as MultipleChoiceSurvey } from './components/survey/MessageCard.Survey.MultipleChoice' | ||
|
||
export { FacesSurvey, ThumbsSurvey, MultipleChoiceSurvey } | ||
export { EmojiSurvey, FacesSurvey, ThumbsSurvey, MultipleChoiceSurvey } |
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
58 changes: 58 additions & 0 deletions
58
src/components/MessageCard/components/survey/MessageCard.Survey.Emoji.jsx
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,58 @@ | ||
import PropTypes from 'prop-types' | ||
|
||
import { | ||
EmojiButtonUI, | ||
ImageEmojiUI, | ||
SurveyOptionsUI, | ||
} from './MessageCard.Survey.css' | ||
|
||
import { useButtonResizeOnSelection } from '../../utils/MessageCard.hooks' | ||
import { useSurveyContext } from '../../utils/MessageCard.Survey.context' | ||
|
||
const EMOJI_CDN_BASE_PATH = | ||
'https://beacon-v2.helpscout.net/static/img-apple-64' | ||
|
||
export const MessageCardSurveyEmoji = ({ emojis = [] }) => { | ||
const { onSelection, selected, withFeedbackForm } = useSurveyContext() | ||
const { buttonSize, handleOnClick } = useButtonResizeOnSelection({ | ||
disableTransition: !withFeedbackForm, | ||
onSelection, | ||
}) | ||
|
||
const className = `is-${buttonSize}` | ||
|
||
return ( | ||
<SurveyOptionsUI> | ||
{emojis.map(({ id, name, unicode, native }) => ( | ||
<EmojiButtonUI | ||
key={id} | ||
onClick={() => handleOnClick(id)} | ||
className={`${className} ${selected === id ? 'is-selected' : ''}`} | ||
> | ||
{native ? ( | ||
<span aria-label={name} role="img"> | ||
{native} | ||
</span> | ||
) : ( | ||
<ImageEmojiUI | ||
alt={name} | ||
src={`${EMOJI_CDN_BASE_PATH}/${unicode}.png`} | ||
className={className} | ||
/> | ||
)} | ||
</EmojiButtonUI> | ||
))} | ||
</SurveyOptionsUI> | ||
) | ||
} | ||
|
||
MessageCardSurveyEmoji.propTypes = { | ||
emojis: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.string.isRequired, | ||
name: PropTypes.string.isRequired, | ||
unicode: PropTypes.string.isRequired, | ||
native: PropTypes.string, | ||
}) | ||
).isRequired, | ||
} |
76 changes: 76 additions & 0 deletions
76
src/components/MessageCard/components/survey/MessageCard.Survey.Emoji.test.js
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,76 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
import { SurveyContext } from '../../utils/MessageCard.Survey.context' | ||
import { MessageCardSurveyEmoji } from './MessageCard.Survey.Emoji' | ||
|
||
describe('MessageCard.Survey.Emoji', () => { | ||
test('should only render the emojis passed in the emojis prop', () => { | ||
render( | ||
<MessageCardSurveyEmoji | ||
emojis={[ | ||
{ id: 'happy', name: 'Happy', unicode: '1f600' }, | ||
{ id: 'sad', name: 'Sad', unicode: '1f622' }, | ||
]} | ||
/> | ||
) | ||
|
||
expect(screen.getAllByRole('button')).toHaveLength(2) | ||
|
||
expect(queryButtonByName('Happy')).toBeInTheDocument() | ||
expect(queryButtonByName('Sad')).toBeInTheDocument() | ||
}) | ||
|
||
test('should render a native emoji instead of an image if the native prop is provided', () => { | ||
render( | ||
<MessageCardSurveyEmoji | ||
emojis={[ | ||
{ id: 'happy', name: 'Happy', unicode: '1f600' }, | ||
{ id: 'sad', name: 'Sad', unicode: '1f622' }, | ||
{ | ||
id: 'grimacing', | ||
name: 'Grimacing', | ||
unicode: '1f62c', | ||
native: '😬', | ||
}, | ||
]} | ||
/> | ||
) | ||
|
||
expect(screen.getAllByRole('button')).toHaveLength(3) | ||
|
||
expect(queryButtonByName('Happy')).toBeInTheDocument() | ||
expect(queryButtonByName('Sad')).toBeInTheDocument() | ||
expect(queryButtonByName('Grimacing')).toBeInTheDocument() | ||
expect(queryButtonByName('Grimacing')).toHaveTextContent('😬') | ||
}) | ||
|
||
test('should call the onSelection callback from the SurveyContext when an emoji is selected', () => { | ||
const onSelection = jest.fn() | ||
const contextValue = { | ||
onSelection, | ||
selected: null, | ||
} | ||
|
||
render( | ||
<SurveyContext.Provider value={contextValue}> | ||
<MessageCardSurveyEmoji | ||
emojis={[ | ||
{ id: '+1::skin-tone-3', name: 'Thumbs Up', unicode: '1f602' }, | ||
{ id: 'sad', name: 'Sad', unicode: '1f622' }, | ||
]} | ||
/> | ||
</SurveyContext.Provider> | ||
) | ||
|
||
expect(onSelection).not.toHaveBeenCalled() | ||
|
||
userEvent.click(queryButtonByName('Thumbs Up')) | ||
|
||
expect(onSelection).toHaveBeenCalledWith('+1::skin-tone-3') | ||
}) | ||
|
||
function queryButtonByName(name) { | ||
return screen.queryByRole('button', { name }) | ||
} | ||
}) |
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