|
| 1 | +import _ from 'lodash'; |
| 2 | +import Autolinker from '../src/autolinker'; |
| 3 | + |
| 4 | +describe( `Autolinker Hashtag Matching -`, () => { |
| 5 | + const autolinker = new Autolinker( { newWindow: false } ); |
| 6 | + const twitterAutolinker = new Autolinker( { hashtag: 'twitter', newWindow: false } ); |
| 7 | + const facebookAutolinker = new Autolinker( { hashtag: 'facebook', newWindow: false } ); |
| 8 | + const instagramAutolinker = new Autolinker( { hashtag: 'instagram', newWindow: false } ); |
| 9 | + |
| 10 | + const services = [ |
| 11 | + { serviceName: 'twitter', urlPrefix: 'https://twitter.com/hashtag', autolinker: twitterAutolinker }, |
| 12 | + { serviceName: 'instagram', urlPrefix: 'https://instagram.com/explore/tags', autolinker: instagramAutolinker }, |
| 13 | + { serviceName: 'facebook', urlPrefix: 'https://www.facebook.com/hashtag', autolinker: facebookAutolinker }, |
| 14 | + ]; |
| 15 | + |
| 16 | + |
| 17 | + it( `should NOT autolink hashtags by default for both backward compatibility, |
| 18 | + and because we don't know which service (twitter, facebook, etc.) to |
| 19 | + point them to`, |
| 20 | + () => { |
| 21 | + expect( autolinker.link( `#test` ) ).toBe( `#test` ); |
| 22 | + } ); |
| 23 | + |
| 24 | + |
| 25 | + it( `should NOT autolink hashtags the 'hashtag' cfg is explicitly false`, () => { |
| 26 | + const result = Autolinker.link( `#test`, { hashtag: false } ); |
| 27 | + |
| 28 | + expect( result ).toBe( `#test` ); |
| 29 | + } ); |
| 30 | + |
| 31 | + |
| 32 | + describe( 'all services hashtag tests', () => { |
| 33 | + |
| 34 | + services.forEach( ( { serviceName, urlPrefix, autolinker } ) => { |
| 35 | + |
| 36 | + it( `should automatically link hashtags to ${serviceName} when the |
| 37 | + 'hashtag' option is '${serviceName}'`, |
| 38 | + () => { |
| 39 | + const result = autolinker.link( `#test` ); |
| 40 | + |
| 41 | + expect( result ).toBe( `<a href="${urlPrefix}/test">#test</a>` ); |
| 42 | + } ); |
| 43 | + |
| 44 | + |
| 45 | + it( `should automatically link hashtags which are part of a full |
| 46 | + string when using the ${serviceName} service`, |
| 47 | + () => { |
| 48 | + const result = autolinker.link( `my hashtag is #test these days` ); |
| 49 | + |
| 50 | + expect( result ).toBe( `my hashtag is <a href="${urlPrefix}/test">#test</a> these days` ); |
| 51 | + } ); |
| 52 | + |
| 53 | + |
| 54 | + it( `should link a hashtag that is up to 139 characters long when |
| 55 | + using the ${serviceName} service`, |
| 56 | + () => { |
| 57 | + const aHashtag = _.repeat( 'a', 139 ); |
| 58 | + const bHashtag = _.repeat( 'b', 140 ); // too long - don't link |
| 59 | + |
| 60 | + const result = autolinker.link( `#${aHashtag} and #${bHashtag}` ); |
| 61 | + expect( result ).toBe( `<a href="${urlPrefix}/${aHashtag}">#${aHashtag}</a> and #${bHashtag}` ); |
| 62 | + } ); |
| 63 | + |
| 64 | + |
| 65 | + it( `should automatically link a hashtag with underscores when using |
| 66 | + the ${serviceName} service`, |
| 67 | + () => { |
| 68 | + const result = autolinker.link( `Yay, #hello_world` ); |
| 69 | + expect( result ).toBe( `Yay, <a href="${urlPrefix}/hello_world">#hello_world</a>` ); |
| 70 | + } ); |
| 71 | + |
| 72 | + |
| 73 | + it( `should automatically link a hashtag with accented characters |
| 74 | + when using the ${serviceName} service`, |
| 75 | + () => { |
| 76 | + const result = autolinker.link( `Yay, #mañana` ); |
| 77 | + expect( result ).toBe( `Yay, <a href="${urlPrefix}/mañana">#mañana</a>` ); |
| 78 | + } ); |
| 79 | + |
| 80 | + |
| 81 | + it( `should automatically link a hashtag with cyrillic characters |
| 82 | + when using the ${serviceName} service`, |
| 83 | + () => { |
| 84 | + const result = autolinker.link( `Yay, #Кириллица` ); |
| 85 | + expect( result ).toBe( `Yay, <a href="${urlPrefix}/Кириллица">#Кириллица</a>` ); |
| 86 | + } ); |
| 87 | + |
| 88 | + |
| 89 | + it( `should NOT automatically link a hashtag when the '#' belongs to |
| 90 | + part of another string when using the ${serviceName} service`, |
| 91 | + () => { |
| 92 | + const result = autolinker.link( `test as#df test` ); |
| 93 | + |
| 94 | + expect( result ).toBe( `test as#df test` ); |
| 95 | + } ); |
| 96 | + |
| 97 | + |
| 98 | + it( `should NOT automatically link a hashtag that is actually a |
| 99 | + named anchor within a URL when using the ${serviceName} service`, |
| 100 | + () => { |
| 101 | + const result = autolinker.link( `http://google.com/#link` ); |
| 102 | + |
| 103 | + expect( result ).toBe( `<a href="http://google.com/#link">google.com/#link</a>` ); |
| 104 | + } ); |
| 105 | + |
| 106 | + |
| 107 | + it( `should NOT automatically link a hashtag that is actually a |
| 108 | + named anchor within a URL **when URL linking is turned off** |
| 109 | + when using the ${serviceName} service`, |
| 110 | + () => { |
| 111 | + const noUrlTwitterHashtagAutolinker = new Autolinker( { |
| 112 | + urls: false, |
| 113 | + hashtag: 'twitter', |
| 114 | + newWindow: false |
| 115 | + } ); |
| 116 | + const result = noUrlTwitterHashtagAutolinker.link( `http://google.com/#link` ); |
| 117 | + |
| 118 | + expect( result ).toBe( `http://google.com/#link` ); |
| 119 | + } ); |
| 120 | + |
| 121 | + } ); |
| 122 | + |
| 123 | + } ); |
| 124 | + |
| 125 | +} ); |
0 commit comments