-
-
Notifications
You must be signed in to change notification settings - Fork 311
/
resources_controller_spec.rb
162 lines (130 loc) · 5.87 KB
/
resources_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# frozen_string_literal: true
require "rails_helper"
describe Admin::EventsController do
it "should include ResourcesHelper" do
expect(controller.respond_to?(:resource_window_size)).to be_truthy
end
describe "#index" do
let(:params) { Hash.new }
let!(:peter) { create(:event, name: "Peter") }
let!(:lustig) { create(:event, name: "Lustig") }
before do
authorize_user(:as_admin)
end
it "returns all records" do
get :index, params: params
expect(assigns(:events)).to include(peter)
expect(assigns(:events)).to include(lustig)
end
context "with search query given" do
let(:params) { {q: {name_or_hidden_name_or_description_or_location_name_cont: "PeTer"}} }
it "returns only matching records" do
get :index, params: params
expect(assigns(:events)).to include(peter)
expect(assigns(:events)).not_to include(lustig)
end
context "but searching for record with certain association" do
let(:bauwagen) { create(:location, name: "Bauwagen") }
let(:params) { {q: {name_or_hidden_name_or_description_or_location_name_cont: "Bauwagen"}} }
before do
peter.location = bauwagen
peter.save
end
it "returns only matching records" do
get :index, params: params
expect(assigns(:events)).to include(peter)
expect(assigns(:events)).not_to include(lustig)
end
end
context "with sort parameter given" do
let(:params) { {q: {s: "name desc"}} }
it "returns records in the defined order" do
get :index, params: params
expect(assigns(:events)).to eq([peter, lustig])
end
end
context "without sort parameter given" do
context "if resource has name attribute" do
it "returns records sorted by name" do
get :index
expect(assigns(:events)).to eq([lustig, peter])
end
end
context "if resource has no name attribute" do
let!(:booking1) { Booking.create!(from: 2.week.from_now) }
let!(:booking2) { Booking.create!(from: 1.weeks.from_now) }
controller(::Alchemy::Admin::ResourcesController) do
def resource_handler
@_resource_handler ||= Alchemy::Resource.new(controller_path, alchemy_module, Booking)
end
end
it "returns records sorted by first attribute" do
get :index
expect(assigns(:resources)).to eq([booking2, booking1])
end
end
end
end
end
describe "#update" do
let(:params) { {q: {name_or_hidden_name_or_description_or_location_name_cont: "some_query"}, page: 6} }
context "with regular noun model name" do
let(:peter) { create(:event, name: "Peter") }
it "redirects to index, keeping the current location parameters" do
post :update, params: {id: peter.id, event: {name: "Hans"}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
end
end
context "with zero plural noun model name" do
let!(:peter) { create(:series, name: "Peter") }
let(:params) { {q: { name_cont: "some_query"}, page: 6} }
it "redirects to index, keeping the current location parameters" do
expect(controller).to receive(:controller_path) { "admin/series" }
post :update, params: {id: peter.id, series: {name: "Hans"}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/series?page=6&q%5Bname_cont%5D=some_query")
end
end
end
describe "#create" do
let(:params) { {q: {name_or_hidden_name_or_description_or_location_name_cont: "some_query"}, page: 6} }
let!(:location) { create(:location) }
context "with regular noun model name" do
it "redirects to index, keeping the current location parameters" do
post :create, params: {event: {name: "Hans", location_id: location.id}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
end
end
context "with zero plural noun model name" do
let(:params) { {q: {name_cont: "some_query"}, page: 6} }
it "redirects to index, keeping the current location parameters" do
expect(controller).to receive(:controller_path) { "admin/series" }
post :create, params: {series: {name: "Hans"}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/series?page=6&q%5Bname_cont%5D=some_query")
end
end
end
describe "#destroy" do
let(:params) { {q: {name_or_hidden_name_or_description_or_location_name_cont: "some_query"}, page: 6} }
let!(:peter) { create(:event, name: "Peter") }
it "redirects to index, keeping the current location parameters" do
delete :destroy, params: {id: peter.id}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
end
context "If the resource is not destroyable" do
let!(:undestroyable) { create(:event, name: "Undestructible") }
it "adds an error flash" do
delete :destroy, params: {id: undestroyable.id}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
expect(flash[:error]).to eq("This is the undestructible event!")
end
end
end
describe "#common_search_filter_includes" do
before do
allow(controller).to receive(:alchemy_module) { { name: "events" } }
end
it "should not be frozen" do
expect(controller.send(:common_search_filter_includes)).to_not be_frozen
end
end
end