Skip to content

Commit

Permalink
add tests for sorting users with capybara
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyAndriyovuch committed Sep 24, 2023
1 parent be90c6a commit 67b19b5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
25 changes: 13 additions & 12 deletions app/views/account/users/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ div class="container home"
td = t '.table.edit'
td = t '.table.ban'
td = t '.table.delete'
- @users.each do |user|
tr id="user-info-#{user.id}"
td = user.email
td = user.last_sign_in_at
td = link_to icon('fa-solid', 'eye'), account_user_path(id: user.id)
td = link_to icon('fa-solid', 'edit'), edit_account_user_path(id: user.id)
td = link_to (icon('fa-solid', user.blocked? ? 'lock' : 'lock-open')),
account_user_path(id: user, user: {blocked: user.blocked? ? false : true}),
onclick: "alert('Are you sure?')", method: :patch
td = link_to account_user_path(id: user), method: :delete,
data: { confirm: t('.confirm_delete') } do
i.fa.fa-trash.mx-2
tbody id="users"
- @users.each do |user|
tr id="user-info-#{user.id}"
td = user.email
td = user.last_sign_in_at
td = link_to icon('fa-solid', 'eye'), account_user_path(id: user.id)
td = link_to icon('fa-solid', 'edit'), edit_account_user_path(id: user.id)
td = link_to (icon('fa-solid', user.blocked? ? 'lock' : 'lock-open')),
account_user_path(id: user, user: {blocked: user.blocked? ? false : true}),
onclick: "alert('Are you sure?')", method: :patch
td = link_to account_user_path(id: user), method: :delete,
data: { confirm: t('.confirm_delete') } do
i.fa.fa-trash.mx-2
39 changes: 26 additions & 13 deletions spec/features/account/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

describe "visit admin page", js: true do
let(:time_login) { Time.new(2020, 0o1, 0o1).utc }
let!(:another_user) do
create(:user, email: "[email protected]", password: "12345878",
last_sign_in_at: time_login)
end
let!(:users) { create_list(:user, 5, last_sign_in_at: time_login) }

before do
@admin = create(:user, :admin)
Expand All @@ -16,18 +13,34 @@

it "visits admin page" do
visit account_users_path
expect(page).to have_content "[email protected]"
expect(page).to have_content users.first.email
expect(page).to have_content time_login
end

context "with sorting" do
it "by id asc" do
visit account_users_path(sort: "id asc")

expect(page.find("#users > :first-child > :first-child")).to have_content User.first.email
expect(page.find("#users > :last-child > :first-child")).to have_content User.last.email
end

it "by id desc" do
visit account_users_path(sort: "id desc")

expect(page.find("#users > :first-child > :first-child")).to have_content User.last.email
expect(page.find("#users > :last-child > :first-child")).to have_content User.first.email
end
end

context "when user clicks show icon" do
it "redirects to user info page" do
visit account_users_path
within(:css, "#user-info-#{another_user.id}") do
within(:css, "#user-info-#{users.first.id}") do
find(".fa-eye", visible: :all).click
sleep 3
end
expect(page).to have_current_path(account_user_path(id: another_user.id))
expect(page).to have_current_path(account_user_path(id: users.first.id))
expect(page).to have_content "Email"
expect(page).to have_content "First name"
expect(page).to have_content "Last name"
Expand All @@ -41,11 +54,11 @@
context "when user clicks edit icon" do
it "redirects to user edit info page" do
visit account_users_path
within(:css, "#user-info-#{another_user.id}") do
click_link(href: edit_account_user_path(id: another_user.id))
within(:css, "#user-info-#{users.first.id}") do
click_link(href: edit_account_user_path(id: users.first.id))
sleep 3
end
expect(page).to have_current_path(edit_account_user_path(id: another_user.id))
expect(page).to have_current_path(edit_account_user_path(id: users.first.id))
expect(page).to have_content "First name"
expect(page).to have_content "Last name"
expect(page).to have_content "Country"
Expand All @@ -56,14 +69,14 @@

context "when edit user`s info correctly" do
it "redirects to user info page" do
visit edit_account_user_path(id: another_user.id)
visit edit_account_user_path(id: users.first.id)
find("#user_first_name").set("John")
find("#user_last_name").set("Doe")
select "Albania", from: "user_country"
find("#user_password").set("111111111")
find("#user_password_confirmation").set("111111111")
find_button("commit").click
expect(page).to have_current_path(account_user_path(id: another_user.id))
expect(page).to have_current_path(account_user_path(id: users.first.id))
expect(page).to have_content "John"
expect(page).to have_content "Doe"
expect(page).to have_content "AL"
Expand All @@ -72,7 +85,7 @@

context "when edit user`s info wrongly" do
it "show error messages" do
visit edit_account_user_path(id: another_user.id)
visit edit_account_user_path(id: users.first.id)
find("#user_first_name").set("J")
find("#user_last_name").set("D")
select "Albania", from: "user_country"
Expand Down
4 changes: 2 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@
config.include Devise::Test::IntegrationHelpers, type: :helper
config.include Devise::Test::ControllerHelpers, type: :controller

config.include Capybara::DSL, type: :request

config.add_formatter "Fuubar"
config.fuubar_output_pending_results = false

Webdrivers::Chromedriver.required_version = "114.0.5735.90"
end

Shoulda::Matchers.configure do |config|
Expand Down
1 change: 0 additions & 1 deletion spec/requests/account/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

expect(response).to be_successful
expect(response).to render_template(:index)
expect(response.body).to include(I18n.t("account.users.index.main_header"))
end
end

Expand Down

0 comments on commit 67b19b5

Please sign in to comment.