2014-08-30 17:38:47 +00:00
|
|
|
require 'rails_helper'
|
2014-02-01 00:16:28 +00:00
|
|
|
|
2014-02-01 00:26:58 +00:00
|
|
|
describe UserPagePresenter do
|
2014-02-01 00:16:28 +00:00
|
|
|
|
|
|
|
describe '.build' do
|
|
|
|
subject { described_class.build(user, current_user, page, per_page) }
|
|
|
|
|
|
|
|
let(:user) { double('user', decorate: decorated_user) }
|
|
|
|
let(:decorated_user) { double('decorated_user') }
|
|
|
|
let(:current_user) { double('current_user') }
|
|
|
|
let(:page) { 2 }
|
|
|
|
let(:per_page) { 5 }
|
|
|
|
|
2014-02-01 09:29:49 +00:00
|
|
|
it "builds presenter with given user decorated" do
|
2014-02-01 00:16:28 +00:00
|
|
|
expect(subject.user).to be(decorated_user)
|
|
|
|
end
|
|
|
|
|
2014-02-01 09:29:49 +00:00
|
|
|
it "builds presenter with given current_user" do
|
2014-02-01 00:16:28 +00:00
|
|
|
expect(subject.current_user).to be(current_user)
|
|
|
|
end
|
|
|
|
|
2014-02-01 09:29:49 +00:00
|
|
|
it "builds presenter with given page" do
|
2014-02-01 00:16:28 +00:00
|
|
|
expect(subject.page).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when page is nil" do
|
|
|
|
let(:page) { nil }
|
|
|
|
|
2014-02-01 09:29:49 +00:00
|
|
|
it "builds presenter with page = 1" do
|
2014-02-01 00:16:28 +00:00
|
|
|
expect(subject.page).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-01 09:29:49 +00:00
|
|
|
it "builds presenter with given per_page" do
|
2014-02-01 00:16:28 +00:00
|
|
|
expect(subject.per_page).to eq(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when per_page is nil" do
|
|
|
|
let(:per_page) { nil }
|
|
|
|
|
2014-02-01 09:29:49 +00:00
|
|
|
it "builds presenter with per_page = PER_PAGE" do
|
2014-02-01 00:16:28 +00:00
|
|
|
expect(subject.per_page).to eq(described_class::PER_PAGE)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-05 12:59:42 +00:00
|
|
|
let(:presenter) { described_class.new(user, current_user, policy, page,
|
|
|
|
per_page) }
|
2014-09-23 19:01:40 +00:00
|
|
|
let(:user) { stub_model(User, username: 'cartman').decorate }
|
2014-07-05 12:59:42 +00:00
|
|
|
let(:current_user) { stub_model(User) }
|
|
|
|
let(:policy) { double('policy') }
|
2014-02-01 00:16:28 +00:00
|
|
|
let(:page) { 2 }
|
|
|
|
let(:per_page) { 5 }
|
|
|
|
|
|
|
|
let(:view_context) {
|
|
|
|
controller = ApplicationController.new
|
|
|
|
controller.request = ActionController::TestRequest.new
|
|
|
|
controller.view_context
|
|
|
|
}
|
|
|
|
|
|
|
|
describe '#title' do
|
|
|
|
subject { presenter.title }
|
|
|
|
|
|
|
|
it { should eq("cartman's profile") }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#user_full_name' do
|
|
|
|
subject { presenter.user_full_name }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:full_name) { 'E.C.' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should eq('E.C.') }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#user_joined_at' do
|
|
|
|
subject { presenter.user_joined_at }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:joined_at) { 'Jan 1, 1970' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should eq('Jan 1, 1970') }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#user_avatar_image_tag' do
|
|
|
|
subject { presenter.user_avatar_image_tag }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:avatar_image_tag) { '<img...>' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should eq('<img...>') }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#show_settings?' do
|
|
|
|
subject { presenter.show_settings? }
|
|
|
|
|
2014-07-05 12:59:42 +00:00
|
|
|
context "when policy allows for update" do
|
|
|
|
before do
|
|
|
|
allow(policy).to receive(:update?) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should be(true) }
|
2014-02-01 00:16:28 +00:00
|
|
|
end
|
|
|
|
|
2014-07-05 12:59:42 +00:00
|
|
|
context "when policy doesn't allow for update" do
|
|
|
|
before do
|
|
|
|
allow(policy).to receive(:update?) { false }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should be(false) }
|
|
|
|
end
|
2014-02-01 00:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#asciicast_count_text' do
|
|
|
|
subject { presenter.asciicast_count_text(view_context) }
|
|
|
|
|
2015-04-26 13:30:42 +00:00
|
|
|
context 'for non author' do
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:public_asciicast_count) { 2 }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should match(/2.+cartman/) }
|
2014-02-01 00:16:28 +00:00
|
|
|
end
|
|
|
|
|
2015-04-26 13:30:42 +00:00
|
|
|
context 'for author' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:asciicast_count) { 3 }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should match(/you.+3/i) }
|
|
|
|
end
|
2014-02-01 00:16:28 +00:00
|
|
|
end
|
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
describe '#user_username' do
|
|
|
|
subject { presenter.user_username }
|
2014-02-01 00:16:28 +00:00
|
|
|
|
|
|
|
it { should eq('cartman') }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#asciicasts' do
|
|
|
|
subject { presenter.asciicasts }
|
|
|
|
|
|
|
|
let(:collection) { [asciicast] }
|
|
|
|
let(:asciicast) { double('asciicast', decorate: double(title: 'quux')) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:paged_asciicasts) { collection }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gets user's asciicasts paged" do
|
|
|
|
subject
|
|
|
|
|
2015-04-26 13:30:42 +00:00
|
|
|
expect(user).to have_received(:paged_asciicasts).with(2, 5, false)
|
2014-02-01 00:16:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "wraps the asciicasts with paginating decorator" do
|
|
|
|
expect(subject).to respond_to(:current_page)
|
|
|
|
expect(subject).to respond_to(:total_pages)
|
|
|
|
expect(subject).to respond_to(:limit_value)
|
|
|
|
expect(subject.first.title).to eq('quux')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#current_users_profile?' do
|
|
|
|
subject { presenter.current_users_profile? }
|
|
|
|
|
|
|
|
context "when current_user is the same user" do
|
|
|
|
let(:current_user) { user }
|
|
|
|
|
|
|
|
it { should be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when current_user is a different user" do
|
2014-07-05 12:59:42 +00:00
|
|
|
let(:current_user) { stub_model(User) }
|
2014-02-01 00:16:28 +00:00
|
|
|
|
|
|
|
it { should be(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when current_user is nil" do
|
|
|
|
let(:current_user) { nil }
|
|
|
|
|
|
|
|
it { should be_falsy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|