Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract the build_page test helper #39

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<h1>Hello, world!</h1>")
.run
end
page = build_page(<<-HTML)
<form>
<input name="name" type="text" />
<input name="email" type="text" />
</form>
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

Expand Down
7 changes: 0 additions & 7 deletions spec/features/arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 1 addition & 8 deletions spec/features/capybara_javascript_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
<section>
Expand Down Expand Up @@ -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
13 changes: 5 additions & 8 deletions spec/features/composition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 0 additions & 7 deletions spec/features/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 1 addition & 8 deletions spec/features/has_many_ordered_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<template id="item">
<li>
Expand Down Expand Up @@ -175,11 +175,4 @@
expect(test_page.list_at(2, name: "List of 2 Items").items).to have_count_of(2)
expect(test_page).not_to have_list_at(2, name: "List of 3 Items")
end

def build_page(markup)
AppGenerator
.new
.route("/", markup)
.run(runner: :selenium_chrome_headless)
end
end
7 changes: 0 additions & 7 deletions spec/features/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,4 @@ def name_text
expect(test_page.sections("Section").map(&:text)).to eq(["Section 1", "Section 2"])
expect(test_page.sections("Bogus")).to be_empty
end

def build_page(markup)
AppGenerator
.new
.route("/", markup)
.run
end
end
7 changes: 0 additions & 7 deletions spec/features/has_one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,4 @@ def section_heading_tag_name
expect(test_page.form.name_field.value).to eq "Awesome Person"
expect(test_page.form.email_field.value).to eq "[email protected]"
end

def build_page(markup)
AppGenerator
.new
.route("/", markup)
.run
end
end
7 changes: 0 additions & 7 deletions spec/features/macros_with_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,4 @@ def items(state:)
expect(test_page.items(state: "incomplete").first.name).to have_text("Buy eggs")
expect(test_page.items(state: "incomplete")[1].name).to have_text("Buy onions")
end

def build_page(markup)
AppGenerator
.new
.route("/", markup)
.run
end
end
7 changes: 0 additions & 7 deletions spec/features/selectors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,4 @@

expect(test_page).to have_heading
end

def build_page(markup)
AppGenerator
.new
.route("/", markup)
.run
end
end
19 changes: 19 additions & 0 deletions spec/support/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,22 @@ def run(runner: :rack_test)

attr_reader :app, :title
end

module BuildPage
def build_page(markup)
AppGenerator
.new
.route("/", markup)
.run(runner: @app_runner)
end
end

RSpec.configure do |config|
include BuildPage

config.around do |example|
@app_runner = (!!example.metadata[:js]) ? :selenium_chrome_headless : :rack_test

example.run
end
end