require "application_system_test_case"

class OnboardingTest < ApplicationSystemTestCase
  test "full onboarding process with personal account setup" do
    visit new_user_registration_path

    fill_in "user[email]", with: "firstname.lastname@example.com"
    fill_in "user[password]", with: "th1s1sp@ssw0rd"
    fill_in "user[password_confirmation]", with: "th1s1sp@ssw0rd"
    find(".irelia-form button[type='submit']").click

    # It should show a message that the user has signed up but is unconfirmed.
    assert_text I18n.t("devise.registrations.signed_up")

    # This is a hack to confirm the email for the user
    user = User.find_by(email: "firstname.lastname@example.com")
    user.update(confirmed_at: Time.now)

    # Login in with the newly created user
    visit new_user_session_path

    # fill_in "user[email]", with: "firstname.lastname@example.com"
    # fill_in "user[password]", with: "th1s1sp@ssw0rd"
    # find(".irelia-form button[type='submit']").click

    # Should be on the onboarding_path after first sign in
    assert_current_path onboarding_path

    # Fill the personal account setup form
    fill_in "user[personal_account_attributes][name]", with: "Sven Bertilsson"

    click_on I18n.t("users.auth.onboarding.show.submit")

    # Should be redirected to dashboard after successfully signing up.
    assert_current_path dashboard_path
    assert_equal "Sven Bertilsson", user.reload.personal_account.name
  end
end
