rich_content.spec.js 13 KB

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