rich_content.spec.js 14 KB

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