Skip to content

Commit 0e2a167

Browse files
committed
Add tests for calling multiple times on single string
1 parent 821444a commit 0e2a167

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ test('Works with strings', t => {
2424
);
2525
});
2626

27+
test('Works with arrays', t => {
28+
const input = ['hey there', { value: 'you' }, 'again'];
29+
t.same(
30+
replaceString(input, 'hey', x => ({ worked: x })),
31+
['', { worked: 'hey' }, ' there', { value: 'you' }, 'again']
32+
);
33+
});
34+
2735
test('Successfully escapes parens in strings', t => {
2836
t.same(
2937
replaceString('(hey) there', '(hey)', x => ({ worked: x })),
@@ -35,3 +43,47 @@ test('Successfully escapes parens in strings', t => {
3543
['hey ', { worked: '((y)(you))' }, ' there']
3644
);
3745
});
46+
47+
test('Can be called consecutively on returned result of previous call', t => {
48+
const originalTweet = 'Hey @iansinnott, check this link https://github.com/iansinnott/ #github';
49+
let reactReplacedTweet;
50+
51+
// Match URLs
52+
reactReplacedTweet = replaceString(originalTweet, /(https?:\/\/\S+)/g, match => (
53+
{ type: 'url', value: match }
54+
));
55+
56+
t.same(reactReplacedTweet, [
57+
'Hey @iansinnott, check this link ',
58+
{ type: 'url', value: 'https://github.com/iansinnott/' },
59+
' #github',
60+
]);
61+
62+
// Match @-mentions
63+
reactReplacedTweet = replaceString(reactReplacedTweet, /(@\w+)/g, match => (
64+
{ type: 'mention', value: match }
65+
));
66+
67+
t.same(reactReplacedTweet, [
68+
'Hey ',
69+
{ type: 'mention', value: '@iansinnott' },
70+
', check this link ',
71+
{ type: 'url', value: 'https://github.com/iansinnott/' },
72+
' #github',
73+
]);
74+
75+
// Match hashtags
76+
reactReplacedTweet = replaceString(reactReplacedTweet, /(#\w+)/g, match => (
77+
{ type: 'hashtag', value: match }
78+
));
79+
80+
t.same(reactReplacedTweet, [
81+
'Hey ',
82+
{ type: 'mention', value: '@iansinnott' },
83+
', check this link ',
84+
{ type: 'url', value: 'https://github.com/iansinnott/' },
85+
' ',
86+
{ type: 'hashtag', value: '#github' },
87+
'',
88+
]);
89+
});

0 commit comments

Comments
 (0)