Skip to content

Commit

Permalink
Refract rspec test of email reseed to be more modular
Browse files Browse the repository at this point in the history
  • Loading branch information
perryzjc committed Feb 28, 2024
1 parent 82c5475 commit d00c889
Showing 1 changed file with 57 additions and 15 deletions.
72 changes: 57 additions & 15 deletions spec/controllers/email_templates_reseed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

# Setup phase: Mocks the SeedData.emails call to return predetermined email content.
# This mimics the initial seeding process with specific email templates before each test.
# The `load Rails.root.join("db/seeds.rb")` line simulates the seeding action.
before do
allow(SeedData).to receive(:emails).and_return(
[
Expand Down Expand Up @@ -62,29 +61,39 @@
subject: "Request Info Email"
}
])
# Initial seed with original data
# Simulates the seeding action.
load Rails.root.join("db/seeds.rb")
end

# This test verifies the entire reseed process by first checking the state after initial seeding,
# then mocking updated seed data, reseeding, and finally asserting the expected outcomes.
# It checks for the correct handling of both updated and removed email templates.
it "correctly handles email templates on reseed without duplication" do
# Verify original content
expect(EmailTemplate.count).to eq(2)
request_info_email = EmailTemplate.find_by(title: "Request Info Email")
expect(request_info_email).to be_present
expect(request_info_email.title).to eq("Request Info Email")
expect(request_info_email.body).to include("I am the original content of the request info email")

form_submission_email = EmailTemplate.find_by(title: "Form Submission")
expect(form_submission_email).to be_present
expect(form_submission_email.title).to eq("Form Submission")
expect(form_submission_email.body).to include("I am the original content of the form submission email")
end

# Simulate an update to the seed data by changing the content of one email template
# and removing another. This prepares for the reseed to test how the application handles
# updated and removed templates.
it "does not duplicate email templates on reseed with updated content" do
# Mock the SeedData.emails call to return updated email content for the "Request Info Email" template.
# This simulates the reseeding process with updated email content.
allow(SeedData).to receive(:emails).and_return(
[
{
to: "[email protected], [email protected]",
body: form_submission_old,
path: "teacher_mailer/form_submission",
locale: nil,
handler: "liquid",
partial: false,
format: "html",
title: "Form Submission",
required: true,
subject: "Form Submission"
},
{
body: request_info_email_new,
to: "{{teacher_email}}, {{teacher_personal_email}}",
Expand All @@ -100,14 +109,47 @@
])
load Rails.root.join("db/seeds.rb")

# Final assertions to verify that after reseeding, the email templates in the database
# reflect the updated seed data: updated content is present, and removed templates are absent.
expect(EmailTemplate.count).to eq(1)

# Verify updated content: Email count is still 2, with only updated content for the request info email.
expect(EmailTemplate.count).to eq(2)
request_info_email = EmailTemplate.find_by(title: "Request Info Email")
expect(request_info_email).to be_present
expect(request_info_email.title).to eq("Request Info Email")
expect(request_info_email.body).to include("NEW Template Content of the request info email")
expect(request_info_email.body).to include("I am NEW Template Content of the request info email")

form_submission_email = EmailTemplate.find_by(title: "Form Submission")
expect(form_submission_email).to be_present
expect(form_submission_email.title).to eq("Form Submission")
expect(form_submission_email.body).to include("I am the original content of the form submission email")
end

it "removes email templates not present in updated seed data" do
# Mock the SeedData.emails call to only return the "Request Info Email" template,
# simulating the removal of the "Form Submission" template from the seed data.
allow(SeedData).to receive(:emails).and_return(
[
{
body: request_info_email_old,
to: "{{teacher_email}}, {{teacher_personal_email}}",
path: "teacher_mailer/request_info_email",
locale: nil,
handler: "liquid",
partial: false,
format: "html",
title: "Request Info Email",
required: true,
subject: "Request Info Email"
}
])
load Rails.root.join("db/seeds.rb")

# Verify that the "Form Submission" email template is removed
expect(EmailTemplate.count).to eq(1)
expect(EmailTemplate.find_by(title: "Form Submission")).to be_nil

# Additionally, verify that the "Request Info Email" template remains and is unchanged
request_info_email = EmailTemplate.find_by(title: "Request Info Email")
expect(request_info_email).to be_present
expect(request_info_email.title).to eq("Request Info Email")
expect(request_info_email.body).to include("I am the original content of the request info email")
end
end

0 comments on commit d00c889

Please sign in to comment.