Replace initialjs-rails with custom avatar code

The initialjs-rails gem hasn't been maintained for years, and it
currently requires `railties < 7.0`, meaning we can't upgrade to Rails 7
while we depend on it.

Since the code in the gem is simple, and we were already rewriting its
most complex part (generating a background color), we can implement the
same code, only we're using Ruby instead of JavaScript. This way, the
avatars will be shown on browsers without JavaScript as well. Since
we're adding a component test that checks SVG images are displayed even
without JavaScript, we no longer need the test that checked images were
displayed after AJAX requests.

Now the tests show the user experience better; people don't care about
the internal name used to select the initial (which is what we were
checking); they care about the initial actually displayed.

Note initialjs generated an <img> tag using a `src="data:image/svg+xml;`
attribute. We're generating an <svg> tag instead, because it's easier.
For this reason, we need to change the code slightly, giving the <svg>
tag the `img` role and using `aria-label` so its contents won't be read
aloud by screen readers. We could give it a `presentation` role instead
and forget about `aria-label`, but then screen readers would read the
text anyway (or, at least, some of them would).
This commit is contained in:
Javi Martín
2024-03-29 20:01:45 +01:00
parent d3b1b21d3d
commit 35659d4419
15 changed files with 81 additions and 51 deletions

View File

@@ -1,10 +1,22 @@
require "rails_helper"
describe Shared::AvatarComponent do
it "does not contain redundant text already present around it" do
render_inline Shared::AvatarComponent.new(double(id: 1, name: "Johnny"))
let(:user) { double(id: 1, name: "Johnny") }
let(:component) { Shared::AvatarComponent.new(user) }
expect(page).to have_css "img", count: 1
expect(page).to have_css "img[alt='']"
it "does not contain redundant text already present around it" do
render_inline component
expect(page).to have_css "svg", count: 1
expect(page).to have_css "svg[role='img'][aria-label='']"
end
it "shows the initial letter of the name" do
render_inline component
page.find("svg") do |avatar|
expect(avatar).to have_text "J"
expect(avatar).not_to have_text "Johnny"
end
end
end