docbrown/spec/system/comments/user_views_a_comment_spec.rb
thejwuscript fea90d2e84
Fix inconsistent published date of Article and Comment (#18412)
* Revise system specs for viewing a comment

* Add system specs for viewing comment date

* Add conditional to SetTimeZone middleware

* Revise comment_date partial

* Refactor viewing a comment system specs

* Add more system specs for viewing a comment

* Fix format of comment date with short year

* Fix edited comment date

* Fix comment date when posting new comments
2022-09-09 14:12:15 -06:00

69 lines
1.9 KiB
Ruby

require "rails_helper"
RSpec.describe "Viewing a comment", type: :system, js: true do
let(:user) { create(:user) }
let(:article) { create(:article, user_id: user.id, show_comments: true) }
let(:comment) { create(:comment, commentable: article, user: user) }
before do
Timecop.freeze
ENV["DESYNC_TIMEZONE"] = "true"
mock_user_tz = ActiveSupport::TimeZone[Zonebie.random_timezone].tzinfo.name
ENV["TZ"] = mock_user_tz
end
after do
ENV["TZ"] = Time.zone.tzinfo.name
ENV["DESYNC_TIMEZONE"] = nil
Capybara.current_session.quit
Timecop.return
end
context "when viewing the comment date" do
it "contains a time tag with the correct value for the datetime attribute" do
sign_in user
visit comment.path
timestamp = comment.decorate.published_timestamp
expect(page).to have_selector(".comment-date time[datetime='#{timestamp}']")
end
it "shows the published date in the user's local time zone" do
sign_in user
visit comment.path
date = comment.created_at.getlocal.strftime("%b %-d")
expect(page).to have_selector(".comment-date time", text: date)
end
end
context "when a year has passed" do
before do
comment
Timecop.freeze(1.year.from_now)
end
it "shows the published date in the correct format" do
sign_in user
visit comment.path
date = comment.created_at.getlocal.strftime("%b %-d '%y")
expect(page).to have_selector(".comment-date time", text: date)
end
end
context "when the comment is edited and a year has passed" do
before do
comment.update(body_markdown: "This message is edited.", edited_at: 1.day.from_now)
Timecop.freeze(1.year.from_now)
end
it "shows the edited date in the correct format" do
sign_in user
visit comment.path
date = comment.edited_at.getlocal.strftime("%b %-d")
expect(page).to have_content("Edited on #{date}")
end
end
end