Created
February 24, 2023 19:18
-
-
Save bradpauly/08e9d85b5b77659afadfcbfaff6dc5de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "minitest/autorun" | |
require "minitest/spec" | |
require "ostruct" | |
class Pager | |
def initialize(rows:, rows_per_page:) | |
@rows = rows | |
@rows_per_page = rows_per_page | |
end | |
def total_pages | |
(@rows.count/@rows_per_page.to_f).ceil | |
end | |
end | |
describe Pager do | |
describe "#total_pages" do | |
it "rounds up to the next whole number" do | |
rows = OpenStruct.new(count: 33) | |
pager = Pager.new(rows: rows, rows_per_page: 10) | |
assert_equal pager.total_pages, 4 | |
end | |
it "returns 1 when there are less than rows_per_page" do | |
rows = OpenStruct.new(count: 5) | |
pager = Pager.new(rows: rows, rows_per_page: 10) | |
assert_equal pager.total_pages, 1 | |
end | |
it "returns 0 when there are 0 rows" do | |
rows = OpenStruct.new(count: 0) | |
pager = Pager.new(rows: rows, rows_per_page: 10) | |
assert_equal pager.total_pages, 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment