Skip to content

Commit

Permalink
[RUBY-3220] Add conditional validation for project name and refactor …
Browse files Browse the repository at this point in the history
…tests

- Updated `project_name_step.rb` to include a conditional validation for `name` presence.
- Refactored `project_name_step_spec.rb` to improve readability and organization.
- Grouped test cases logically and used more descriptive variable names.
  • Loading branch information
jjromeo committed Sep 3, 2024
1 parent ab50ae3 commit 18dbfb3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 54 deletions.
2 changes: 1 addition & 1 deletion app/steps/pafs_core/project_name_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ProjectNameStep < BasicStep
validates :name, format: {
with: /\A[A-Za-z0-9 _-]+\z/,
message: "The project name must only contain letters, underscores, hyphens and numbers"
}
}, if: -> { name.present? }

private

Expand Down
84 changes: 31 additions & 53 deletions spec/steps/pafs_core/project_name_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,44 @@
require "rails_helper"

RSpec.describe PafsCore::ProjectNameStep, type: :model do
describe "attributes" do
subject { build(:project_name_step) }

it_behaves_like "a project step"
it { is_expected.to validate_presence_of(:name).with_message("Tell us the project name") }

context "when validating name format" do
valid_names = [
"Project 123",
"PROJECT_ABC",
"project-xyz",
"Project With Spaces",
"123_PROJECT_456"
]

invalid_names = [
"Project/123",
"Project\\ABC",
"Project&XYZ",
"Project@Name",
"Project#123"
]

valid_names.each do |name|
it "accepts '#{name}'" do
subject.name = name
expect(subject).to be_valid
end
end

invalid_names.each do |name|
it "rejects '#{name}'" do
subject.name = name
expect(subject).not_to be_valid
expect(subject.errors[:name].join).to eq("The project name must only contain letters, underscores, hyphens and numbers")
end
end
end
end

describe "#update" do
subject { create(:project_name_step) }
subject(:project_name_step) { create(:project_name_step) }

let(:params) { ActionController::Parameters.new({ project_name_step: { name: "Wigwam waste water" } }) }
let(:error_params) { ActionController::Parameters.new({ project_name_step: { name: nil } }) }
let(:invalid_format_params) { ActionController::Parameters.new({ project_name_step: { name: "Invalid/Name" } }) }
let(:valid_params) { ActionController::Parameters.new({ project_name_step: { name: "Wigwam waste water" } }) }
let(:blank_name_params) { ActionController::Parameters.new({ project_name_step: { name: nil } }) }
let(:error_message) { "The project name must only contain letters, underscores, hyphens and numbers" }

it "saves the :name when valid" do
expect(subject.name).not_to eq "Wigwam waste water"
expect(subject.update(params)).to be true
expect(subject.name).to eq "Wigwam waste water"
context "when name is valid" do
it "returns true and saves the name" do
expect(project_name_step.name).not_to eq "Wigwam waste water"
expect(project_name_step.update(valid_params)).to be true
expect(project_name_step.name).to eq "Wigwam waste water"
end
end

it "returns false when validation fails due to blank name" do
expect(subject.update(error_params)).to be false
context "when name is blank" do
it "returns false and sets the correct error message" do
expect(project_name_step.update(blank_name_params)).to be false
expect(project_name_step.errors[:name].first).to eq("Tell us the project name")
end
end

it "returns false when validation fails due to invalid format" do
expect(subject.update(invalid_format_params)).to be false
expect(subject.errors[:name].join).to eq("The project name must only contain letters, underscores, hyphens and numbers")
context "when name has an invalid format" do
invalid_names = {
"slash" => "Invalid/Name",
"backslash" => "Invalid\\Name",
"ampersand" => "Invalid&Name",
"at symbol" => "Invalid@Name",
"hash symbol" => "Invalid#Name"
}

invalid_names.each do |description, name|
it "returns false and sets the correct error message for #{description} character" do
project_name_step.name = name
expect(project_name_step).not_to be_valid
expect(project_name_step.errors[:name].first).to eq(error_message)
end
end
end
end
end

0 comments on commit 18dbfb3

Please sign in to comment.