rich_content.spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. '</hashtag-link-stub>',
  279. ' <hashtag-link-stub url="https://shitposter.club/tag/screencap" content="#screencap" tag="screencap">',
  280. '</hashtag-link-stub>',
  281. ' </p>'
  282. ].join('')
  283. const wrapper = shallowMount(RichContent, {
  284. global,
  285. props: {
  286. attentions,
  287. handleLinks: true,
  288. greentext: true,
  289. emoji: [],
  290. html
  291. }
  292. })
  293. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  294. })
  295. it('rich contents of a mention are handled properly', () => {
  296. attentions.push({ statusnet_profile_url: 'lol' })
  297. const html = [
  298. p(
  299. '<a href="lol" class="mention">',
  300. '<span>',
  301. 'https://</span>',
  302. '<span>',
  303. 'lol.tld/</span>',
  304. '<span>',
  305. '</span>',
  306. '</a>'
  307. ),
  308. p(
  309. 'Testing'
  310. )
  311. ].join('')
  312. const expected = [
  313. p(
  314. '<span class="MentionsLine">',
  315. '<span class="MentionLink mention-link">',
  316. '<!-- eslint-disable vue/no-v-html -->',
  317. '<a href="lol" class="original" target="_blank">',
  318. '<span>',
  319. 'https://</span>',
  320. '<span>',
  321. 'lol.tld/</span>',
  322. '<span>',
  323. '</span>',
  324. '</a>',
  325. '<!-- eslint-enable vue/no-v-html -->',
  326. '<!--v-if-->', // v-if placeholder, mentionlink's "new" (i.e. rich) display
  327. '</span>',
  328. '<!--v-if-->', // v-if placeholder, mentionsline's extra mentions and stuff
  329. '</span>'
  330. ),
  331. p(
  332. 'Testing'
  333. )
  334. ].join('')
  335. const wrapper = mount(RichContent, {
  336. global,
  337. props: {
  338. attentions,
  339. handleLinks: true,
  340. greentext: true,
  341. emoji: [],
  342. html
  343. }
  344. })
  345. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  346. })
  347. it('rich contents of nested mentions are handled properly', () => {
  348. attentions.push({ statusnet_profile_url: 'lol' })
  349. const html = [
  350. '<span class="poast-style">',
  351. '<a href="lol" class="mention">',
  352. '<span>',
  353. 'https://</span>',
  354. '<span>',
  355. 'lol.tld/</span>',
  356. '<span>',
  357. '</span>',
  358. '</a>',
  359. ' ',
  360. '<a href="lol" class="mention">',
  361. '<span>',
  362. 'https://</span>',
  363. '<span>',
  364. 'lol.tld/</span>',
  365. '<span>',
  366. '</span>',
  367. '</a>',
  368. ' ',
  369. '</span>',
  370. 'Testing'
  371. ].join('')
  372. const expected = [
  373. '<span class="poast-style">',
  374. '<span class="MentionsLine">',
  375. '<span class="MentionLink mention-link">',
  376. '<!-- eslint-disable vue/no-v-html -->',
  377. '<a href="lol" class="original" target="_blank">',
  378. '<span>',
  379. 'https://</span>',
  380. '<span>',
  381. 'lol.tld/</span>',
  382. '<span>',
  383. '</span>',
  384. '</a>',
  385. '<!-- eslint-enable vue/no-v-html -->',
  386. '<!--v-if-->', // v-if placeholder, mentionlink's "new" (i.e. rich) display
  387. '</span>',
  388. '<span class="MentionLink mention-link">',
  389. '<!-- eslint-disable vue/no-v-html -->',
  390. '<a href="lol" class="original" target="_blank">',
  391. '<span>',
  392. 'https://</span>',
  393. '<span>',
  394. 'lol.tld/</span>',
  395. '<span>',
  396. '</span>',
  397. '</a>',
  398. '<!-- eslint-enable vue/no-v-html -->',
  399. '<!--v-if-->', // v-if placeholder, mentionlink's "new" (i.e. rich) display
  400. '</span>',
  401. '<!--v-if-->', // v-if placeholder, mentionsline's extra mentions and stuff
  402. '</span>',
  403. ' ',
  404. '</span>',
  405. 'Testing'
  406. ].join('')
  407. const wrapper = mount(RichContent, {
  408. global,
  409. props: {
  410. attentions,
  411. handleLinks: true,
  412. greentext: true,
  413. emoji: [],
  414. html
  415. }
  416. })
  417. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  418. })
  419. it('rich contents of a link are handled properly', () => {
  420. const html = [
  421. '<p>',
  422. 'Freenode is dead.</p>',
  423. '<p>',
  424. '<a href="https://isfreenodedeadyet.com/">',
  425. '<span>',
  426. 'https://</span>',
  427. '<span>',
  428. 'isfreenodedeadyet.com/</span>',
  429. '<span>',
  430. '</span>',
  431. '</a>',
  432. '</p>'
  433. ].join('')
  434. const expected = [
  435. '<p>',
  436. 'Freenode is dead.</p>',
  437. '<p>',
  438. '<a href="https://isfreenodedeadyet.com/" target="_blank">',
  439. '<span>',
  440. 'https://</span>',
  441. '<span>',
  442. 'isfreenodedeadyet.com/</span>',
  443. '<span>',
  444. '</span>',
  445. '</a>',
  446. '</p>'
  447. ].join('')
  448. const wrapper = shallowMount(RichContent, {
  449. global,
  450. props: {
  451. attentions,
  452. handleLinks: true,
  453. greentext: true,
  454. emoji: [],
  455. html
  456. }
  457. })
  458. expect(wrapper.html().replace(/\n/g, '')).to.eql(compwrap(expected))
  459. })
  460. it.skip('[INFORMATIVE] Performance testing, 10 000 simple posts', () => {
  461. const amount = 20
  462. const onePost = p(
  463. makeMention('Lain'),
  464. makeMention('Lain'),
  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. ' i just landed in l a where are you'
  474. )
  475. const TestComponent = {
  476. template: `
  477. <div v-if="!vhtml">
  478. ${new Array(amount).fill(`<RichContent html="${onePost}" :greentext="true" :handleLinks="handeLinks" :emoji="[]" :attentions="attentions"/>`)}
  479. </div>
  480. <div v-else="vhtml">
  481. ${new Array(amount).fill(`<div v-html="${onePost}"/>`)}
  482. </div>
  483. `,
  484. props: ['handleLinks', 'attentions', 'vhtml']
  485. }
  486. console.log(1)
  487. const ptest = (handleLinks, vhtml) => {
  488. const t0 = performance.now()
  489. const wrapper = mount(TestComponent, {
  490. global,
  491. props: {
  492. attentions,
  493. handleLinks,
  494. vhtml
  495. }
  496. })
  497. const t1 = performance.now()
  498. wrapper.destroy()
  499. const t2 = performance.now()
  500. return `Mount: ${t1 - t0}ms, destroy: ${t2 - t1}ms, avg ${(t1 - t0) / amount}ms - ${(t2 - t1) / amount}ms per item`
  501. }
  502. console.log(`${amount} items with links handling:`)
  503. console.log(ptest(true))
  504. console.log(`${amount} items without links handling:`)
  505. console.log(ptest(false))
  506. console.log(`${amount} items plain v-html:`)
  507. console.log(ptest(false, true))
  508. })
  509. })