# frozen_string_literal: true RSpec.describe PrettyText do def n(html) html.strip end it "supports multi choice polls" do cooked = PrettyText.cook <<~MD [poll type=multiple min=1 max=3 public=true] * option 1 * option 2 * option 3 [/poll] MD expect(cooked).to include( '
', ) end it "can dynamically generate a poll" do cooked = PrettyText.cook <<~MD [poll type=number min=1 max=20 step=1] [/poll] MD expect(cooked.scan("test 2. test 2 [/poll] MD loose_cooked = PrettyText.cook <<~MD [poll type=multiple] 1. test 1 :) test 2. test 2 [/poll] MD tight_hashes = tight_cooked.scan(/data-poll-option-id=['"]([^'"]+)/) loose_hashes = loose_cooked.scan(/data-poll-option-id=['"]([^'"]+)/) expect(tight_hashes.size).to eq(2) expect(tight_hashes).to eq(loose_hashes) end it "can correctly cook polls" do cooked = PrettyText.cook <<~MD [poll type=multiple] 1. test 1 :) test 2. test 2 [/poll] MD expected = <<~HTML
  1. test 1 :slight_smile: test
  2. test 2
0 voters
HTML # note, hashes should remain stable even if emoji changes cause text content is hashed expect(n cooked).to eq(n expected) end it "can onebox posts" do post = Fabricate(:post, raw: <<~MD) A post with a poll [poll type=regular] * Hello * World [/poll] MD onebox = Oneboxer.onebox_raw(post.full_url, user_id: Fabricate(:user).id) expect(onebox[:preview]).to include("A post with a poll") expect(onebox[:preview]).to include("poll") end it "can reduce excerpts" do post = Fabricate(:post, raw: <<~MD) A post with a poll [poll type=regular] * Hello * World [/poll] MD excerpt = PrettyText.excerpt(post.cooked, SiteSetting.post_onebox_maxlength, post: post) expect(excerpt).to eq("A post with a poll \npoll") excerpt = PrettyText.excerpt(post.cooked, SiteSetting.post_onebox_maxlength) expect(excerpt).to eq("A post with a poll \npoll") end it "supports the title attribute" do cooked = PrettyText.cook <<~MD [poll] # What's your favorite *berry*? :wink: https://google.com/ * Strawberry * Raspberry * Blueberry [/poll] MD expect(cooked).to include <<~HTML
What’s your favorite berry? :wink: https://google.com/
HTML end it "supports polls in block quotes" do cooked = PrettyText.cook <<~MD [quote] [poll] * Strawberry * Raspberry * Blueberry [/poll] [/quote] MD expect(cooked).to include "
" expect(cooked).to include '
' end it "supports polls in quotes" do cooked = PrettyText.cook <<~MD > [poll] > * Strawberry > * Raspberry > * Blueberry > [/poll] MD expect(cooked).to include "
" expect(cooked).to include '
' end it "supports polls in details" do cooked = PrettyText.cook <<~MD [details=please vote] [poll] * Strawberry * Raspberry * Blueberry [/poll] [/details] MD expect(cooked).to include "
" expect(cooked).to include '
' end it "does not break when there are headings before/after a poll with a title" do cooked = PrettyText.cook <<~MD # Pre-heading [poll] # What's your favorite *berry*? :wink: https://google.com/ * Strawberry * Raspberry * Blueberry [/poll] # Post-heading MD expect(cooked).to include <<~HTML
What’s your favorite berry? :wink: https://google.com/
HTML expect(cooked).to include( '

Pre-heading

', ) expect(cooked).to include( '

Post-heading

', ) end it "does not break when there are headings before/after a poll without a title" do cooked = PrettyText.cook <<~MD # Pre-heading [poll] * Strawberry * Raspberry * Blueberry [/poll] # Post-heading MD expect(cooked).to_not include('
') expect(cooked).to include('
') expect(cooked).to include( '

Pre-heading

', ) expect(cooked).to include( '

Post-heading

', ) end end