Skip to content

Improve tests result consistency #581

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

Merged
merged 16 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
40 changes: 2 additions & 38 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,45 +66,9 @@
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Capybara config
#
# selenium_firefox webdriver only works for Travis-CI builds.
default_driver = :selenium_chrome_headless

supported_drivers = %i[selenium_chrome_headless selenium_chrome selenium_firefox selenium]
driver = ENV["DRIVER"].try(:to_sym) || default_driver
Capybara.default_driver = driver

raise "Unsupported driver: #{driver} (supported = #{supported_drivers})" unless supported_drivers.include?(driver)

case driver
when :selenium_chrome
DriverRegistration.register_selenium_chrome

when :selenium_chrome_headless
DriverRegistration.register_selenium_chrome_headless

when :selenium_firefox, :selenium
DriverRegistration.register_selenium_firefox
driver = :selenium_firefox
end

Capybara.javascript_driver = driver
Capybara.default_driver = driver

Capybara.register_server(Capybara.javascript_driver) do |app, port|
require "rack/handler/puma"
Rack::Handler::Puma.run(app, Port: port)
end
Capybara.server = :puma

config.before(:each, type: :system, js: true) do
driven_by driver
driven_by :selenium, using: :chrome,
options: { args: %w[headless disable-gpu no-sandbox disable-dev-shm-usage] }
end
Capybara.default_driver = :selenium_chrome_headless
Capybara.javascript_driver = :selenium_chrome_headless

# Capybara.default_max_wait_time = 15
puts "=" * 80
puts "Capybara using driver: #{Capybara.javascript_driver}"
puts "=" * 80
Expand Down
9 changes: 9 additions & 0 deletions spec/rescript/rescript_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,35 @@
fill_in author_field, with: comment.author
fill_in text_field, with: comment.text
click_button("Post")

page.driver.browser.manage.timeouts.implicit_wait = 1

expect(Comment.all.count).to equal(new_comment_count)
end

it "comment count remains the same when author field is empty" do
initial_comment_count = Comment.all.count
fill_in text_field, with: comment.text
click_button("Post")

expect(page).to have_text(/Can't save the comment!/)
expect(Comment.all.count).to equal(initial_comment_count)
end

it "comment count remains the same when text field is empty" do
initial_comment_count = Comment.all.count
fill_in author_field, with: comment.author
click_button("Post")

expect(page).to have_text(/Can't save the comment!/)
expect(Comment.all.count).to equal(initial_comment_count)
end

it "comment count remains the same when both form fields are empty" do
initial_comment_count = Comment.all.count
click_button("Post")

expect(page).to have_text(/Can't save the comment!/)
expect(Comment.all.count).to equal(initial_comment_count)
end
end
Expand Down
11 changes: 10 additions & 1 deletion spec/stimulus/turbo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,36 @@
fill_in author_field, with: comment.author
fill_in text_field, with: comment.text
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure the text is some unique value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this test runs, we have a fresh database.

click_button("Post")
sleep(1)

# This will ensure we wait enough for the changes to be applied
expect(page).to have_text("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahangarha, this is a very strange assertion!

Please add self-review comments for WTF's like this!

CC: @Judahmeek

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in the comment above that line, we need to put some delay before getting assertion for comment count change. Before I fixed it with sleep but that is not efficient. This assertion makes it more efficient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect page to display the new comment in the right format block (not in the input field).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already had a test to ensure the new comment is displayed on the page. This test was to ensure the comment was added to the database.

Now, we combine them into one test with multiple assertions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


expect(Comment.all.count).to equal(new_comment_count)
end

it "comment count remains the same when author field is empty" do
initial_comment_count = Comment.all.count
fill_in text_field, with: comment.text
click_button("Post")

expect(page).to have_text("Author: can't be blank")
expect(Comment.all.count).to equal(initial_comment_count)
end

it "comment count remains the same when text field is empty" do
initial_comment_count = Comment.all.count
fill_in author_field, with: comment.author
click_button("Post")

expect(page).to have_text("Text: can't be blank")
expect(Comment.all.count).to equal(initial_comment_count)
end

it "comment count remains the same when both form fields are empty" do
initial_comment_count = Comment.all.count
click_button("Post")

expect(page).to have_text("Author: can't be blank")
expect(Comment.all.count).to equal(initial_comment_count)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/system/add_new_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "system/shared/contexts"

describe "Add new comment" do
context "when React Router", page: :main, js: true, type: :system do
context "when React Router", page: :main do
describe "with Horizontal Form" do
before do
visit root_path
Expand Down Expand Up @@ -61,7 +61,7 @@
end
end

context "when React/Redux", page: :react_demo, js: true, type: :system do
context "when React/Redux", page: :react_demo do
describe "with Horizontal Form" do
before do
visit root_path
Expand Down Expand Up @@ -118,7 +118,7 @@
end
end

context "when simple page", page: :simple, js: true, type: :system do
context "when simple page", page: :simple do
describe "with Horizontal Form" do
before do
visit root_path
Expand Down
2 changes: 1 addition & 1 deletion spec/system/react_router_demo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "rails_helper"
require "system/shared/contexts"

describe "React Router Routes", js: true, type: :system do
describe "React Router Routes" do
context "when Root URL", page: :main do
it "shows comments section" do
visit root_path
Expand Down