2021-12-26 13:07:42 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe SocialHelper::TwitterMethods, :type => :helper do
|
|
|
|
let(:user) { FactoryBot.create(:user) }
|
2022-01-04 16:00:18 -08:00
|
|
|
let(:question_content) { 'q' * 255 }
|
|
|
|
let(:answer_content) { 'a' * 255 }
|
2021-12-26 13:07:42 -08:00
|
|
|
let(:answer) { FactoryBot.create(:answer, user: user,
|
2022-01-04 16:00:18 -08:00
|
|
|
content: answer_content,
|
|
|
|
question_content: question_content) }
|
2021-12-26 13:07:42 -08:00
|
|
|
|
2021-12-27 05:15:36 -08:00
|
|
|
before do
|
|
|
|
stub_const("APP_CONFIG", {
|
|
|
|
'hostname' => 'example.com',
|
|
|
|
'https' => true,
|
|
|
|
'items_per_page' => 5
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2021-12-26 13:07:42 -08:00
|
|
|
describe '#prepare_tweet' do
|
|
|
|
context 'when the question and answer need to be shortened' do
|
|
|
|
subject { prepare_tweet(answer) }
|
|
|
|
|
|
|
|
it 'should return a properly formatted tweet' do
|
2022-07-22 14:20:21 -07:00
|
|
|
expect(subject).to eq("#{'q' * 123}… — #{'a' * 124}… https://example.com/@#{user.screen_name}/a/#{answer.id}")
|
2021-12-26 13:07:42 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-04 16:00:18 -08:00
|
|
|
context 'when a suffix has been passed' do
|
|
|
|
let(:question_content) { 'question' }
|
|
|
|
let(:answer_content) { 'answer' }
|
|
|
|
|
|
|
|
subject { prepare_tweet(answer, '#askracc') }
|
|
|
|
|
|
|
|
it 'should include the suffix after the link' do
|
2022-07-22 14:20:21 -07:00
|
|
|
expect(subject).to eq("question — answer #askracc https://example.com/@#{user.screen_name}/a/#{answer.id}")
|
2022-01-04 16:00:18 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a suffix has been passed and the tweet needs to be shortened' do
|
|
|
|
subject { prepare_tweet(answer, '#askracc') }
|
|
|
|
|
|
|
|
it 'should shorten the tweet while keeping the suffix intact' do
|
2022-07-22 14:20:21 -07:00
|
|
|
expect(subject).to eq("#{'q' * 120}… — #{'a' * 120}… #askracc https://example.com/@#{user.screen_name}/a/#{answer.id}")
|
2022-01-04 16:00:18 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-26 13:07:42 -08:00
|
|
|
context 'when the question and answer are short' do
|
|
|
|
before do
|
|
|
|
answer.question.content = 'Why are raccoons so good?'
|
|
|
|
answer.question.save!
|
|
|
|
answer.content = 'Because they are good cunes.'
|
|
|
|
answer.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { prepare_tweet(answer) }
|
|
|
|
|
|
|
|
it 'should return a properly formatted tweet' do
|
2022-07-22 14:20:21 -07:00
|
|
|
expect(subject).to eq("#{answer.question.content} — #{answer.content} https://example.com/@#{user.screen_name}/a/#{answer.id}")
|
2021-12-26 13:07:42 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#twitter_share_url' do
|
|
|
|
subject { twitter_share_url(answer) }
|
|
|
|
|
|
|
|
it 'should return a proper share link' do
|
|
|
|
expect(subject).to eq("https://twitter.com/intent/tweet?text=#{CGI.escape(prepare_tweet(answer))}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|