|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +module DocumentCollection |
| 4 | + class EmailNotificationsTest < ActionDispatch::IntegrationTest |
| 5 | + include GovukPersonalisation::TestHelpers::Features |
| 6 | + |
| 7 | + def schema_type |
| 8 | + "document_collection" |
| 9 | + end |
| 10 | + |
| 11 | + def taxonomy_topic_base_path |
| 12 | + "/taxonomy_topic_base_path" |
| 13 | + end |
| 14 | + |
| 15 | + def email_alert_frontend_signup_endpoint_no_account |
| 16 | + "/email-signup" |
| 17 | + end |
| 18 | + |
| 19 | + def email_alert_frontend_signup_endpoint_enforce_account |
| 20 | + "/email/subscriptions/single-page/new" |
| 21 | + end |
| 22 | + |
| 23 | + test "renders a signup link if the document collection has a taxonomy topic email override" do |
| 24 | + content_item = get_content_example("document_collection") |
| 25 | + content_item["links"]["taxonomy_topic_email_override"] = [{ "base_path" => taxonomy_topic_base_path.to_s }] |
| 26 | + stub_content_store_has_item(content_item["base_path"], content_item) |
| 27 | + visit_with_cachebust(content_item["base_path"]) |
| 28 | + assert page.has_css?(".gem-c-signup-link") |
| 29 | + assert page.has_link?(href: "/email-signup/confirm?topic=#{taxonomy_topic_base_path}") |
| 30 | + assert_not page.has_css?(".gem-c-single-page-notification-button") |
| 31 | + end |
| 32 | + |
| 33 | + test "renders the single page notification button with a form action of email-alert-frontend's non account signup endpoint" do |
| 34 | + setup_and_visit_content_item("document_collection") |
| 35 | + |
| 36 | + form = page.find("form.gem-c-single-page-notification-button") |
| 37 | + assert_match(email_alert_frontend_signup_endpoint_no_account, form["action"]) |
| 38 | + |
| 39 | + button = page.find(:button, class: "gem-c-single-page-notification-button__submit") |
| 40 | + |
| 41 | + expected_tracking = { |
| 42 | + "event_name" => "navigation", |
| 43 | + "type" => "subscribe", |
| 44 | + "index_link" => 1, |
| 45 | + "index_total" => 2, |
| 46 | + "section" => "Top", |
| 47 | + "url" => email_alert_frontend_signup_endpoint_no_account, |
| 48 | + } |
| 49 | + actual_tracking = JSON.parse(button["data-ga4-link"]) |
| 50 | + |
| 51 | + assert_equal expected_tracking, actual_tracking |
| 52 | + end |
| 53 | + |
| 54 | + test "renders the single page notification button with a form action of EmailAlertAPI's account-only endpoint for users logged into their gov.uk account" do |
| 55 | + # Need to use Rack as Selenium, the default driver, doesn't provide header access, and we need to set a govuk_account_session header |
| 56 | + Capybara.current_driver = :rack_test |
| 57 | + mock_logged_in_session |
| 58 | + setup_and_visit_content_item("document_collection") |
| 59 | + |
| 60 | + form = page.find("form.gem-c-single-page-notification-button") |
| 61 | + assert_match(email_alert_frontend_signup_endpoint_enforce_account, form["action"]) |
| 62 | + |
| 63 | + button = page.find(:button, class: "gem-c-single-page-notification-button__submit") |
| 64 | + |
| 65 | + expected_tracking = { |
| 66 | + "event_name" => "navigation", |
| 67 | + "type" => "subscribe", |
| 68 | + "index_link" => 1, |
| 69 | + "index_total" => 2, |
| 70 | + "section" => "Top", |
| 71 | + "url" => "/email/subscriptions/single-page/new", |
| 72 | + } |
| 73 | + |
| 74 | + actual_tracking = JSON.parse(button["data-ga4-link"]) |
| 75 | + |
| 76 | + assert_equal expected_tracking, actual_tracking |
| 77 | + |
| 78 | + # reset back to default driver |
| 79 | + Capybara.use_default_driver |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments