-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
361 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`text linkify attach link on arrays containing elements that has link 1`] = ` | ||
Array [ | ||
Array [ | ||
"Please go to ", | ||
<a | ||
href="http://google.com" | ||
> | ||
http://google.com | ||
</a>, | ||
"", | ||
], | ||
<span> | ||
Please go to | ||
<a | ||
href="http://google.com" | ||
> | ||
http://google.com | ||
</a> | ||
</span>, | ||
] | ||
`; | ||
|
||
exports[`text linkify attach link on nested elements containing links 1`] = ` | ||
<span> | ||
Please go to | ||
<span> | ||
<a | ||
href="http://google.com" | ||
> | ||
http://google.com | ||
</a> | ||
</span> | ||
lalala | ||
</span> | ||
`; | ||
|
||
exports[`text linkify attach link on simple elements containing links 1`] = ` | ||
<span> | ||
Please go to | ||
<a | ||
href="http://google.com" | ||
> | ||
http://google.com | ||
</a> | ||
</span> | ||
`; | ||
|
||
exports[`text linkify attach link on strings containing links 1`] = ` | ||
Array [ | ||
"Please go to ", | ||
<a | ||
href="http://google.com" | ||
> | ||
http://google.com | ||
</a>, | ||
"", | ||
] | ||
`; | ||
|
||
exports[`text linkify decodes URI encoded URLs 1`] = ` | ||
Array [ | ||
"Please go to ", | ||
<a | ||
href="http://www.rumtoast.com/5444/line%E7%BE%A4%E7%B5%84%E8%A1%8C%E5%8B%95%E6%A2%9D%E7%A2%BC%E9%82%80%E8%AB%8B-%E4%B8%80%E5%AE%9A%E8%A6%81%E9%97%9C%E6%8E%89%EF%BC%8C%E4%B8%8D%E7%84%B6%E9%A7%AD%E5%AE%A2%E6%9C%83%E5%85%A5%E4%BE%B5" | ||
> | ||
http://www.rumtoast.com/5444/line群組行動條碼邀請-一定要關掉,不然駭客會入侵 | ||
</a>, | ||
"", | ||
] | ||
`; | ||
|
||
exports[`text linkify decodes URLS that is too long 1`] = ` | ||
Array [ | ||
"Please go to ", | ||
<a | ||
href="http://www.rumtoast.com/5444/line%E7%BE%A4%E7%B5%84%E8%A1%8C%E5%8B%95%E6%A2%9D%E7%A2%BC%E9%82%80%E8%AB%8B-%E4%B8%80%E5%AE%9A%E8%A6%81%E9%97%9C%E6%8E%89%EF%BC%8C%E4%B8%8D%E7%84%B6%E9%A7%AD%E5%AE%A2%E6%9C%83%E5%85%A5%E4%BE%B5" | ||
> | ||
http:⋯駭客會入侵 | ||
</a>, | ||
"", | ||
] | ||
`; | ||
|
||
exports[`text linkify sets prop to link 1`] = ` | ||
Array [ | ||
"Please go to ", | ||
<a | ||
href="http://google.com" | ||
target="_blank" | ||
> | ||
http://google.com | ||
</a>, | ||
"", | ||
] | ||
`; | ||
|
||
exports[`text nl2br inserts <br> on line breaks in strings, with ending <br>s trimmed 1`] = ` | ||
Array [ | ||
"Foo", | ||
<br />, | ||
"Bar", | ||
] | ||
`; | ||
exports[`text nl2br inserts <br> on line breaks in strings, with ending <br>s trimmed 2`] = ` | ||
Array [ | ||
"This should be first line with empty next line", | ||
<br />, | ||
<br />, | ||
"This should be second line with no <br> afterwards", | ||
] | ||
`; | ||
exports[`text nl2br inserts <br> on line breaks within elements 1`] = ` | ||
<p> | ||
This should be first line | ||
<br /> | ||
<br /> | ||
This should be second line with no <br> afterwards | ||
</p> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React from 'react'; | ||
import { linkify, nl2br } from '../text'; | ||
|
||
describe('text', () => { | ||
describe('linkify', () => { | ||
it('does nothing on strings, arrays and elements without links', () => { | ||
expect(linkify('foo')).toBe('foo'); | ||
|
||
const singleLevelElem = <p>foo</p>; | ||
expect(linkify(singleLevelElem)).toBe(singleLevelElem); | ||
|
||
const nestedElem = <p>foo <span> bar </span> foo2</p>; | ||
expect(linkify(nestedElem)).toBe(nestedElem); | ||
|
||
const array = ['foo', 'bar', <span key="elem">lala</span>]; | ||
expect(linkify(array)).toEqual(array); | ||
}); | ||
|
||
it('attach link on strings containing links', () => { | ||
expect(linkify('Please go to http://google.com')).toMatchSnapshot(); | ||
}); | ||
|
||
it('attach link on simple elements containing links', () => { | ||
expect( | ||
linkify(<span>Please go to http://google.com</span>) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('attach link on nested elements containing links', () => { | ||
expect( | ||
linkify( | ||
<span> | ||
Please go to <span>http://google.com</span> lalala | ||
</span> | ||
) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('attach link on arrays containing elements that has link', () => { | ||
expect( | ||
linkify([ | ||
'Please go to http://google.com', | ||
<span key="elem">Please go to http://google.com</span>, | ||
]) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('decodes URI encoded URLs', () => { | ||
expect( | ||
linkify( | ||
'Please go to http://www.rumtoast.com/5444/line%E7%BE%A4%E7%B5%84%E8%A1%8C%E5%8B%95%E6%A2%9D%E7%A2%BC%E9%82%80%E8%AB%8B-%E4%B8%80%E5%AE%9A%E8%A6%81%E9%97%9C%E6%8E%89%EF%BC%8C%E4%B8%8D%E7%84%B6%E9%A7%AD%E5%AE%A2%E6%9C%83%E5%85%A5%E4%BE%B5' | ||
) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('decodes URLS that is too long', () => { | ||
expect( | ||
linkify( | ||
'Please go to http://www.rumtoast.com/5444/line%E7%BE%A4%E7%B5%84%E8%A1%8C%E5%8B%95%E6%A2%9D%E7%A2%BC%E9%82%80%E8%AB%8B-%E4%B8%80%E5%AE%9A%E8%A6%81%E9%97%9C%E6%8E%89%EF%BC%8C%E4%B8%8D%E7%84%B6%E9%A7%AD%E5%AE%A2%E6%9C%83%E5%85%A5%E4%BE%B5', | ||
{ maxLength: 10 } | ||
) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('sets prop to link', () => { | ||
expect( | ||
linkify('Please go to http://google.com', { | ||
props: { target: '_blank' }, | ||
}) | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('nl2br', () => { | ||
it('does nothing on strings, arrays and elements without line breaks', () => { | ||
expect(nl2br('foo')).toBe('foo'); | ||
|
||
const singleLevelElem = <p>foo</p>; | ||
expect(nl2br(singleLevelElem)).toBe(singleLevelElem); | ||
|
||
const nestedElem = <p>foo <span> bar </span> foo2</p>; | ||
expect(nl2br(nestedElem)).toBe(nestedElem); | ||
|
||
const array = ['foo', 'bar', <span key="elem">lala</span>]; | ||
expect(nl2br(array)).toEqual(array); | ||
}); | ||
|
||
it('inserts <br> on line breaks in strings, with ending <br>s trimmed', () => { | ||
expect(nl2br('Foo\nBar')).toMatchSnapshot(); | ||
|
||
expect( | ||
nl2br(` \n | ||
This should be first line with empty next line | ||
This should be second line with no <br> afterwards \n | ||
`) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('inserts <br> on line breaks within elements', () => { | ||
expect( | ||
nl2br( | ||
<p> | ||
{` \n | ||
This should be first line | ||
This should be second line with no <br> afterwards \n | ||
`} | ||
</p> | ||
) | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.