asciinema.org/spec/presenters/asciicast_page_presenter_spec.rb

198 lines
4.6 KiB
Ruby
Raw Normal View History

2014-08-30 17:38:47 +00:00
require 'rails_helper'
2014-01-18 10:44:13 +00:00
describe AsciicastPagePresenter do
2014-01-18 10:44:13 +00:00
describe '.build' do
subject { described_class.build(asciicast, user, playback_options) }
let(:asciicast) { stub_model(Asciicast, decorate: decorated_asciicast) }
let(:user) { double('user') }
let(:playback_options) { { speed: 3.0 } }
let(:decorated_asciicast) { double('decorated_asciicast', theme_name: 'foo') }
2014-02-01 09:29:49 +00:00
it "builds presenter with given asciicast decorated" do
expect(subject.asciicast).to be(decorated_asciicast)
end
2014-02-01 09:29:49 +00:00
it "builds presenter with given user" do
expect(subject.current_user).to be(user)
end
2014-02-01 09:29:49 +00:00
it "builds presenter with given playback options" do
expect(subject.playback_options.speed).to eq(3.0)
expect(subject.playback_options.theme).to eq('foo')
end
end
2014-07-05 12:59:42 +00:00
let(:presenter) { described_class.new(asciicast, current_user, policy, nil) }
let(:asciicast) { stub_model(Asciicast, user: author) }
2014-01-18 10:44:13 +00:00
let(:current_user) { User.new }
2014-07-05 12:59:42 +00:00
let(:policy) { double('policy') }
let(:author) { User.new }
2014-01-18 10:44:13 +00:00
let(:view_context) {
controller = ApplicationController.new
controller.request = ActionController::TestRequest.new
controller.view_context
}
describe '#title' do
subject { presenter.title }
before do
allow(asciicast).to receive(:title) { 'the-title' }
2014-01-18 10:44:13 +00:00
end
it { should eq('the-title') }
end
describe '#asciicast_title' do
subject { presenter.asciicast_title }
before do
allow(asciicast).to receive(:title) { 'the-title' }
2014-01-18 10:44:13 +00:00
end
it { should eq('the-title') }
end
describe '#author_img_link' do
subject { presenter.author_img_link }
before do
allow(asciicast).to receive(:author_img_link) { '<a href=...>' }
2014-01-18 10:44:13 +00:00
end
it { should eq('<a href=...>') }
end
describe '#author_link' do
subject { presenter.author_link }
before do
allow(asciicast).to receive(:author_link) { '<a href=...>' }
2014-01-18 10:44:13 +00:00
end
it { should eq('<a href=...>') }
end
describe '#asciicast_created_at' do
subject { presenter.asciicast_created_at }
let(:now) { Time.now }
before do
allow(asciicast).to receive(:created_at) { now }
2014-01-18 10:44:13 +00:00
end
it { should eq(now) }
end
describe '#asciicast_env_details' do
subject { presenter.asciicast_env_details }
before do
allow(asciicast).to receive(:os) { 'Linux' }
allow(asciicast).to receive(:shell) { 'bash' }
allow(asciicast).to receive(:terminal_type) { 'xterm' }
2014-01-18 10:44:13 +00:00
end
it { should eq('Linux / bash / xterm') }
end
describe '#views_count' do
subject { presenter.views_count }
before do
allow(asciicast).to receive(:views_count) { 5 }
2014-01-18 10:44:13 +00:00
end
it { should eq(5) }
end
describe '#embed_script' do
subject { presenter.embed_script(view_context) }
let(:src_regexp) { /src="[^"]+\b123\b[^"]*\.js"/ }
let(:id_regexp) { /id="asciicast-123"/ }
let(:script_regexp) {
/^<script[^>]+#{src_regexp}[^>]+#{id_regexp}[^>]*><\/script>/
}
before do
allow(asciicast).to receive(:id).and_return(123)
2014-01-18 10:44:13 +00:00
end
it 'is an async script tag including asciicast id' do
expect(subject).to match(script_regexp)
end
end
describe '#show_description?' do
subject { presenter.show_description? }
before do
allow(asciicast).to receive(:description) { description }
2014-01-18 10:44:13 +00:00
end
context "when description is present" do
let(:description) { 'i am description' }
it { should be(true) }
end
context "when description isn't present" do
let(:description) { '' }
it { should be(false) }
end
end
describe '#description' do
subject { presenter.description }
before do
allow(asciicast).to receive(:description) { 'i am description' }
2014-01-18 10:44:13 +00:00
end
it { should eq('i am description') }
end
describe '#show_other_asciicasts_by_author?' do
subject { presenter.show_other_asciicasts_by_author? }
before do
allow(author).to receive(:asciicast_count) { count }
end
context "when user has more than 1 asciicast" do
let(:count) { 2 }
it { should be(true) }
end
context "when user doesn't have more than 1 asciicasts" do
let(:count) { 1 }
it { should be(false) }
end
end
describe '#other_asciicasts_by_author' do
subject { presenter.other_asciicasts_by_author }
let(:others) { double('others', decorate: decorated_others) }
let(:decorated_others) { double('decorated_others') }
2014-01-18 10:44:13 +00:00
before do
allow(author).to receive(:asciicasts_excluding).
with(asciicast, 3) { others }
2014-01-18 10:44:13 +00:00
end
it "returns decorated asciicasts excluding the given one" do
expect(subject).to be(decorated_others)
2014-01-18 10:44:13 +00:00
end
end
end