-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathresource_controller_spec.rb
201 lines (161 loc) · 4.63 KB
/
resource_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# frozen_string_literal: true
require 'spec_helper'
module Spree
module Admin
class WidgetsController < Spree::Admin::ResourceController
prepend_view_path('spec/test_views')
def model_class
Widget
end
end
end
end
describe Spree::Admin::WidgetsController, type: :controller do
stub_authorization!
after(:all) do
# Spree::Core::Engine.routes.reload_routes!
Rails.application.reload_routes!
end
with_model 'Widget', scope: :all do
table do |t|
t.string :name
t.integer :position
t.timestamps null: false
end
model do
acts_as_list
validates :name, presence: true
before_destroy :check_destroy_constraints
def check_destroy_constraints
return unless name == 'undestroyable'
errors.add :base, "You can't destroy undestroyable things!"
errors.add :base, "Terrible things might happen."
throw(:abort)
end
end
end
before do
Spree::Core::Engine.routes.draw do
namespace :admin do
resources :widgets do
post :update_positions, on: :member
end
end
end
end
describe '#new' do
subject do
get :new
end
it 'succeeds' do
subject
expect(response).to be_successful
end
end
describe '#edit' do
let(:widget) { Widget.create!(name: 'a widget') }
subject do
get :edit, params: { id: widget.to_param }
end
it 'succeeds' do
subject
expect(response).to be_successful
end
end
describe '#create' do
let(:params) do
{ widget: { name: 'a widget' } }
end
subject { post :create, params: params }
it 'creates the resource' do
expect { subject }.to change { Widget.count }.by(1)
end
context 'failure' do
let(:params) do
{ widget: { name: '' } } # blank name generates an error
end
it 'sets a flash error' do
subject
expect(flash[:error]).to eq assigns(:widget).errors.full_messages.join(', ')
end
end
context 'without any parameters' do
let(:params) { {} }
before do
allow_any_instance_of(Widget).to receive(:name).and_return('some name')
end
it 'creates the resource' do
expect { subject }.to change { Widget.count }.by(1)
end
end
end
describe '#update' do
let(:widget) { Widget.create!(name: 'a widget') }
let(:params) do
{
id: widget.to_param,
widget: { name: 'widget renamed' }
}
end
subject { put :update, params: params }
it 'updates the resource' do
expect { subject }.to change { widget.reload.name }.from('a widget').to('widget renamed')
end
context 'failure' do
let(:params) do
{
id: widget.to_param,
widget: { name: '' }, # a blank name will trigger a validation error
}
end
it 'sets a flash error' do
subject
expect(flash[:error]).to eq assigns(:widget).errors.full_messages.join(', ')
end
end
end
describe '#destroy' do
let!(:widget) { Widget.create!(name: 'a widget') }
let(:params) { { id: widget.id } }
subject {
delete :destroy, params: params
}
it 'destroys the resource' do
expect { subject }.to change { Widget.count }.from(1).to(0)
end
context 'failure' do
let(:widget) { Widget.create!(name: 'undestroyable') }
let(:params) { { id: widget.id } }
context 'js format' do
subject { delete :destroy, params: params, format: 'js' }
it 'responds with error message' do
subject
expect(response).to be_unprocessable
expect(response.body).to eq assigns(:widget).errors.full_messages.to_sentence
end
end
context 'html format' do
subject { delete :destroy, params: params }
it 'responds with error message' do
subject
expect(response).to be_redirect
expect(flash[:error]).to eq assigns(:widget).errors.full_messages.to_sentence
end
end
end
end
describe '#update_positions' do
let(:widget_1) { Widget.create!(name: 'widget 1', position: 1) }
let(:widget_2) { Widget.create!(name: 'widget 2', position: 2) }
subject do
post :update_positions, params: { id: widget_1.to_param,
positions: { widget_1.id => '2', widget_2.id => '1' }, format: 'js' }
end
it 'updates the position of widget 1' do
expect { subject }.to change { widget_1.reload.position }.from(1).to(2)
end
it 'updates the position of widget 2' do
expect { subject }.to change { widget_2.reload.position }.from(2).to(1)
end
end
end