Extract component to render avatars in comments

This way it'll be easier to add tests for it and refactor it.
This commit is contained in:
Javi Martín
2024-04-11 16:28:55 +02:00
parent 35659d4419
commit 2c9c5d9cd4
5 changed files with 79 additions and 15 deletions

View File

@@ -0,0 +1,56 @@
require "rails_helper"
describe Comments::AvatarComponent do
it "displays a regular avatar for regular comments" do
comment = create(:comment, user: create(:user, username: "Oscar Wilde"))
render_inline Comments::AvatarComponent.new(comment)
expect(page).to have_avatar "O"
expect(page).not_to have_css "img"
end
it "displays the admin avatar for admin comments" do
admin = create(:administrator)
comment = create(:comment, user: admin.user, administrator_id: admin.id)
render_inline Comments::AvatarComponent.new(comment)
expect(page).to have_css "img.admin-avatar"
end
it "displays the moderator avatar for moderator comments" do
moderator = create(:moderator)
comment = create(:comment, user: moderator.user, moderator_id: moderator.id)
render_inline Comments::AvatarComponent.new(comment)
expect(page).to have_css "img.moderator-avatar"
end
it "displays the organization avatar for organization comments" do
comment = create(:comment, user: create(:organization).user)
render_inline Comments::AvatarComponent.new(comment)
expect(page).to have_css "img.avatar"
end
it "displays an empty icon for comments by hidden users" do
comment = create(:comment, user: create(:user, :hidden))
render_inline Comments::AvatarComponent.new(comment)
expect(page).to have_css ".user-deleted"
expect(page).not_to have_css "img"
end
it "displays an empty icon for comments by erased users" do
comment = create(:comment, user: create(:user, erased_at: Time.current))
render_inline Comments::AvatarComponent.new(comment)
expect(page).to have_css ".user-deleted"
expect(page).not_to have_css "img"
end
end