From 20f99214c0a6a7af28f0e012e41dff3285361c16 Mon Sep 17 00:00:00 2001 From: Alex Demchenko Date: Mon, 24 May 2021 18:49:33 +0200 Subject: [PATCH] Fix build --- package.json | 2 +- src/components/TextMessage/TextMessage.tsx | 8 +++-- .../__tests__/TextMessage.test.tsx | 32 +++++++++++++++++++ yarn.lock | 8 ++--- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 6fa5bb4..994c029 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ }, "dependencies": { "@flyerhq/react-native-keyboard-accessory-view": "^2.2.0", - "@flyerhq/react-native-link-preview": "^1.3.0", + "@flyerhq/react-native-link-preview": "^1.3.1", "dayjs": "^1.10.4", "react-native-image-viewing": "^0.2.0", "react-native-parsed-text": "^0.0.22" diff --git a/src/components/TextMessage/TextMessage.tsx b/src/components/TextMessage/TextMessage.tsx index 55d5fe2..7793fdb 100644 --- a/src/components/TextMessage/TextMessage.tsx +++ b/src/components/TextMessage/TextMessage.tsx @@ -45,7 +45,11 @@ export const TextMessage = ({ onPreviewDataFetched?.({ message, previewData: data }) } - const handleUrlPress = (url: string) => Linking.openURL(url) + const handleUrlPress = (url: string) => { + const uri = url.toLowerCase().startsWith('http') ? url : `https://${url}` + + Linking.openURL(uri) + } const renderPreviewDescription = (description: string) => { return ( @@ -62,11 +66,11 @@ export const TextMessage = ({ parse={[ { onPress: handleUrlPress, + pattern: REGEX_LINK, style: StyleSheet.flatten([ text, { textDecorationLine: 'underline' }, ]), - type: 'url', }, ]} style={text} diff --git a/src/components/TextMessage/__tests__/TextMessage.test.tsx b/src/components/TextMessage/__tests__/TextMessage.test.tsx index aca8a19..f0cd347 100644 --- a/src/components/TextMessage/__tests__/TextMessage.test.tsx +++ b/src/components/TextMessage/__tests__/TextMessage.test.tsx @@ -38,4 +38,36 @@ describe('text message', () => { getPreviewDataMock.mockRestore() openUrlMock.mockRestore() }) + + it('renders preview image without https and handles link press', async () => { + expect.assertions(2) + const link = 'github.com/flyerhq/' + const getPreviewDataMock = jest + .spyOn(utils, 'getPreviewData') + .mockResolvedValue({ + description: 'description', + image: { + height: 460, + url: 'https://avatars2.githubusercontent.com/u/59206044', + width: 460, + }, + link, + title: 'title', + }) + const openUrlMock = jest.spyOn(Linking, 'openURL') + const { getByRole, getByText } = render( + + ) + await waitFor(() => getByRole('image')) + const image = getByRole('image') + expect(image).toBeDefined() + const text = getByText(link) + fireEvent.press(text) + expect(openUrlMock).toHaveBeenCalledWith('https://' + link) + getPreviewDataMock.mockRestore() + openUrlMock.mockRestore() + }) }) diff --git a/yarn.lock b/yarn.lock index 44818fb..6f7b8a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -765,10 +765,10 @@ resolved "https://registry.yarnpkg.com/@flyerhq/react-native-keyboard-accessory-view/-/react-native-keyboard-accessory-view-2.2.0.tgz#b9aa613d10541ff0a8a4984ee16ddb5627b75496" integrity sha512-6tBsrLXJ6u2ChjVAmbMMiSJmLOQJ7aneroS8HTUzPhefBbZXKKODQjmZ+pVF2tXmwuZr+CjZSzOQrfKXeXd68A== -"@flyerhq/react-native-link-preview@^1.3.0": - version "1.3.0" - resolved "https://registry.yarnpkg.com/@flyerhq/react-native-link-preview/-/react-native-link-preview-1.3.0.tgz#c54dd41861a07ef8e5c69d5dcd9b3ccfd77178ca" - integrity sha512-nQgmYbfyfFkD2pJMe3EqtYiHMcDYA8gZ9pevhXZ16Stn1EwWvd7s14bd9h2B63g67TFoyVr/fiQrbH0rm8dryA== +"@flyerhq/react-native-link-preview@^1.3.1": + version "1.3.1" + resolved "https://registry.yarnpkg.com/@flyerhq/react-native-link-preview/-/react-native-link-preview-1.3.1.tgz#620c5405ce3b51084673b963125e65db8fb92c0b" + integrity sha512-b6kExHnWt4Shwbgtv11MClVFl1v46RDciS/fAv3+Aw8RVbk12HgMkCDLGbXCcYpXAzp3iiTqtAyUHQOj70/Qag== dependencies: html-entities "^2.3.2"