rich_content.spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. import { mount, shallowMount } from '@vue/test-utils'
  2. import RichContent from 'src/components/rich_content/rich_content.jsx'
  3. const attentions = []
  4. const global = {
  5. mocks: {
  6. $store: {
  7. state: {},
  8. getters: {
  9. mergedConfig: () => ({
  10. mentionLinkShowTooltip: true
  11. }),
  12. findUserByUrl: () => null
  13. }
  14. }
  15. },
  16. stubs: {
  17. FAIcon: true
  18. }
  19. }
  20. const makeMention = (who, noClass) => {
  21. attentions.push({ statusnet_profile_url: `https://fake.tld/@${who}` })
  22. return noClass
  23. ? `<span><a href="https://fake.tld/@${who}">@<span>${who}</span></a></span>`
  24. : `<span class="h-card"><a class="u-url mention" href="https://fake.tld/@${who}">@<span>${who}</span></a></span>`
  25. }
  26. const p = (...data) => `<p>${data.join('')}</p>`
  27. const compwrap = (...data) => `<span class="RichContent">${data.join('')}</span>`
  28. const mentionsLine = (times) => [
  29. '<mentions-line-stub mentions="',
  30. new Array(times).fill('[object Object]').join(','),
  31. '"></mentions-line-stub>'
  32. ].join('')
  33. describe('RichContent', () => {
  34. it('renders simple post without exploding', () => {
  35. const html = p('Hello world!')
  36. const wrapper = shallowMount(RichContent, {
  37. global,
  38. props: {
  39. attentions,
  40. handleLinks: true,
  41. greentext: true,
  42. emoji: [],
  43. html
  44. }
  45. })
  46. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(html))
  47. })
  48. it('unescapes everything as needed', () => {
  49. const html = [
  50. p('Testing &#39;em all'),
  51. 'Testing &#39;em all'
  52. ].join('')
  53. const expected = [
  54. p('Testing \'em all'),
  55. 'Testing \'em all'
  56. ].join('')
  57. const wrapper = shallowMount(RichContent, {
  58. global,
  59. props: {
  60. attentions,
  61. handleLinks: true,
  62. greentext: true,
  63. emoji: [],
  64. html
  65. }
  66. })
  67. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  68. })
  69. it('replaces mention with mentionsline', () => {
  70. const html = p(
  71. makeMention('John'),
  72. ' how are you doing today?'
  73. )
  74. const wrapper = shallowMount(RichContent, {
  75. global,
  76. props: {
  77. attentions,
  78. handleLinks: true,
  79. greentext: true,
  80. emoji: [],
  81. html
  82. }
  83. })
  84. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(p(
  85. mentionsLine(1),
  86. ' how are you doing today?'
  87. )))
  88. })
  89. it('replaces mentions at the end of the hellpost', () => {
  90. const html = [
  91. p('How are you doing today, fine gentlemen?'),
  92. p(
  93. makeMention('John'),
  94. makeMention('Josh'),
  95. makeMention('Jeremy')
  96. )
  97. ].join('')
  98. const expected = [
  99. p(
  100. 'How are you doing today, fine gentlemen?'
  101. ),
  102. // TODO fix this extra line somehow?
  103. p(
  104. '<mentions-line-stub mentions="',
  105. '[object Object],',
  106. '[object Object],',
  107. '[object Object]',
  108. '"></mentions-line-stub>'
  109. )
  110. ].join('')
  111. const wrapper = shallowMount(RichContent, {
  112. global,
  113. props: {
  114. attentions,
  115. handleLinks: true,
  116. greentext: true,
  117. emoji: [],
  118. html
  119. }
  120. })
  121. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  122. })
  123. it('Does not touch links if link handling is disabled', () => {
  124. const html = [
  125. [
  126. makeMention('Jack'),
  127. 'let\'s meet up with ',
  128. makeMention('Janet')
  129. ].join(''),
  130. [
  131. makeMention('John'),
  132. makeMention('Josh'), makeMention('Jeremy')
  133. ].join('')
  134. ].join('\n')
  135. const strippedHtml = [
  136. [
  137. makeMention('Jack', true),
  138. 'let\'s meet up with ',
  139. makeMention('Janet', true)
  140. ].join(''),
  141. [
  142. makeMention('John', true),
  143. makeMention('Josh', true), makeMention('Jeremy', true)
  144. ].join('')
  145. ].join('\n')
  146. const wrapper = shallowMount(RichContent, {
  147. global,
  148. props: {
  149. attentions,
  150. handleLinks: false,
  151. greentext: true,
  152. emoji: [],
  153. html
  154. }
  155. })
  156. expect(wrapper.html()).to.eql(compwrap(strippedHtml))
  157. })
  158. it('Adds greentext and cyantext to the post', () => {
  159. const html = [
  160. '&gt;preordering videogames',
  161. '&gt;any year'
  162. ].join('\n')
  163. const expected = [
  164. '<span class="greentext">&gt;preordering videogames</span>',
  165. '<span class="greentext">&gt;any year</span>'
  166. ].join('\n')
  167. const wrapper = shallowMount(RichContent, {
  168. global,
  169. props: {
  170. attentions,
  171. handleLinks: false,
  172. greentext: true,
  173. emoji: [],
  174. html
  175. }
  176. })
  177. expect(wrapper.html()).to.eql(compwrap(expected))
  178. })
  179. it('Does not add greentext and cyantext if setting is set to false', () => {
  180. const html = [
  181. '&gt;preordering videogames',
  182. '&gt;any year'
  183. ].join('\n')
  184. const wrapper = shallowMount(RichContent, {
  185. global,
  186. props: {
  187. attentions,
  188. handleLinks: false,
  189. greentext: false,
  190. emoji: [],
  191. html
  192. }
  193. })
  194. expect(wrapper.html()).to.eql(compwrap(html))
  195. })
  196. it('Adds emoji to post', () => {
  197. const html = p('Ebin :DDDD :spurdo:')
  198. const expected = p(
  199. 'Ebin :DDDD ',
  200. '<anonymous-stub src="about:blank" alt=":spurdo:" class="emoji img" title=":spurdo:"></anonymous-stub>'
  201. )
  202. const wrapper = shallowMount(RichContent, {
  203. global,
  204. props: {
  205. attentions,
  206. handleLinks: false,
  207. greentext: false,
  208. emoji: [{ url: 'about:blank', shortcode: 'spurdo' }],
  209. html
  210. }
  211. })
  212. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  213. })
  214. it('Doesn\'t add nonexistent emoji to post', () => {
  215. const html = p('Lol :lol:')
  216. const wrapper = shallowMount(RichContent, {
  217. global,
  218. props: {
  219. attentions,
  220. handleLinks: false,
  221. greentext: false,
  222. emoji: [],
  223. html
  224. }
  225. })
  226. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(html))
  227. })
  228. it('Greentext + last mentions', () => {
  229. const html = [
  230. '&gt;quote',
  231. makeMention('lol'),
  232. '&gt;quote',
  233. '&gt;quote'
  234. ].join('\n')
  235. const expected = [
  236. '<span class="greentext">&gt;quote</span>',
  237. mentionsLine(1),
  238. '<span class="greentext">&gt;quote</span>',
  239. '<span class="greentext">&gt;quote</span>'
  240. ].join('\n')
  241. const wrapper = shallowMount(RichContent, {
  242. global,
  243. props: {
  244. attentions,
  245. handleLinks: true,
  246. greentext: true,
  247. emoji: [],
  248. html
  249. }
  250. })
  251. expect(wrapper.html()).to.eql(compwrap(expected))
  252. })
  253. it('One buggy example', () => {
  254. const html = [
  255. 'Bruh',
  256. 'Bruh',
  257. [
  258. makeMention('foo'),
  259. makeMention('bar'),
  260. makeMention('baz')
  261. ].join(''),
  262. 'Bruh'
  263. ].join('<br>')
  264. const expected = [
  265. 'Bruh',
  266. 'Bruh',
  267. mentionsLine(3),
  268. 'Bruh'
  269. ].join('<br>')
  270. const wrapper = shallowMount(RichContent, {
  271. global,
  272. props: {
  273. attentions,
  274. handleLinks: true,
  275. greentext: true,
  276. emoji: [],
  277. html
  278. }
  279. })
  280. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  281. })
  282. it('buggy example/hashtags', () => {
  283. const html = [
  284. '<p>',
  285. '<a href="http://macrochan.org/images/N/H/NHCMDUXJPPZ6M3Z2CQ6D2EBRSWGE7MZY.jpg">',
  286. 'NHCMDUXJPPZ6M3Z2CQ6D2EBRSWGE7MZY.jpg</a>',
  287. ' <a class="hashtag" data-tag="nou" href="https://shitposter.club/tag/nou">',
  288. '#nou</a>',
  289. ' <a class="hashtag" data-tag="screencap" href="https://shitposter.club/tag/screencap">',
  290. '#screencap</a>',
  291. ' </p>'
  292. ].join('')
  293. const expected = [
  294. '<p>',
  295. '<a href="http://macrochan.org/images/N/H/NHCMDUXJPPZ6M3Z2CQ6D2EBRSWGE7MZY.jpg" target="_blank">',
  296. 'NHCMDUXJPPZ6M3Z2CQ6D2EBRSWGE7MZY.jpg</a>',
  297. ' <hashtag-link-stub url="https://shitposter.club/tag/nou" content="#nou" tag="nou">',
  298. '</hashtag-link-stub>',
  299. ' <hashtag-link-stub url="https://shitposter.club/tag/screencap" content="#screencap" tag="screencap">',
  300. '</hashtag-link-stub>',
  301. ' </p>'
  302. ].join('')
  303. const wrapper = shallowMount(RichContent, {
  304. global,
  305. props: {
  306. attentions,
  307. handleLinks: true,
  308. greentext: true,
  309. emoji: [],
  310. html
  311. }
  312. })
  313. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  314. })
  315. it('rich contents of a mention are handled properly', () => {
  316. attentions.push({ statusnet_profile_url: 'lol' })
  317. const html = [
  318. p(
  319. '<a href="lol" class="mention">',
  320. '<span>',
  321. 'https://</span>',
  322. '<span>',
  323. 'lol.tld/</span>',
  324. '<span>',
  325. '</span>',
  326. '</a>'
  327. ),
  328. p(
  329. 'Testing'
  330. )
  331. ].join('')
  332. const expected = [
  333. p(
  334. '<span class="MentionsLine">',
  335. '<span class="MentionLink mention-link">',
  336. '<a href="lol" class="original" target="_blank">',
  337. '<span>',
  338. 'https://</span>',
  339. '<span>',
  340. 'lol.tld/</span>',
  341. '<span>',
  342. '</span>',
  343. '</a>',
  344. '</span>',
  345. '</span>'
  346. ),
  347. p(
  348. 'Testing'
  349. )
  350. ].join('')
  351. const wrapper = mount(RichContent, {
  352. global,
  353. props: {
  354. attentions,
  355. handleLinks: true,
  356. greentext: true,
  357. emoji: [],
  358. html
  359. }
  360. })
  361. expect(wrapper.html().replace(/\n/g, '').replace(/<!--.*?-->/g, '')).to.eql(compwrap(expected))
  362. })
  363. it('rich contents of nested mentions are handled properly', () => {
  364. attentions.push({ statusnet_profile_url: 'lol' })
  365. const html = [
  366. '<span class="poast-style">',
  367. '<a href="lol" class="mention">',
  368. '<span>',
  369. 'https://</span>',
  370. '<span>',
  371. 'lol.tld/</span>',
  372. '<span>',
  373. '</span>',
  374. '</a>',
  375. ' ',
  376. '<a href="lol" class="mention">',
  377. '<span>',
  378. 'https://</span>',
  379. '<span>',
  380. 'lol.tld/</span>',
  381. '<span>',
  382. '</span>',
  383. '</a>',
  384. ' ',
  385. '</span>',
  386. 'Testing'
  387. ].join('')
  388. const expected = [
  389. '<span>',
  390. '<span class="MentionsLine">',
  391. '<span class="MentionLink mention-link">',
  392. '<a href="lol" class="original" target="_blank">',
  393. '<span>',
  394. 'https://</span>',
  395. '<span>',
  396. 'lol.tld/</span>',
  397. '<span>',
  398. '</span>',
  399. '</a>',
  400. '</span>',
  401. '<span class="MentionLink mention-link">',
  402. '<a href="lol" class="original" target="_blank">',
  403. '<span>',
  404. 'https://</span>',
  405. '<span>',
  406. 'lol.tld/</span>',
  407. '<span>',
  408. '</span>',
  409. '</a>',
  410. '</span>',
  411. '</span>',
  412. ' ',
  413. '</span>',
  414. 'Testing'
  415. ].join('')
  416. const wrapper = mount(RichContent, {
  417. global,
  418. props: {
  419. attentions,
  420. handleLinks: true,
  421. greentext: true,
  422. emoji: [],
  423. html
  424. }
  425. })
  426. expect(wrapper.html().replace(/\n/g, '').replace(/<!--.*?-->/g, '')).to.eql(compwrap(expected))
  427. })
  428. it('rich contents of a link are handled properly', () => {
  429. const html = [
  430. '<p>',
  431. 'Freenode is dead.</p>',
  432. '<p>',
  433. '<a href="https://isfreenodedeadyet.com/">',
  434. '<span>',
  435. 'https://</span>',
  436. '<span>',
  437. 'isfreenodedeadyet.com/</span>',
  438. '<span>',
  439. '</span>',
  440. '</a>',
  441. '</p>'
  442. ].join('')
  443. const expected = [
  444. '<p>',
  445. 'Freenode is dead.</p>',
  446. '<p>',
  447. '<a href="https://isfreenodedeadyet.com/" target="_blank">',
  448. '<span>',
  449. 'https://</span>',
  450. '<span>',
  451. 'isfreenodedeadyet.com/</span>',
  452. '<span>',
  453. '</span>',
  454. '</a>',
  455. '</p>'
  456. ].join('')
  457. const wrapper = shallowMount(RichContent, {
  458. global,
  459. props: {
  460. attentions,
  461. handleLinks: true,
  462. greentext: true,
  463. emoji: [],
  464. html
  465. }
  466. })
  467. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  468. })
  469. it.skip('[INFORMATIVE] Performance testing, 10 000 simple posts', () => {
  470. const amount = 20
  471. const onePost = p(
  472. makeMention('Lain'),
  473. makeMention('Lain'),
  474. makeMention('Lain'),
  475. makeMention('Lain'),
  476. makeMention('Lain'),
  477. makeMention('Lain'),
  478. makeMention('Lain'),
  479. makeMention('Lain'),
  480. makeMention('Lain'),
  481. makeMention('Lain'),
  482. ' i just landed in l a where are you'
  483. )
  484. const TestComponent = {
  485. template: `
  486. <div v-if="!vhtml">
  487. ${new Array(amount).fill(`<RichContent html="${onePost}" :greentext="true" :handleLinks="handeLinks" :emoji="[]" :attentions="attentions"/>`)}
  488. </div>
  489. <div v-else="vhtml">
  490. ${new Array(amount).fill(`<div v-html="${onePost}"/>`)}
  491. </div>
  492. `,
  493. props: ['handleLinks', 'attentions', 'vhtml']
  494. }
  495. console.log(1)
  496. const ptest = (handleLinks, vhtml) => {
  497. const t0 = performance.now()
  498. const wrapper = mount(TestComponent, {
  499. global,
  500. props: {
  501. attentions,
  502. handleLinks,
  503. vhtml
  504. }
  505. })
  506. const t1 = performance.now()
  507. wrapper.destroy()
  508. const t2 = performance.now()
  509. return `Mount: ${t1 - t0}ms, destroy: ${t2 - t1}ms, avg ${(t1 - t0) / amount}ms - ${(t2 - t1) / amount}ms per item`
  510. }
  511. console.log(`${amount} items with links handling:`)
  512. console.log(ptest(true))
  513. console.log(`${amount} items without links handling:`)
  514. console.log(ptest(false))
  515. console.log(`${amount} items plain v-html:`)
  516. console.log(ptest(false, true))
  517. })
  518. })