Skip to content

Commit

Permalink
New and better tests for adding facility reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJackson-Oslo committed Feb 16, 2024
1 parent 84bca47 commit bd2a6f9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 33 deletions.
26 changes: 18 additions & 8 deletions spec/features/user_creates_facility_reviews_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
Fabricate(:space, address: "Ulefossvegen 32", post_number: 3730, post_address: "Skien", lat: 59.196, lng: 9.603)
end

let(:facility_to_review) { space.relevant_space_facilities.first.facility }
let(:second_facility_to_review) { space.relevant_space_facilities.second.facility }
let!(:facility_to_review) { space.relevant_space_facilities.first.facility }
let!(:second_facility_to_review) { space.relevant_space_facilities.second.facility }

around do |test|
create_user!

login_and_logout_with_warden do
test.run
end
Expand All @@ -25,7 +26,7 @@

expect_to_add_facility_review(
space:,
facility_to_review:
facility: facility_to_review
)
end

Expand All @@ -34,23 +35,32 @@

expect_to_add_facility_review(
space:,
facility_to_review:,
facility: facility_to_review,
description_to_add: Faker::Lorem.sentence
)
end

it "two facilities, at the same time" do
visit space_path(space)

expect_to_add_multiple_facility_reviews(
space:,
facilities_to_review: [facility_to_review, second_facility_to_review]
)
end

it "two facilities, one at a time, and checks that changes are only made to the reviewed facilities" do
visit space_path(space)

expect_to_add_facility_review(
space:,
facility_to_review:
facility: facility_to_review
)

expect_to_add_facility_review(
count_that_already_have_the_new_experience: 1,
space:,
facility_to_review: second_facility_to_review
facility: second_facility_to_review
)
end

Expand All @@ -59,12 +69,12 @@

expect_to_add_facility_review(
space:,
facility_to_review:
facility: facility_to_review
)

expect_to_only_change_facility_review_description(
space:,
facility_to_review:,
facility: facility_to_review,
description_to_add: Faker::Lorem.sentence
)
end
Expand Down
93 changes: 68 additions & 25 deletions spec/support/user_creates_facility_reviews_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
module UserCreatesFacilityReviewsHelpers
def expect_to_only_change_facility_review_description(
space:,
facility_to_review:,
facility:,
description_to_add: ""
)

expected_previous_experience =
space.reload
.relevant_space_facilities
.find_by(facility: facility_to_review)
.find_by(facility:)
.experience

expect_to_add_facility_review(
space:,
facility_to_review:,
facility:,
description_to_add:,
count_that_already_have_the_new_experience: 1,
count_that_changes_to_new_experience: 0,
Expand All @@ -27,7 +27,7 @@ def expect_to_only_change_facility_review_description(
# rubocop:disable Metrics/ParameterLists # This is a test helper, it's fine
def expect_to_add_facility_review(
space:,
facility_to_review:,
facility:,
description_to_add: "",
count_that_already_have_the_new_experience: 0,
expected_previous_experience: "unknown",
Expand All @@ -36,7 +36,7 @@ def expect_to_add_facility_review(
)

expect_changes_around_reviewing_facility(
facility_to_review:,
facility:,
space:,
expected_previous_experience:,
expected_new_experience:,
Expand All @@ -45,42 +45,76 @@ def expect_to_add_facility_review(
count_that_changes_to_new_experience:
) do
click_to_create_new_facility_review(
facility_to_review:,
facility:,
description_to_add:
)
end
end
# rubocop:enable Metrics/ParameterLists

def click_to_create_new_facility_review(facility_to_review:,
def expect_to_add_multiple_facility_reviews(
space:,
facilities_to_review: []
)
expect_changes_around_reviewing_facility(
facility: facilities_to_review,
space:,
count_that_changes_to_new_experience: facilities_to_review.count
) do
click_to_create_multiple_facility_reviews(
facilities_to_review:
)
end
end

def click_to_create_new_facility_review(facility:,
description_to_add: "")
open_form_modal

enter_data_about_facility(facility:, description_to_add:)

save_form_modal
end

def click_to_create_multiple_facility_reviews(facilities_to_review: [])
open_form_modal

facilities_to_review.each do |facility|
enter_data_about_facility(facility:)
end

save_form_modal
end

def open_form_modal
# Click the edit button
find("button[aria-label='Rediger fasiliteter']").click
end

def save_form_modal
click_on(I18n.t("facility_reviews.save"))

# Wait for the save to go through
expect(page).to have_text(I18n.t("reviews.added_review"))
end

# For the chosen facility, add an experience, and conditionally a description:
fieldset = find("fieldset", text: facility_to_review.title)
def enter_data_about_facility(facility:, description_to_add: "")
fieldset = find("fieldset", text: facility.title)
within(fieldset) do
click_on("Rediger")
choose(I18n.t("reviews.form.facility_experience_particular_tense.was_allowed"), allow_label_click: true)

# Add a description
fill_in(I18n.t("simple_form.labels.space_facility.description"), with: description_to_add)
end

# And save:
click_on(I18n.t("facility_reviews.save"))

# Wait for the save to go through
expect(page).to have_text(I18n.t("reviews.added_review"))
end

# rubocop:disable Metrics/ParameterLists, Metrics/MethodLength, Metrics/AbcSize # This is a test helper, it's fine
def expect_changes_around_reviewing_facility(
facility_to_review:,
facility:,
space:,
count_that_already_have_the_new_experience:,
count_that_changes_to_new_experience:,
count_that_already_have_the_new_experience: 0,
count_that_changes_to_new_experience: 1,
count_of_visible_facilities: space.relevant_space_facilities.count,
expected_previous_experience: "unknown",
expected_new_experience: "likely",
Expand All @@ -101,6 +135,7 @@ def expect_changes_around_reviewing_facility(
reviewed_selector = "li[title='#{I18n.t("tooltips.facility_aggregated_experience.#{expected_new_experience}")}']"

relevant_space_facilities = space.relevant_space_facilities
space_facilities_matching_facility = relevant_space_facilities.where(facility:)

# Expect that we have more than one facility visible:
expect(count_of_visible_facilities).to be > 1
Expand All @@ -111,8 +146,10 @@ def expect_changes_around_reviewing_facility(
).to eq(count_that_already_have_the_new_experience)

expect(
relevant_space_facilities.find_by(facility: facility_to_review).experience
).to eq(expected_previous_experience)
space_facilities_matching_facility.reload.all? do |space_facility|
space_facility.experience == expected_previous_experience
end
).to be(true)

# And are not reviewed in the rendered HTML either:
expect(page).to have_selector(
Expand All @@ -125,8 +162,11 @@ def expect_changes_around_reviewing_facility(

# Check that the reviewed facility changed in the model:
expect(
relevant_space_facilities.find_by(facility: facility_to_review).experience
).to eq(expected_new_experience)
space_facilities_matching_facility.reload.all? do |space_facility|
space_facility.experience == expected_new_experience
end
).to be(true)

expect(
relevant_space_facilities.where(experience: expected_new_experience).count
).to eq(count_should_have_new_experience)
Expand All @@ -149,9 +189,12 @@ def expect_changes_around_reviewing_facility(
return if expected_new_description.blank?

# Check that the description changed in the model
expect(relevant_space_facilities.find_by(
facility: facility_to_review
).description).to eq(expected_new_description)

expect(
space_facilities_matching_facility.all? do |space_facility|
space_facility.description == expected_new_description
end
).to be(true)

# and in the rendered HTML:
expect(page).to have_text(expected_new_description)
Expand Down

0 comments on commit bd2a6f9

Please sign in to comment.