-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
batch_request_authority_searches_controller_spec.rb
146 lines (123 loc) · 4.16 KB
/
batch_request_authority_searches_controller_spec.rb
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'spec_helper'
shared_examples_for "creating a search" do
it "performs a search" do
results = assigns[:search].results
expect(results.count).to eq 3
expect(results.first[:model].name).to eq authority_1.name
expect(results.second[:model].name).to eq authority_2.name
expect(results.third[:model].name).to eq authority_3.name
end
it "sets @query" do
expect(assigns[:query]).to eq 'Example'
end
it "sets @result_limit" do
expect(assigns[:result_limit]).to eq assigns[:search].matches_estimated
end
it "sets @page" do
expect(assigns[:page]).to eq 1
end
it "sets @per_page" do
expect(assigns[:per_page]).to eq 25
end
end
RSpec.describe AlaveteliPro::BatchRequestAuthoritySearchesController do
let(:pro_user) { FactoryBot.create(:pro_user) }
describe "#index" do
let(:authority_1) { FactoryBot.build(:public_body) }
let(:authority_2) { FactoryBot.build(:public_body) }
let(:authority_3) { FactoryBot.build(:public_body) }
before do
authority_1.save!
authority_2.save!
authority_3.save!
update_xapian_index
sign_in pro_user
end
context 'without a draft_id param' do
it 'initializes a draft if a draft_id was not provided' do
get :index
expect(assigns[:draft_batch_request]).to be_new_record
end
end
context 'with a draft_id param' do
it 'finds a draft by draft_id' do
draft = FactoryBot.create(:draft_info_request_batch, user: pro_user)
get :index, params: { draft_id: draft.id }
expect(assigns[:draft_batch_request]).to eq(draft)
end
it 'initializes a draft if one cannot be found with the given draft_id' do
max_id =
AlaveteliPro::DraftInfoRequestBatch.maximum(:id).try(:next) || 99
get :index, params: { draft_id: max_id }
expect(assigns[:draft_batch_request]).to be_new_record
expect(assigns[:draft_batch_request].user).to eq(pro_user)
end
end
context "when responding to a normal request" do
before do
with_feature_enabled(:alaveteli_pro) do
get :index, params: { authority_query: 'Example' }
end
end
it_behaves_like "creating a search"
it "handles an empty query string" do
with_feature_enabled(:alaveteli_pro) do
get :index
# No need for _search_result because no results
expect(response).not_to render_template partial: '_search_result'
end
end
it "renders index" do
expect(response).to render_template('index')
end
it "raises WillPaginate::InvalidPage error for pages beyond the limit" do
expect {
get :index, params: {
authority_query: 'Example Public Body',
page: 21
}
}.to raise_error(ActiveRecord::RecordNotFound)
end
end
context "when responding to an ajax request" do
before do
with_feature_enabled :alaveteli_pro do
get :index, xhr: true, params: { authority_query: 'Example' }
end
end
it_behaves_like "creating a search"
it "handles an empty query string" do
with_feature_enabled(:alaveteli_pro) do
get :index, xhr: true, params: { authority_query: '' }
# No need for _search_result because no results
expect(response).not_to render_template partial: '_search_result'
end
end
it "only renders search_results partial" do
expect(response).to render_template '_search_results'
expect(response).not_to render_template('new')
end
it "raises WillPaginate::InvalidPage error for pages beyond the limit" do
expect {
get :index,
xhr: true,
params: {
authority_query: 'Example Public Body',
page: 21
}
}.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
describe '#new' do
before do
sign_in pro_user
end
it 'redirects to index action' do
get :new
expect(response).to redirect_to(
'/pro/batch_request_authority_searches'
)
end
end
end