File: block_paginator_spec.rb

package info (click to toggle)
ruby-tty-prompt 0.23.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,452 kB
  • sloc: ruby: 8,847; makefile: 4
file content (86 lines) | stat: -rw-r--r-- 3,133 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

RSpec.describe TTY::Prompt::BlockPaginator, "#paginate" do
  it "ignores per_page when equal items " do
    list = %w[a b c d]
    paginator = described_class.new(per_page: 4)

    expect(paginator.paginate(list, 1).to_a).to eq([
      ["a", 0], ["b", 1], ["c", 2], ["d", 3]
    ])
  end

  it "ignores per_page when less items " do
    list = %w[a b c d]
    paginator = described_class.new(per_page: 5)

    expect(paginator.paginate(list, 1).to_a).to eq([
      ["a", 0], ["b", 1], ["c", 2], ["d", 3]
    ])
  end

  it "paginates items matching per_page count" do
    list = %w[a b c d e f]
    paginator = described_class.new(per_page: 3)

    expect(paginator.paginate(list, 1).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
    expect(paginator.paginate(list, 2).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
    expect(paginator.paginate(list, 3).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
    expect(paginator.paginate(list, 4).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
    expect(paginator.paginate(list, 5).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
    expect(paginator.paginate(list, 6).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
    expect(paginator.paginate(list, 7).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
  end

  it "paginates items not matching per_page count" do
    list = %w[a b c d e f g]
    paginator = described_class.new(per_page: 3)

    expect(paginator.paginate(list, 1).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
    expect(paginator.paginate(list, 2).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
    expect(paginator.paginate(list, 3).to_a).to eq([["a", 0], ["b", 1], ["c", 2]])
    expect(paginator.paginate(list, 4).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
    expect(paginator.paginate(list, 5).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
    expect(paginator.paginate(list, 6).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
    expect(paginator.paginate(list, 7).to_a).to eq([["g", 6]])
    expect(paginator.paginate(list, 8).to_a).to eq([["g", 6]])
  end

  it "finds both start and end index for current selection" do
    list = %w[a b c d e f g]
    paginator = described_class.new(per_page: 3, default: 0)

    paginator.paginate(list, 3)
    expect(paginator.start_index).to eq(0)
    expect(paginator.end_index).to eq(2)

    paginator.paginate(list, 4)
    expect(paginator.start_index).to eq(3)
    expect(paginator.end_index).to eq(5)

    paginator.paginate(list, 5)
    expect(paginator.start_index).to eq(3)
    expect(paginator.end_index).to eq(5)

    paginator.paginate(list, 7)
    expect(paginator.start_index).to eq(6)
    expect(paginator.end_index).to eq(6)
  end

  it "starts with default selection" do
    list = %w[a b c d e f g]
    paginator = described_class.new(per_page: 3, default: 3)

    expect(paginator.paginate(list, 4).to_a).to eq([["d", 3], ["e", 4], ["f", 5]])
  end

  it "doesn't accept invalid pagination" do
    list = %w[a b c d e f g]

    paginator = described_class.new(per_page: 0)

    expect {
      paginator.paginate(list, 4)
    }.to raise_error(TTY::Prompt::InvalidArgument, /per_page must be > 0/)
  end
end