From 7844721621c55c539a0e3d7e28ed9b1811d0cde9 Mon Sep 17 00:00:00 2001 From: Josh Clayton Date: Tue, 25 Jul 2023 14:15:33 -0400 Subject: [PATCH] Extract the build_page test helper What? ===== Now that patterns have been established within the test suite, we can safely extract the build_page helper method to a module that's mixed into RSpec. This also adjusts behavior to run via a headless browser by using RSpec's `:js` metadata. --- README.md | 21 +++++++++++---------- spec/features/arguments_spec.rb | 7 ------- spec/features/capybara_javascript_spec.rb | 9 +-------- spec/features/composition_spec.rb | 13 +++++-------- spec/features/form_spec.rb | 7 ------- spec/features/has_many_ordered_spec.rb | 9 +-------- spec/features/has_many_spec.rb | 7 ------- spec/features/has_one_spec.rb | 7 ------- spec/features/macros_with_methods_spec.rb | 7 ------- spec/features/selectors_spec.rb | 7 ------- spec/support/app_generator.rb | 19 +++++++++++++++++++ 11 files changed, 37 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index b993273..295c99a 100644 --- a/README.md +++ b/README.md @@ -373,20 +373,21 @@ git commits and the created tag, and push the `.gem` file to ## Feature Tests This uses a test harness for Rack app generation called `AppGenerator`, which -handles mounting HTML responses to endpoints accessible via `GET`. +handles mounting HTML responses to endpoints accessible via `GET`. In tests, +call `build_page` with the markup you'd like and it will mount that response to +the root of the application. ```ruby -def page - @app ||= AppGenerator - .new - .route("/", "Hello, strange!") - .route("/hello", "

Hello, world!

") - .run -end +page = build_page(<<-HTML) +
+ + +
+HTML ``` -To run tests, either define `page` (via `def page` or RSpec's `let`) to allow -for standard Capybara interaction within tests. +To drive interactions with a headless browser, add the RSpec metadata `:js` to +either individual `it`s or `describe`s. ## Roadmap diff --git a/spec/features/arguments_spec.rb b/spec/features/arguments_spec.rb index 93f2659..f22678a 100644 --- a/spec/features/arguments_spec.rb +++ b/spec/features/arguments_spec.rb @@ -31,11 +31,4 @@ expect(test_page).not_to have_heading_with_required_args_and_kwarg("Hello", 2, visible: true) expect(test_page).to have_heading_with_hash_arg(text: "Hello") end - - def build_page(markup) - AppGenerator - .new - .route("/", markup) - .run - end end diff --git a/spec/features/capybara_javascript_spec.rb b/spec/features/capybara_javascript_spec.rb index 100300a..be10f62 100644 --- a/spec/features/capybara_javascript_spec.rb +++ b/spec/features/capybara_javascript_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -RSpec.describe "Capybara and JavaScript", type: :feature do +RSpec.describe "Capybara and JavaScript", :js, type: :feature do it "behaves as expected when using has_css? and has_no_css?" do page = build_page(<<-HTML)
@@ -150,11 +150,4 @@ expect(test_page.list.items).to have_count_of(4) end end - - def build_page(markup) - AppGenerator - .new - .route("/", markup) - .run(runner: :selenium_chrome_headless) - end end diff --git a/spec/features/composition_spec.rb b/spec/features/composition_spec.rb index c73d065..570fda7 100644 --- a/spec/features/composition_spec.rb +++ b/spec/features/composition_spec.rb @@ -71,13 +71,10 @@ def awesome? end def build_page(markup) - AppGenerator - .new - .route("/", markup) - .run.tap do |session| - # this simulates more standard Rails testing behavior, where a `page` - # isn't assigned to explicitly. - allow(Capybara).to receive(:current_session).and_return(session) - end + super(markup).tap do |session| + # this simulates more standard Rails testing behavior, where a `page` + # isn't assigned to explicitly. + allow(Capybara).to receive(:current_session).and_return(session) + end end end diff --git a/spec/features/form_spec.rb b/spec/features/form_spec.rb index d5f007c..97ab11f 100644 --- a/spec/features/form_spec.rb +++ b/spec/features/form_spec.rb @@ -26,11 +26,4 @@ def select_thing(value) test_page.form.select_thing("One") test_page.form.select_thing("Two") end - - def build_page(markup) - AppGenerator - .new - .route("/", markup) - .run - end end diff --git a/spec/features/has_many_ordered_spec.rb b/spec/features/has_many_ordered_spec.rb index a65b81a..c511576 100644 --- a/spec/features/has_many_ordered_spec.rb +++ b/spec/features/has_many_ordered_spec.rb @@ -51,7 +51,7 @@ # expect(test_page.webhook_at(0).webhook_header).to have_source end - it "allows for selection at an index" do + it "allows for selection at an index", :js do page = build_page(<<-HTML)