Skip to content

Commit

Permalink
Update integration test for non-JS scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtrussler committed Jan 9, 2025
1 parent 28ecf3c commit 4e7e002
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 49 deletions.
163 changes: 114 additions & 49 deletions test/integration/edition_edit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -591,59 +591,124 @@ class EditionEditTest < IntegrationTest
click_link "Related external links"
end

should "show 'Related external links' header" do
within :css, "h2.gem-c-heading" do
assert page.has_text?("Related external links")
end
should "render 'Related external links' header, inset text and save button" do
assert page.has_css?("h2", text: "Related external links")
assert page.has_css?("div.gem-c-inset-text", text: "After saving, changes to related external links will be visible on the site the next time this publication is published.")
assert page.has_css?("button.gem-c-button", text: "Save")
end
end

without_javascript do
context "Related external links tab (without JavaScript)" do
context "Document has no external links when page loads" do
setup do
visit_draft_edition
click_link "Related external links"
end

should "render an empty 'Add another' form" do
# Link 1
assert page.has_css?("legend", text: "Link 1")
assert page.has_css?("input[name='artefact[external_links_attributes][0][_destroy]']")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_0_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_0_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][0][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][0][url]']").value

context "Document has no external links" do
should "show an empty 'Add another' form" do
# Link 1
assert page.has_css?("legend", text: "Link 1")
assert page.has_css?("input[name='artefact[external_links_attributes][0][_destroy]']")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_0_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_0_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][0][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][0][url]']").value

# Link 2 (empty fields)
assert page.has_css?("legend", text: "Link 2")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_1_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_1_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][url]']").value

assert page.has_css?("div.gem-c-inset-text", text: "After saving, changes to related external links will be visible on the site the next time this publication is published.")
assert page.has_css?("button.gem-c-button", text: "Save")
# Link 2 (empty fields)
assert page.has_css?("legend", text: "Link 2")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_1_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_1_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][url]']").value
end
end
end

context "Document already has external links" do
setup do
visit_draft_edition
@draft_edition.artefact.external_links = [{title: "Link One", url: "https://gov.uk"}]
click_link "Related external links"
end

should "show a pre-populated 'Add another' form" do
# Link 1
assert page.has_css?("legend", text: "Link 1")
assert page.has_css?("input[name='artefact[external_links_attributes][0][_destroy]']")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_0_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_0_url']").text
assert_equal "Link One", page.find("input[name='artefact[external_links_attributes][0][title]']").value
assert_equal "https://gov.uk", page.find("input[name='artefact[external_links_attributes][0][url]']").value

# Link 2 (empty fields)
assert page.has_css?("legend", text: "Link 2")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_1_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_1_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][url]']").value

assert page.has_css?("div.gem-c-inset-text", text: "After saving, changes to related external links will be visible on the site the next time this publication is published.")
assert page.has_css?("button.gem-c-button", text: "Save")
context "Document already has external links when page loads" do
setup do
visit_draft_edition
@draft_edition.artefact.external_links = [{ title: "Link One", url: "https://gov.uk" }]
click_link "Related external links"
end

should "render a pre-populated 'Add another' form" do
# Link 1
assert page.has_css?("legend", text: "Link 1")
assert page.has_css?("input[name='artefact[external_links_attributes][0][_destroy]']")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_0_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_0_url']").text
assert_equal "Link One", page.find("input[name='artefact[external_links_attributes][0][title]']").value
assert_equal "https://gov.uk", page.find("input[name='artefact[external_links_attributes][0][url]']").value

# Link 2 (empty fields)
assert page.has_css?("legend", text: "Link 2")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_1_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_1_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][url]']").value
end
end

context "User adds a new external link and saves" do
setup do
visit_draft_edition
click_link "Related external links"
end

should "render a prepopulated 'Add another' form" do
within :css, ".gem-c-add-another .js-add-another__fieldset:first-of-type" do
fill_in "Title", with: "A new external link"
fill_in "URL", with: "https://foo.com"
end

click_button("Save")

# Link 1
assert page.has_css?("legend", text: "Link 1")
assert page.has_css?("input[name='artefact[external_links_attributes][0][_destroy]']")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_0_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_0_url']").text
assert_equal "A new external link", page.find("input[name='artefact[external_links_attributes][0][title]']").value
assert_equal "https://foo.com", page.find("input[name='artefact[external_links_attributes][0][url]']").value

# Link 2 (empty fields)
assert page.has_css?("legend", text: "Link 2")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_1_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_1_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][url]']").value
end
end

context "User deletes an external link and saves" do
setup do
visit_draft_edition
@draft_edition.artefact.external_links = [{ title: "Link One", url: "https://gov.uk" }]
click_link "Related external links"
end

should "render an empty 'Add another' form" do
within :css, ".gem-c-add-another .js-add-another__fieldset:first-of-type" do
check("Delete")
end

click_button("Save")

# Link 1
assert page.has_css?("legend", text: "Link 1")
assert page.has_css?("input[name='artefact[external_links_attributes][0][_destroy]']")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_0_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_0_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][0][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][0][url]']").value

# Link 2 (empty fields)
assert page.has_css?("legend", text: "Link 2")
assert_equal "Title", page.find("label[for='artefact_external_links_attributes_1_title']").text
assert_equal "URL", page.find("label[for='artefact_external_links_attributes_1_url']").text
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][title]']").value
assert_equal "", page.find("input[name='artefact[external_links_attributes][1][url]']").value
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/integration_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ def filter_by_content_type(option, from: "Content type")
end
end

def with_javascript
context "with javascript" do
setup do
Capybara.current_driver = Capybara.javascript_driver
end

yield
end
end

def without_javascript
context "without javascript" do
setup do
Capybara.use_default_driver
end

yield
end
end

class JavascriptIntegrationTest < IntegrationTest
setup do
Capybara.current_driver = Capybara.javascript_driver
Expand Down

0 comments on commit 4e7e002

Please sign in to comment.