rich_content.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. ' ',
  318. '<!---->', // v-if placeholder, mentionlink's "new" (i.e. rich) display
  319. '</span>',
  320. '<!---->', // v-if placeholder, mentionsline's extra mentions and stuff
  321. '</span>'
  322. ),
  323. p(
  324. 'Testing'
  325. )
  326. ].join('')
  327. const wrapper = mount(RichContent, {
  328. localVue,
  329. propsData: {
  330. attentions,
  331. handleLinks: true,
  332. greentext: true,
  333. emoji: [],
  334. html
  335. }
  336. })
  337. expect(wrapper.html()).to.eql(compwrap(expected))
  338. })
  339. it('rich contents of a link are handled properly', () => {
  340. const html = [
  341. '<p>',
  342. 'Freenode is dead.</p>',
  343. '<p>',
  344. '<a href="https://isfreenodedeadyet.com/">',
  345. '<span>',
  346. 'https://</span>',
  347. '<span>',
  348. 'isfreenodedeadyet.com/</span>',
  349. '<span>',
  350. '</span>',
  351. '</a>',
  352. '</p>'
  353. ].join('')
  354. const expected = [
  355. '<p>',
  356. 'Freenode is dead.</p>',
  357. '<p>',
  358. '<a href="https://isfreenodedeadyet.com/" target="_blank">',
  359. '<span>',
  360. 'https://</span>',
  361. '<span>',
  362. 'isfreenodedeadyet.com/</span>',
  363. '<span>',
  364. '</span>',
  365. '</a>',
  366. '</p>'
  367. ].join('')
  368. const wrapper = shallowMount(RichContent, {
  369. localVue,
  370. propsData: {
  371. attentions,
  372. handleLinks: true,
  373. greentext: true,
  374. emoji: [],
  375. html
  376. }
  377. })
  378. expect(wrapper.html()).to.eql(compwrap(expected))
  379. })
  380. it.skip('[INFORMATIVE] Performance testing, 10 000 simple posts', () => {
  381. const amount = 20
  382. const onePost = p(
  383. makeMention('Lain'),
  384. makeMention('Lain'),
  385. makeMention('Lain'),
  386. makeMention('Lain'),
  387. makeMention('Lain'),
  388. makeMention('Lain'),
  389. makeMention('Lain'),
  390. makeMention('Lain'),
  391. makeMention('Lain'),
  392. makeMention('Lain'),
  393. ' i just landed in l a where are you'
  394. )
  395. const TestComponent = {
  396. template: `
  397. <div v-if="!vhtml">
  398. ${new Array(amount).fill(`<RichContent html="${onePost}" :greentext="true" :handleLinks="handeLinks" :emoji="[]" :attentions="attentions"/>`)}
  399. </div>
  400. <div v-else="vhtml">
  401. ${new Array(amount).fill(`<div v-html="${onePost}"/>`)}
  402. </div>
  403. `,
  404. props: ['handleLinks', 'attentions', 'vhtml']
  405. }
  406. console.log(1)
  407. const ptest = (handleLinks, vhtml) => {
  408. const t0 = performance.now()
  409. const wrapper = mount(TestComponent, {
  410. localVue,
  411. propsData: {
  412. attentions,
  413. handleLinks,
  414. vhtml
  415. }
  416. })
  417. const t1 = performance.now()
  418. wrapper.destroy()
  419. const t2 = performance.now()
  420. return `Mount: ${t1 - t0}ms, destroy: ${t2 - t1}ms, avg ${(t1 - t0) / amount}ms - ${(t2 - t1) / amount}ms per item`
  421. }
  422. console.log(`${amount} items with links handling:`)
  423. console.log(ptest(true))
  424. console.log(`${amount} items without links handling:`)
  425. console.log(ptest(false))
  426. console.log(`${amount} items plain v-html:`)
  427. console.log(ptest(false, true))
  428. })
  429. })