Skip to content

Commit

Permalink
Add specs for subcommand project use view
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Jan 22, 2024
1 parent 067c520 commit ae8c3dc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/dsu/views/project/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def render
end
rescue StandardError => e
puts apply_theme(e.message, theme_color: color_theme.error)
puts apply_theme(e.backtrace_locations.join("\n"), theme_color: color_theme.error) if Dsu.env.local?
end

private
Expand Down
70 changes: 59 additions & 11 deletions spec/dsu/views/project/create_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# rubocop:disable RSpec/MultipleMemoizedHelpers
# frozen_string_literal: true

# rubocop:disable RSpec/MultipleMemoizedHelpers
RSpec.describe Dsu::Views::Project::Create do
subject(:create_view) do
described_class.new(presenter: presenter, options: options)
end

before do
allow($stdin).to receive(:getch).and_return(response)
end

let(:presenter) do
build(:create_presenter, project_name: project_name, description: description, options: options)
end
Expand All @@ -23,6 +27,53 @@
end

describe '#render' do
context 'when the project already exists' do
let(:presenter) do
build(:create_presenter, :with_project, project_name: project_name, description: description, options: options)
end
let(:expected_error) do
"Project \"#{project_name}\" already exists."
end

it 'displays the error' do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
create_view.render
end.chomp)).to include(expected_error)
end
end

context 'when the project does not exist' do
context "when the user confirmation is 'y'" do
let(:response) { 'y' }

it 'displays the project created message' do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
create_view.render
end.chomp)).to include("Created project \"#{project_name}\".")
end

it 'creates the project' do
create_view.render
expect(Dsu::Models::Project.exist?(project_name: project_name)).to be(true)
end
end

context "when the user confirmation is 'n'" do
let(:response) { 'n' }

it 'displays the cancelled message' do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
create_view.render
end.chomp)).to include('Cancelled.')
end

it 'does not create the project' do
create_view.render
expect(Dsu::Models::Project.exist?(project_name: project_name)).to be(false)
end
end
end

context 'when the project has errors' do
before do
allow(presenter).to receive_messages(project_errors?: true,
Expand All @@ -46,25 +97,22 @@
end
end

context 'when the project already exists' do
# before do
# allow(presenter).to receive_messages(project_already_exists?: true)
# end
context 'when an error is raised that is not rescued' do
before do
allow(presenter).to receive(:project_errors?).and_raise(StandardError, expected_error)
end

let(:presenter) do
build(:create_presenter, :with_project, project_name: project_name, description: description, options: options)
end
let(:expected_error) do
"Project \"#{project_name}\" already exists."
build(:create_presenter, project_name: project_name, description: description, options: options)
end
let(:expected_error) { 'Test error' }

it 'displays the error' do
it 'rescues and displays the error' do
expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do
create_view.render
end.chomp)).to include(expected_error)
end
end
end
end

# rubocop:enable RSpec/MultipleMemoizedHelpers
6 changes: 3 additions & 3 deletions spec/dsu/views/project/use_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
build(:use_presenter, :with_project_name, project_name_or_number: project_name, options: options)
end

context "when user the confirmation is 'y'" do
context "when the user confirmation is 'y'" do
let(:response) { 'y' }

it_behaves_like 'the project is not the current project'
Expand Down Expand Up @@ -95,7 +95,7 @@
build(:use_presenter, :without_project_name, options: options)
end

context "when user the confirmation is 'y'" do
context "when the user confirmation is 'y'" do
let(:response) { 'y' }

it_behaves_like 'the project is the current project'
Expand Down Expand Up @@ -143,7 +143,7 @@
end
end

context 'when an error is raised that is not captured' do
context 'when an error is raised that is not rescued' do
before do
allow(presenter).to receive(:project_does_not_exist?).and_raise(StandardError, expected_error)
end
Expand Down

0 comments on commit ae8c3dc

Please sign in to comment.