Skip to content

Commit cf8b936

Browse files
committed
Extract shared spec tests
Signed-off-by: David Celis <[email protected]>
1 parent 58da3b5 commit cf8b936

File tree

9 files changed

+85
-117
lines changed

9 files changed

+85
-117
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in api_pagination.gemspec
44
gemspec
5+
gem 'pry'

lib/rails/pagination.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'pry'
12
module Rails
23
module Pagination
34
protected

spec/grape_spec.rb

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,43 @@
11
require 'spec_helper'
2+
require 'support/shared_examples/existing_headers'
3+
require 'support/shared_examples/first_page'
4+
require 'support/shared_examples/middle_page'
5+
require 'support/shared_examples/last_page'
26

37
describe NumbersAPI do
48
describe 'GET #index' do
9+
let(:links) { last_response.headers['Link'].split(', ') }
10+
511
context 'without enough items to give more than one page' do
612
it 'should not paginate' do
7-
get :numbers, count: 20
13+
get :numbers, :count => 20
814
expect(last_response.headers.keys).not_to include('Link')
915
end
1016
end
1117

1218
context 'with existing Link headers' do
13-
before { get :numbers, count: 30, with_headers: true }
14-
let(:links) { last_response.headers['Link'].split(', ') }
15-
16-
it 'should keep existing Links' do
17-
expect(links).to include('<http://example.org/numbers?count=30>; rel="without"')
18-
end
19+
before { get :numbers, :count => 30, :with_headers => true }
1920

20-
it 'should contain pagination Links' do
21-
expect(links).to include('<http://example.org/numbers?count=30&page=2&with_headers=true>; rel="next"')
22-
expect(links).to include('<http://example.org/numbers?count=30&page=2&with_headers=true>; rel="last"')
23-
end
21+
it_behaves_like 'an endpoint with existing Link headers'
2422
end
2523

2624
context 'with enough items to paginate' do
2725
context 'when on the first page' do
2826
before { get :numbers, :count => 100 }
29-
let(:links) { last_response.headers['Link'].split(', ') }
30-
31-
it 'should not give a link with rel "first"' do
32-
expect(links).not_to include('rel="first"')
33-
end
34-
35-
it 'should not give a link with rel "prev"' do
36-
expect(links).not_to include('rel="prev"')
37-
end
3827

39-
it 'should give a link with rel "last"' do
40-
expect(links).to include('<http://example.org/numbers?count=100&page=4>; rel="last"')
41-
end
42-
43-
it 'should give a link with rel "next"' do
44-
expect(links).to include('<http://example.org/numbers?count=100&page=2>; rel="next"')
45-
end
28+
it_behaves_like 'an endpoint with a first page'
4629
end
4730

4831
context 'when on the last page' do
4932
before { get :numbers, :count => 100, :page => 4 }
50-
let(:links) { last_response.headers['Link'].split(', ') }
51-
52-
it 'should not give a link with rel "last"' do
53-
expect(links).not_to include('rel="last"')
54-
end
55-
56-
it 'should not give a link with rel "next"' do
57-
expect(links).not_to include('rel="next"')
58-
end
5933

60-
it 'should give a link with rel "first"' do
61-
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="first"')
62-
end
63-
64-
it 'should give a link with rel "prev"' do
65-
expect(links).to include('<http://example.org/numbers?count=100&page=3>; rel="prev"')
66-
end
34+
it_behaves_like 'an endpoint with a last page'
6735
end
6836

6937
context 'when somewhere comfortably in the middle' do
70-
it 'should give all pagination links' do
71-
get :numbers, count: 100, page: 2
72-
73-
links = last_response.headers['Link'].split(', ')
38+
before { get :numbers, :count => 100, :page => 2 }
7439

75-
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="first"')
76-
expect(links).to include('<http://example.org/numbers?count=100&page=4>; rel="last"')
77-
expect(links).to include('<http://example.org/numbers?count=100&page=3>; rel="next"')
78-
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="prev"')
79-
end
40+
it_behaves_like 'an endpoint with a middle page'
8041
end
8142
end
8243
end

spec/rails_spec.rb

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,44 @@
11
require 'spec_helper'
2+
require 'support/shared_examples/existing_headers'
3+
require 'support/shared_examples/first_page'
4+
require 'support/shared_examples/middle_page'
5+
require 'support/shared_examples/last_page'
26

37
describe NumbersController, :type => :controller do
8+
before { request.host = 'example.org' }
49
describe 'GET #index' do
10+
let(:links) { response.headers['Link'].split(', ') }
11+
512
context 'without enough items to give more than one page' do
613
it 'should not paginate' do
7-
get :index, count: 20
14+
get :index, :count => 20
815
expect(response.headers.keys).not_to include('Link')
916
end
1017
end
1118

1219
context 'with existing Link headers' do
13-
before do
14-
get :index, count: 30, with_headers: true
15-
16-
@links = response.headers['Link'].split(', ')
17-
end
18-
19-
it 'should keep existing Links' do
20-
expect(@links).to include('<http://test.host/numbers?count=30>; rel="without"')
21-
end
20+
before { get :index, :count => 30, :with_headers => true }
2221

