Skip to content

Commit b70dff0

Browse files
committed
test: add additional tests for new functionality
1 parent c94ac87 commit b70dff0

File tree

5 files changed

+227
-3
lines changed

5 files changed

+227
-3
lines changed

src/__tests__/Autolink.test.tsx

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
*/
88

99
import React from 'react';
10-
import { Text, View } from 'react-native';
10+
import { Alert, Text, View } from 'react-native';
1111
import renderer from 'react-test-renderer';
1212
import { Autolink } from '../Autolink';
13-
import { LatLngMatcher } from '../matchers';
13+
import { CustomMatch } from '../CustomMatch';
14+
import { IntlPhoneMatcher, LatLngMatcher } from '../matchers';
1415

1516
describe('<Autolink />', () => {
1617
test('renders a Text node', () => {
@@ -234,6 +235,46 @@ describe('<Autolink />', () => {
234235
expect(onLongPress.mock.calls[0][0]).toBe('mailto:josh%40example.com');
235236
});
236237

238+
describe('alert', () => {
239+
test('displays alert before linking when showAlert prop enabled', () => {
240+
const spy = jest.spyOn(Alert, 'alert').mockImplementationOnce(() => {});
241+
const tree = renderer.create(<Autolink text="#awesome" hashtag="instagram" showAlert />);
242+
tree.root.findAllByType(Text)[1].props.onPress();
243+
expect(spy).toBeCalledTimes(1);
244+
expect(spy).toBeCalledWith('Leaving App', 'Do you want to continue?', expect.any(Array));
245+
});
246+
});
247+
248+
describe('urls', () => {
249+
test('uses hashtag url when pressing linked hashtag', () => {
250+
const onPress = jest.fn();
251+
const tree = renderer.create(
252+
<Autolink text="#awesome" hashtag="instagram" onPress={onPress} />,
253+
);
254+
tree.root.findAllByType(Text)[1].props.onPress();
255+
expect(onPress.mock.calls.length).toBe(1);
256+
expect(onPress.mock.calls[0][0]).toBe('instagram://tag?name=awesome');
257+
});
258+
259+
test('uses mention url when pressing linked mention', () => {
260+
const onPress = jest.fn();
261+
const tree = renderer.create(
262+
<Autolink text="@twitter" mention="twitter" onPress={onPress} />,
263+
);
264+
tree.root.findAllByType(Text)[1].props.onPress();
265+
expect(onPress.mock.calls.length).toBe(1);
266+
expect(onPress.mock.calls[0][0]).toBe('twitter://user?screen_name=twitter');
267+
});
268+
269+
test('uses phone url when pressing linked phone number', () => {
270+
const onPress = jest.fn();
271+
const tree = renderer.create(<Autolink text="415-555-5555" phone onPress={onPress} />);
272+
tree.root.findAllByType(Text)[1].props.onPress();
273+
expect(onPress.mock.calls.length).toBe(1);
274+
expect(onPress.mock.calls[0][0]).toBe('tel:4155555555');
275+
});
276+
});
277+
237278
/**
238279
* Custom matchers
239280
*/
@@ -244,5 +285,52 @@ describe('<Autolink />', () => {
244285
.toJSON();
245286
expect(tree).toMatchSnapshot();
246287
});
288+
289+
test('calls custom onPress handlers', () => {
290+
const onPress = jest.fn();
291+
const tree = renderer.create(
292+
<Autolink text="+14085550123" matchers={[{ ...IntlPhoneMatcher, onPress }]} />,
293+
);
294+
tree.root.findAllByType(Text)[1].props.onPress();
295+
expect(onPress.mock.calls.length).toBe(1);
296+
expect(onPress.mock.calls[0][0]).toBeInstanceOf(CustomMatch);
297+
});
298+
299+
test('calls custom onLongPress handlers', () => {
300+
const onLongPress = jest.fn();
301+
const tree = renderer.create(
302+
<Autolink text="+14085550123" matchers={[{ ...IntlPhoneMatcher, onLongPress }]} />,
303+
);
304+
tree.root.findAllByType(Text)[1].props.onLongPress();
305+
expect(onLongPress.mock.calls.length).toBe(1);
306+
expect(onLongPress.mock.calls[0][0]).toBeInstanceOf(CustomMatch);
307+
});
308+
309+
test('uses getLinkText when rendering link', () => {
310+
const tree = renderer
311+
.create(
312+
<Autolink
313+
text="+14085550123"
314+
matchers={[{ ...IntlPhoneMatcher, getLinkText: () => '__LINK_TEXT__' }]}
315+
/>,
316+
)
317+
.toJSON();
318+
expect(tree).toMatchSnapshot();
319+
});
320+
321+
test('uses getLinkUrl when using default onPress handler', () => {
322+
const onPress = jest.fn();
323+
const tree = renderer.create(
324+
<Autolink
325+
text="+14085550123"
326+
onPress={onPress}
327+
matchers={[{ ...IntlPhoneMatcher, getLinkUrl: () => '__LINK_URL__' }]}
328+
/>,
329+
);
330+
tree.root.findAllByType(Text)[1].props.onPress();
331+
expect(onPress.mock.calls.length).toBe(1);
332+
expect(onPress.mock.calls[0][0]).toBe('__LINK_URL__');
333+
expect(onPress.mock.calls[0][1]).toBeInstanceOf(CustomMatch);
334+
});
247335
});
248336
});

