The button was announced as expanded when the form was hidden and as
collapsed when the form was shown.
This is because Foundation sets the expanded attribute based on whether
the class to toggle already exists. Since initially the form had the
"hide" class and the button toggled that class, Foundation checked that
the class was already present and so set the button as expanded.
So we're changing the toggler class for a class we don't use at all,
just so Foundation initiall sets `aria-expanded=false` and then changes
it to `aria-expanded=true` after the button is clicked. Then we're
ignoring this class completely and are styling this form with CSS
instead.
We could also use a toggler class like "visible" and write something
like:
```
.add-related-content + form:not(.visible) {
display: none;
}
```
However, using ARIA attributes is more robust as it guarantees the
styles will always be in sync with what screen reader users experience.
And we could also remove all the Foundation toggler functionality and
use our own JavaScript to handle the button state. We might do so in the
future.
143 lines
4.8 KiB
Ruby
143 lines
4.8 KiB
Ruby
shared_examples "relationable" do |relationable_model_name|
|
|
let(:relationable) { create(relationable_model_name.name.parameterize(separator: "_").to_sym) }
|
|
let(:related1) { create([:proposal, :debate, :budget_investment].sample) }
|
|
let(:related2) { create([:proposal, :debate, :budget_investment].sample) }
|
|
let(:user) { create(:user) }
|
|
let!(:url) { Setting["url"] }
|
|
|
|
scenario "related contents are listed" do
|
|
create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
|
|
|
visit relationable.url
|
|
within("#related-content-list") do
|
|
expect(page).to have_content related1.title
|
|
end
|
|
|
|
visit related1.url
|
|
within("#related-content-list") do
|
|
expect(page).to have_content relationable.title
|
|
end
|
|
end
|
|
|
|
scenario "related contents list is not rendered if there are no relations" do
|
|
visit relationable.url
|
|
expect(page).not_to have_css "#related-content-list"
|
|
end
|
|
|
|
scenario "related contents can be added" do
|
|
login_as(user)
|
|
visit relationable.url
|
|
|
|
expect(page).not_to have_css "#related_content"
|
|
expect(page).to have_css ".add-related-content[aria-expanded='false']"
|
|
|
|
click_button "Add related content"
|
|
|
|
expect(page).to have_css ".add-related-content[aria-expanded='true']"
|
|
|
|
within("#related_content") do
|
|
fill_in "url", with: "#{url + related1.url}"
|
|
click_button "Add"
|
|
end
|
|
|
|
within("#related-content-list") do
|
|
expect(page).to have_content related1.title
|
|
end
|
|
|
|
visit related1.url
|
|
|
|
within("#related-content-list") do
|
|
expect(page).to have_content relationable.title
|
|
end
|
|
|
|
click_button "Add related content"
|
|
|
|
within("#related_content") do
|
|
fill_in "url", with: "#{url + related2.url}"
|
|
click_button "Add"
|
|
end
|
|
|
|
within("#related-content-list") do
|
|
expect(page).to have_content related2.title
|
|
end
|
|
end
|
|
|
|
scenario "if related content URL is invalid returns error" do
|
|
login_as(user)
|
|
visit relationable.url
|
|
|
|
click_button "Add related content"
|
|
|
|
within("#related_content") do
|
|
fill_in "url", with: "http://invalidurl.com"
|
|
click_button "Add"
|
|
end
|
|
|
|
expect(page).to have_content "Link not valid. Remember to start with #{url}."
|
|
end
|
|
|
|
scenario "returns error when relating content URL to itself" do
|
|
login_as(user)
|
|
visit relationable.url
|
|
|
|
click_button "Add related content"
|
|
|
|
within("#related_content") do
|
|
fill_in "url", with: url + relationable.url.to_s
|
|
click_button "Add"
|
|
end
|
|
|
|
expect(page).to have_content "Link not valid. You cannot relate a content to itself"
|
|
end
|
|
|
|
scenario "related content can be scored positively" do
|
|
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
|
|
|
login_as(user)
|
|
visit relationable.url
|
|
|
|
within("#related-content-list") do
|
|
find("#related-content-#{related_content.opposite_related_content.id}").hover
|
|
find("#score-positive-related-#{related_content.opposite_related_content.id}").click
|
|
expect(page).not_to have_css "#score-positive-related-#{related_content.opposite_related_content.id}"
|
|
end
|
|
|
|
expect(related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.id).value).to eq(1)
|
|
expect(related_content.opposite_related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.opposite_related_content.id).value).to eq(1)
|
|
end
|
|
|
|
scenario "related content can be scored negatively" do
|
|
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
|
|
|
login_as(user)
|
|
visit relationable.url
|
|
|
|
within("#related-content-list") do
|
|
find("#related-content-#{related_content.opposite_related_content.id}").hover
|
|
find("#score-negative-related-#{related_content.opposite_related_content.id}").click
|
|
expect(page).not_to have_css "#score-negative-related-#{related_content.opposite_related_content.id}"
|
|
end
|
|
|
|
expect(related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.id).value).to eq(-1)
|
|
expect(related_content.opposite_related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.opposite_related_content.id).value).to eq(-1)
|
|
end
|
|
|
|
scenario "if related content has negative score it will be hidden" do
|
|
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
|
|
|
2.times do
|
|
related_content.send("score_positive", build(:user))
|
|
end
|
|
|
|
6.times do
|
|
related_content.send("score_negative", build(:user))
|
|
end
|
|
|
|
login_as(user)
|
|
|
|
visit relationable.url
|
|
|
|
expect(page).not_to have_css "#related-content-list"
|
|
end
|
|
end
|