Skip to content

Commit aa34f90

Browse files
committed
Add tests for
1 parent 2eebd4f commit aa34f90

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,36 @@ test('Can be called consecutively on returned result of previous call', t => {
8787
'',
8888
]);
8989
});
90+
91+
/**
92+
* This was to address #4, where having a match at the end of a string was
93+
* causing the first replacement to return an array where the last element was
94+
* ''. This was causing an error where I was checking for !str, even though an
95+
* empty string should actually be allwed.
96+
*/
97+
test('Allows empty strings within results', t => {
98+
let replacedContent;
99+
const string = '@username http://a_photo.jpg';
100+
101+
replacedContent = replaceString(string, /(http?:\/\/.*\.(?:png|jpg))/g, match => {
102+
return { key: 'image', match };
103+
});
104+
105+
t.deepEqual(replacedContent, [
106+
'@username ',
107+
{ key: 'image', match: 'http://a_photo.jpg' },
108+
'',
109+
]);
110+
111+
replacedContent = replaceString(replacedContent, /@(\w+)/g, match => {
112+
return { key: 'text', match };
113+
});
114+
115+
t.deepEqual(replacedContent, [
116+
'',
117+
{ key: 'text', match: 'username' },
118+
' ',
119+
{ key: 'image', match: 'http://a_photo.jpg' },
120+
'',
121+
]);
122+
});

0 commit comments

Comments
 (0)