|
| 1 | +import {getLinkProps, isAbsoluteUrl, isLinkExternal} from './url'; |
| 2 | + |
| 3 | +describe('URL utils check', () => { |
| 4 | + test.each([ |
| 5 | + ['https://user:[email protected]:8080/p/a/t/h?query=string&query2=1#hash', true], |
| 6 | + ['http://example.net/path', true], |
| 7 | + ['/p/a/t/h?query=string&query2=1#hash', false], |
| 8 | + ['/path', false], |
| 9 | + ['path', false], |
| 10 | + ])("isAbsoluteUrl('%s') should return '%s'", (url, result) => { |
| 11 | + expect(isAbsoluteUrl(url)).toEqual(result); |
| 12 | + }); |
| 13 | + |
| 14 | + test.each([ |
| 15 | + [ |
| 16 | + 'https://user:[email protected]:8080/p/a/t/h?query=string&query2=1#hash', |
| 17 | + 'example.net', |
| 18 | + true, |
| 19 | + ], |
| 20 | + [ |
| 21 | + 'https://user:[email protected]:8080/p/a/t/h?query=string&query2=1#hash', |
| 22 | + 'sub.example.com', |
| 23 | + false, |
| 24 | + ], |
| 25 | + [ |
| 26 | + 'https://user:[email protected]:8080/p/a/t/h?query=string&query2=1#hash', |
| 27 | + undefined, |
| 28 | + true, |
| 29 | + ], |
| 30 | + ['http://example.net/path', 'example.net', false], |
| 31 | + ['http://example.net/path', 'sub.example.com', true], |
| 32 | + ['http://example.net/path', undefined, true], |
| 33 | + ['/p/a/t/h?query=string&query2=1#hash', 'example.net', false], |
| 34 | + ['/p/a/t/h?query=string&query2=1#hash', undefined, false], |
| 35 | + ['/path', 'example.net', false], |
| 36 | + ['/path', undefined, false], |
| 37 | + ['path', 'example.net', false], |
| 38 | + ['path', undefined, false], |
| 39 | + ])("isLinkExternal('%s', '%s') should return '%s'", (url, hostname, result) => { |
| 40 | + expect(isLinkExternal(url, hostname)).toEqual(result); |
| 41 | + }); |
| 42 | + |
| 43 | + test.each([ |
| 44 | + ['http://example.net/path', 'example.net', '_blank', {target: '_blank'}], |
| 45 | + ['http://example.net/path', 'example.net', undefined, {}], |
| 46 | + [ |
| 47 | + 'http://example.net/path', |
| 48 | + 'example.com', |
| 49 | + '_blank', |
| 50 | + {target: '_blank', rel: 'noopener noreferrer'}, |
| 51 | + ], |
| 52 | + [ |
| 53 | + 'http://example.net/path', |
| 54 | + 'example.com', |
| 55 | + undefined, |
| 56 | + {target: '_blank', rel: 'noopener noreferrer'}, |
| 57 | + ], |
| 58 | + [ |
| 59 | + 'http://example.net/path', |
| 60 | + undefined, |
| 61 | + '_blank', |
| 62 | + {target: '_blank', rel: 'noopener noreferrer'}, |
| 63 | + ], |
| 64 | + [ |
| 65 | + 'http://example.net/path', |
| 66 | + undefined, |
| 67 | + undefined, |
| 68 | + {target: '_blank', rel: 'noopener noreferrer'}, |
| 69 | + ], |
| 70 | + |
| 71 | + ['/path', 'example.net', '_blank', {target: '_blank'}], |
| 72 | + ['/path', 'example.net', undefined, {}], |
| 73 | + ['/path', undefined, '_blank', {target: '_blank'}], |
| 74 | + ['/path', undefined, undefined, {}], |
| 75 | + ])("getLinkProps('%s', '%s', '%s') should return '%s'", (url, hostname, target, result) => { |
| 76 | + expect(getLinkProps(url, hostname, target)).toEqual(result); |
| 77 | + }); |
| 78 | +}); |
0 commit comments