23-
it 'should contain pagination Links' do
24-
expect(@links).to include('<http://test.host/numbers?count=30&page=2>; rel="next"')
25-
expect(@links).to include('<http://test.host/numbers?count=30&page=2>; rel="last"')
26-
end
22+
it_behaves_like 'an endpoint with existing Link headers'
2723
end
2824

2925
context 'with enough items to paginate' do
3026
context 'when on the first page' do
31-
before do
32-
get :index, count: 100
33-
34-
@links = response.headers['Link'].split(', ')
35-
end
36-
37-
it 'should not give a link with rel "first"' do
38-
expect(@links).not_to include('rel="first"')
39-
end
27+
before { get :index, :count => 100 }
4028

41-
it 'should not give a link with rel "prev"' do
42-
expect(@links).not_to include('rel="prev"')
43-
end
44-
45-
it 'should give a link with rel "last"' do
46-
expect(@links).to include('<http://test.host/numbers?count=100&page=4>; rel="last"')
47-
end
48-
49-
it 'should give a link with rel "next"' do
50-
expect(@links).to include('<http://test.host/numbers?count=100&page=2>; rel="next"')
51-
end
29+
it_behaves_like 'an endpoint with a first page'
5230
end
5331

5432
context 'when on the last page' do
55-
before do
56-
get :index, count: 100, page: 4
57-
58-
@links = response.headers['Link'].split(', ')
59-
end
60-
61-
it 'should not give a link with rel "last"' do
62-
expect(@links).not_to include('rel="last"')
63-
end
33+
before { get :index, :count => 100, :page => 4 }
6434

65-
it 'should not give a link with rel "next"' do
66-
expect(@links).not_to include('rel="next"')
67-
end
68-
69-
it 'should give a link with rel "first"' do
70-
expect(@links).to include('<http://test.host/numbers?count=100&page=1>; rel="first"')
71-
end
72-
73-
it 'should give a link with rel "prev"' do
74-
expect(@links).to include('<http://test.host/numbers?count=100&page=3>; rel="prev"')
75-
end
35+
it_behaves_like 'an endpoint with a last page'
7636
end
7737

7838
context 'when somewhere comfortably in the middle' do
79-
it 'should give all pagination links' do
80-
get :index, count: 100, page: 2
81-
82-
links = response.headers['Link'].split(', ')
39+
before { get :index, :count => 100, :page => 2 }
8340

84-
expect(links).to include('<http://test.host/numbers?count=100&page=1>; rel="first"')
85-
expect(links).to include('<http://test.host/numbers?count=100&page=4>; rel="last"')
86-
expect(links).to include('<http://test.host/numbers?count=100&page=3>; rel="next"')
87-
expect(links).to include('<http://test.host/numbers?count=100&page=1>; rel="prev"')
88-
end
41+
it_behaves_like 'an endpoint with a middle page'
8942
end
9043
end
9144
end

spec/support/numbers_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def index
5252
total = params.fetch(:count).to_i
5353

5454
if params[:with_headers]
55-
query = request.query_parameters
55+
query = request.query_parameters.dup
5656
query.delete(:with_headers)
57-
headers['Link'] = %(<#{numbers_url}?#{query.to_param}>; rel="without") if params[:with_headers]
57+
headers['Link'] = %(<#{numbers_url}?#{query.to_param}>; rel="without")
5858
end
5959

6060
@numbers = PaginatedSet.new(page, 25, total)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
shared_examples 'an endpoint with existing Link headers' do
2+
it 'should keep existing Links' do
3+
expect(links).to include('<http://example.org/numbers?count=30>; rel="without"')
4+
end
5+
6+
it 'should contain pagination Links' do
7+
expect(links).to include('<http://example.org/numbers?count=30&page=2&with_headers=true>; rel="next"')
8+
expect(links).to include('<http://example.org/numbers?count=30&page=2&with_headers=true>; rel="last"')
9+
end
10+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
shared_examples 'an endpoint with a first page' do
2+
it 'should not give a link with rel "first"' do
3+
expect(links).not_to include('rel="first"')
4+
end
5+
6+
it 'should not give a link with rel "prev"' do
7+
expect(links).not_to include('rel="prev"')
8+
end
9+
10+
it 'should give a link with rel "last"' do
11+
expect(links).to include('<http://example.org/numbers?count=100&page=4>; rel="last"')
12+
end
13+
14+
it 'should give a link with rel "next"' do
15+
expect(links).to include('<http://example.org/numbers?count=100&page=2>; rel="next"')
16+
end
17+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
shared_examples 'an endpoint with a last page' do
2+
it 'should not give a link with rel "last"' do
3+
expect(links).not_to include('rel="last"')
4+
end
5+
6+
it 'should not give a link with rel "next"' do
7+
expect(links).not_to include('rel="next"')
8+
end
9+
10+
it 'should give a link with rel "first"' do
11+
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="first"')
12+
end
13+
14+
it 'should give a link with rel "prev"' do
15+
expect(links).to include('<http://example.org/numbers?count=100&page=3>; rel="prev"')
16+
end
17+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
shared_examples 'an endpoint with a middle page' do
2+
it 'should give all pagination links' do
3+
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="first"')
4+
expect(links).to include('<http://example.org/numbers?count=100&page=4>; rel="last"')
5+
expect(links).to include('<http://example.org/numbers?count=100&page=3>; rel="next"')
6+
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="prev"')
7+
end
8+
end

0 commit comments

Comments
 (0)