|
@@ -0,0 +1,54 @@
|
|
1
|
+require "test_helper"
|
|
2
|
+require_relative "../../app/models/paginated"
|
|
3
|
+
|
|
4
|
+class FakePaginated
|
|
5
|
+ include Paginated
|
|
6
|
+ DATA = ("a".."z").to_a
|
|
7
|
+
|
|
8
|
+ # fake inspection
|
|
9
|
+ attr_reader :data
|
|
10
|
+
|
|
11
|
+ # fake a model
|
|
12
|
+ def initialize
|
|
13
|
+ @data = DATA
|
|
14
|
+ end
|
|
15
|
+
|
|
16
|
+ # fake a model
|
|
17
|
+ def offset(start)
|
|
18
|
+ @data = @data[start..]
|
|
19
|
+ self
|
|
20
|
+ end
|
|
21
|
+
|
|
22
|
+ # fake a model
|
|
23
|
+ def limit(count)
|
|
24
|
+ @data = @data[...count]
|
|
25
|
+ self
|
|
26
|
+ end
|
|
27
|
+
|
|
28
|
+ def count_by_page
|
|
29
|
+ 3
|
|
30
|
+ end
|
|
31
|
+end
|
|
32
|
+
|
|
33
|
+class PaginatedTest < ActiveSupport::TestCase
|
|
34
|
+ def assert_valid_pagination(paginated, count:)
|
|
35
|
+ assert_equal count, paginated.data.size
|
|
36
|
+ end
|
|
37
|
+
|
|
38
|
+ test "paginated with placeholder" do
|
|
39
|
+ test_empty_params = FakePaginated.new.paginate({})
|
|
40
|
+ assert_valid_pagination test_empty_params, count: FakePaginated.new.count_by_page
|
|
41
|
+ assert_equal %w[a b c], test_empty_params.data
|
|
42
|
+
|
|
43
|
+ test_valid_params = FakePaginated.new.paginate({ page: 1 })
|
|
44
|
+ assert_valid_pagination test_valid_params, count: FakePaginated.new.count_by_page
|
|
45
|
+ assert_equal %w[d e f], test_valid_params.data
|
|
46
|
+ test_valid_params = FakePaginated.new.paginate({ page: 4 })
|
|
47
|
+ assert_valid_pagination test_valid_params, count: FakePaginated.new.count_by_page
|
|
48
|
+ assert_equal %w[m n o], test_valid_params.data
|
|
49
|
+
|
|
50
|
+ test_invalid_params = FakePaginated.new.paginate({ page: "what" })
|
|
51
|
+ assert_valid_pagination test_invalid_params, count: FakePaginated.new.count_by_page
|
|
52
|
+ assert_equal %w[a b c], test_invalid_params.data
|
|
53
|
+ end
|
|
54
|
+end
|