Extract method to get a user segment name

We're going to add geozones as user segments, so it's handy to have the
method in the UserSegments class.

We're also changing the `user_segment_emails` parameter name for
consistency and simplicity.
This commit is contained in:
Javi Martín
2021-11-10 20:35:46 +01:00
parent a0416d4d85
commit 78e543f6d3
7 changed files with 36 additions and 18 deletions

View File

@@ -5,6 +5,22 @@ describe UserSegments do
let(:user2) { create(:user) }
let(:user3) { create(:user) }
describe ".segment_name" do
it "returns a readable name of the segment" do
expect(UserSegments.segment_name("all_users")).to eq "All users"
expect(UserSegments.segment_name("administrators")).to eq "Administrators"
expect(UserSegments.segment_name("proposal_authors")).to eq "Proposal authors"
end
it "accepts symbols as parameters" do
expect(UserSegments.segment_name(:all_users)).to eq "All users"
end
it "returns nil for invalid segments" do
expect(UserSegments.segment_name("invalid")).to be nil
end
end
describe ".all_users" do
it "returns all active users enabled" do
active_user = create(:user)

View File

@@ -215,15 +215,15 @@ describe "Admin Notifications", :admin do
end
scenario "Select list of users to send notification" do
UserSegments.segments.each do |user_segment|
segment_recipient = I18n.t("admin.segment_recipient.#{user_segment}")
UserSegments.segments.each do |segment|
segment_recipient = UserSegments.segment_name(segment)
visit new_admin_admin_notification_path
fill_in_admin_notification_form(segment_recipient: segment_recipient)
click_button "Create notification"
expect(page).to have_content(I18n.t("admin.segment_recipient.#{user_segment}"))
expect(page).to have_content segment_recipient
end
end
end

View File

@@ -16,7 +16,7 @@ describe "Admin newsletter emails", :admin do
expect(page).to have_link "Go back", href: admin_newsletters_path
expect(page).to have_content "This is a subject"
expect(page).to have_content I18n.t("admin.segment_recipient.#{newsletter.segment_recipient}")
expect(page).to have_content "All users"
expect(page).to have_content "no-reply@consul.dev"
expect(page).to have_content "This is a body"
end
@@ -40,10 +40,9 @@ describe "Admin newsletter emails", :admin do
expect(page).to have_css(".newsletter", count: 3)
newsletters.each do |newsletter|
segment_recipient = I18n.t("admin.segment_recipient.#{newsletter.segment_recipient}")
within("#newsletter_#{newsletter.id}") do
expect(page).to have_content newsletter.subject
expect(page).to have_content segment_recipient
expect(page).to have_content UserSegments.segment_name(newsletter.segment_recipient)
end
end
end
@@ -162,13 +161,15 @@ describe "Admin newsletter emails", :admin do
end
scenario "Select list of users to send newsletter" do
UserSegments.segments.each do |user_segment|
UserSegments.segments.each do |segment|
segment_recipient = UserSegments.segment_name(segment)
visit new_admin_newsletter_path
fill_in_newsletter_form(segment_recipient: I18n.t("admin.segment_recipient.#{user_segment}"))
fill_in_newsletter_form(segment_recipient: segment_recipient)
click_button "Create Newsletter"
expect(page).to have_content(I18n.t("admin.segment_recipient.#{user_segment}"))
expect(page).to have_content segment_recipient
end
end
end