mirror of
https://github.com/zhigang1992/react-native-chat-ui.git
synced 2026-01-12 22:50:15 +08:00
Fix link preview
This commit is contained in:
@@ -47,6 +47,12 @@ export const TextMessage = ({
|
||||
user,
|
||||
})
|
||||
|
||||
const hanleEmailPress = (email: string) => {
|
||||
try {
|
||||
Linking.openURL(`mailto:${email}`)
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const handlePreviewDataFetched = (data: PreviewData) => {
|
||||
setPreviewData(data)
|
||||
onPreviewDataFetched?.({ message, previewData: data })
|
||||
@@ -79,6 +85,11 @@ export const TextMessage = ({
|
||||
<ParsedText
|
||||
accessibilityRole='link'
|
||||
parse={[
|
||||
{
|
||||
onPress: hanleEmailPress,
|
||||
style: [text, { textDecorationLine: 'underline' }],
|
||||
type: 'email',
|
||||
},
|
||||
{
|
||||
onPress: handleUrlPress,
|
||||
pattern: REGEX_LINK,
|
||||
|
||||
@@ -80,4 +80,32 @@ describe('text message', () => {
|
||||
getPreviewDataMock.mockRestore()
|
||||
openUrlMock.mockRestore()
|
||||
})
|
||||
|
||||
it('renders and handles email press', async () => {
|
||||
expect.assertions(1)
|
||||
const email = 'john@flyer.chat'
|
||||
const getPreviewDataMock = jest
|
||||
.spyOn(utils, 'getPreviewData')
|
||||
.mockResolvedValue({})
|
||||
const openUrlMock = jest.spyOn(Linking, 'openURL')
|
||||
const { getByText } = render(
|
||||
<TextMessage
|
||||
message={{
|
||||
...derivedTextMessage,
|
||||
author: { id: 'newUserId', firstName: 'John' },
|
||||
text: email,
|
||||
}}
|
||||
messageWidth={440}
|
||||
onPreviewDataFetched={jest.fn}
|
||||
showName
|
||||
usePreviewData
|
||||
/>
|
||||
)
|
||||
await waitFor(() => getByText(email))
|
||||
const text = getByText(email)
|
||||
fireEvent.press(text)
|
||||
expect(openUrlMock).toHaveBeenCalledWith(`mailto:${email}`)
|
||||
getPreviewDataMock.mockRestore()
|
||||
openUrlMock.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user