src/__tests__/__snapshots__/Autolink.test.tsx.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,22 @@ exports[`<Autolink /> links multiple elements individually 1`] = `
174174
</Text>
175175
`;
176176

177+
exports[`<Autolink /> matchers uses getLinkText when rendering link 1`] = `
178+
<Text>
179+
<Text
180+
onLongPress={[Function]}
181+
onPress={[Function]}
182+
style={
183+
Object {
184+
"color": "#0E7AFE",
185+
}
186+
}
187+
>
188+
__LINK_TEXT__
189+
</Text>
190+
</Text>
191+
`;
192+
177193
exports[`<Autolink /> matchers wraps text based on supplied custom matchers 1`] = `
178194
<Text>
179195
<Text

src/__tests__/urls.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { EmailMatch, HashtagMatch, MentionMatch, PhoneMatch } from 'autolinker/dist/es2015';
2+
import { getEmailUrl, getHashtagUrl, getMentionUrl, getPhoneUrl } from '../urls';
3+
4+
describe('urls', () => {
5+
describe('getEmailUrl()', () => {
6+
test('returns mailto url', () => {
7+
expect(getEmailUrl(new EmailMatch({ email: 'test@example.com' } as any))).toEqual([
8+
'mailto:test%40example.com',
9+
]);
10+
});
11+
});
12+
13+
describe('getHashtagUrl()', () => {
14+
test('returns facebook hashtag url with fallback', () => {
15+
expect(getHashtagUrl(new HashtagMatch({ hashtag: 'awesome' } as any), 'facebook')).toEqual([
16+
'fb://hashtag/awesome',
17+
'https://www.facebook.com/hashtag/awesome',
18+
]);
19+
});
20+
21+
test('returns instagram hashtag url with fallback', () => {
22+
expect(getHashtagUrl(new HashtagMatch({ hashtag: 'awesome' } as any), 'instagram')).toEqual([
23+
'instagram://tag?name=awesome',
24+
'https://www.instagram.com/explore/tags/awesome/',
25+
]);
26+
});
27+
28+
test('returns twitter hashtag url with fallback', () => {
29+
expect(getHashtagUrl(new HashtagMatch({ hashtag: 'awesome' } as any), 'twitter')).toEqual([
30+
'twitter://search?query=%23awesome',
31+
'https://twitter.com/hashtag/awesome',
32+
]);
33+
});
34+
35+
test('returns matched text if service does not match any supported service', () => {
36+
expect(getHashtagUrl(new HashtagMatch({ matchedText: '#awesome' } as any), false)).toEqual([
37+
'#awesome',
38+
]);
39+
});
40+
});
41+
42+
describe('getMentionUrl()', () => {
43+
test('returns instagram mention url with fallback', () => {
44+
expect(getMentionUrl(new MentionMatch({ mention: 'username' } as any), 'instagram')).toEqual([
45+
'instagram://user?username=username',
46+
'https://www.instagram.com/username/',
47+
]);
48+
});
49+
50+
test('returns soundcloud mention url', () => {
51+
expect(
52+
getMentionUrl(new MentionMatch({ mention: 'username' } as any), 'soundcloud'),
53+
).toEqual(['https://soundcloud.com/username']);
54+
});
55+
56+
test('returns twitter mention url with fallback', () => {
57+
expect(getMentionUrl(new MentionMatch({ mention: 'username' } as any), 'twitter')).toEqual([
58+
'twitter://user?screen_name=username',
59+
'https://twitter.com/username',
60+
]);
61+
});
62+
63+
test('returns matched text if service does not match any supported service', () => {
64+
expect(getMentionUrl(new MentionMatch({ matchedText: '@username' } as any), false)).toEqual([
65+
'@username',
66+
]);
67+
});
68+
});
69+
70+
describe('getPhoneUrl()', () => {
71+
test('returns sms/text url', () => {
72+
expect(getPhoneUrl(new PhoneMatch({ number: '+14085550123' } as any), 'sms')).toEqual([
73+
'sms:+14085550123',
74+
]);
75+
expect(getPhoneUrl(new PhoneMatch({ number: '+14085550123' } as any), 'text')).toEqual([
76+
'sms:+14085550123',
77+
]);
78+
});
79+
80+
test('returns tel url by default', () => {
81+
expect(getPhoneUrl(new PhoneMatch({ number: '+14085550123' } as any), 'tel')).toEqual([
82+
'tel:+14085550123',
83+
]);
84+
expect(getPhoneUrl(new PhoneMatch({ number: '+14085550123' } as any))).toEqual([
85+
'tel:+14085550123',
86+
]);
87+
});
88+
});
89+
});

src/matchers/__tests__/location.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,36 @@
66
* https://github.com/joshswan/react-native-autolink/blob/master/LICENSE
77
*/
88

9+
import { Platform } from 'react-native';
910
import { LatLngMatcher } from '..';
1011

1112
describe('Location matchers', () => {
13+
let OS: string;
14+
15+
beforeEach(() => {
16+
({ OS } = Platform);
17+
});
18+
19+
afterEach(() => {
20+
Platform.OS = OS as any;
21+
});
22+
1223
describe('LatLngMatcher', () => {
24+
const text = 'Location is 34.0522, -118.2437.';
25+
1326
test('matches latitude/longitude pair in text', () => {
14-
const text = 'Location is 34.0522, -118.2437.';
1527
expect(text.replace(LatLngMatcher.pattern, 'MATCH')).toBe('Location is MATCH.');
1628
});
29+
30+
test('returns appropriate link url', () => {
31+
Platform.OS = 'android';
32+
expect(LatLngMatcher.getLinkUrl(['34.0522, -118.2437'])).toBe(
33+
'https://www.google.com/maps/search/?api=1&query=34.0522,-118.2437',
34+
);
35+
Platform.OS = 'ios';
36+
expect(LatLngMatcher.getLinkUrl(['34.0522, -118.2437'])).toBe(
37+
'http://maps.apple.com/?q=34.0522%2C%20-118.2437&ll=34.0522,-118.2437',
38+
);
39+
});
1740
});
1841
});

src/matchers/__tests__/phone.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ describe('Phone matchers', () => {
2020
expect(getText('+48571775914').replace(IntlPhoneMatcher.pattern, 'MATCH')).toBe(resultText);
2121
expect(getText('+447487428082').replace(IntlPhoneMatcher.pattern, 'MATCH')).toBe(resultText);
2222
});
23+
24+
test('returns appropriate link url', () => {
25+
expect(IntlPhoneMatcher.getLinkUrl(['+14085550123'])).toBe('tel:+14085550123');
26+
});
2327
});
2428

2529
describe('PhoneMatchersByCountry', () => {
30+
test('returns appropriate link url', () => {
31+
expect(PhoneMatchersByCountry.US.getLinkUrl(['+1 (408) 555-0123'])).toBe('tel:+14085550123');
32+
});
33+
2634
test('matches French phone numbers', () => {
2735
expect(getText('+33699520828').replace(PhoneMatchersByCountry.FR.pattern, 'MATCH')).toBe(
2836
resultText,

0 commit comments

Comments
 (0)