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

Add pagination options and RSpec tests for UserController #10

Merged
merged 10 commits into from
Oct 29, 2024
8 changes: 6 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def role

# For filtering the users list with proper search and pagination.
def paginate_list
paginate_options = { '1' => 25, '2' => 50, '3' => 100 }
paginate_options = { '1' => 25, '2' => 50, '3' => 100, '4' => User.count}

# If the above hash does not have a value for the key,
# it means that we need to show all the users on the page
Expand All @@ -259,7 +259,11 @@ def paginate_list

# The type of condition for the search depends on what the user has selected from the search_by dropdown
@search_by = params[:search_by]
@per_page = 3

# Sets the number of users to display per page based on the 'per_page' parameter from the request.
# If no 'per_page' parameter is provided, it defaults to '4', which corresponds to displaying all users on one page.
@per_page = params[:per_page] || '4'

# search for corresponding users
# users = User.search_users(role, user_id, letter, @search_by)

Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,31 @@
expect(response).to redirect_to('/tree_display/drill')
end
end

context '#paginate_list' do
before do
# Seed the database with a number of users for pagination tests
FactoryBot.create_list(:test_user, 110)
end

it 'displays 25 users per page when per_page is 25' do
get :list, params: { per_page: '1' }
expect(assigns(:paginated_users).length).to eq(25)
end

it 'displays 50 users per page when per_page is 50' do
get :list, params: { per_page: '2' }
expect(assigns(:paginated_users).length).to eq(50)
end

it 'displays 100 users per page when per_page is 100' do
get :list, params: { per_page: '3' }
expect(assigns(:paginated_users).length).to eq(100)
end

it 'displays all users on a single page when per_page is "all"' do
get :list, params: { per_page: '4' }
expect(assigns(:paginated_users).length).to eq(110)
end
end
end
4 changes: 2 additions & 2 deletions spec/factories/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@
end

factory :test_user, class: User do
username 'username'
sequence(:username) { |n| "username#{n}" }
name 'full name'
email 'abc@mailinator.com'
sequence(:email) { |n| "user#{n}@mailinator.com" }
end

factory :survey_deployment, class: SurveyDeployment do
Expand Down
Loading