rich_content.spec.js 13 KB

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