REFACTOR: admin color schemes controller specs to requests (#5964)

This commit is contained in:
OsamaSayegh 2018-06-11 07:48:58 +03:00 committed by Guo Xiang Tan
parent 767f022b29
commit da94eaa81d
1 changed files with 30 additions and 22 deletions

View File

@ -6,7 +6,7 @@ describe Admin::ColorSchemesController do
end end
context "while logged in as an admin" do context "while logged in as an admin" do
let!(:user) { log_in(:admin) } let(:admin) { Fabricate(:admin) }
let(:valid_params) { { color_scheme: { let(:valid_params) { { color_scheme: {
name: 'Such Design', name: 'Such Design',
colors: [ colors: [
@ -16,19 +16,24 @@ describe Admin::ColorSchemesController do
} }
} } } }
describe "index" do before do
sign_in(admin)
end
describe "#index" do
it "returns JSON" do it "returns JSON" do
Fabricate(:color_scheme) scheme_name = Fabricate(:color_scheme).name
get :index, format: :json get "/admin/color_schemes.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(::JSON.parse(response.body)).to be_present schemes = JSON.parse(response.body).map { |scheme| scheme["name"] }
expect(schemes).to include(scheme_name)
end end
end end
describe "create" do describe "#create" do
it "returns JSON" do it "returns JSON" do
post :create, params: valid_params, format: :json post "/admin/color_schemes.json", params: valid_params
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(::JSON.parse(response.body)['id']).to be_present expect(::JSON.parse(response.body)['id']).to be_present
@ -38,45 +43,48 @@ describe Admin::ColorSchemesController do
params = valid_params params = valid_params
params[:color_scheme][:colors][0][:hex] = 'cool color please' params[:color_scheme][:colors][0][:hex] = 'cool color please'
post :create, params: valid_params, format: :json post "/admin/color_schemes.json", params: valid_params
expect(response).not_to be_successful expect(response.status).to eq(422)
expect(::JSON.parse(response.body)['errors']).to be_present expect(::JSON.parse(response.body)['errors']).to be_present
end end
end end
describe "update" do describe "#update" do
let(:existing) { Fabricate(:color_scheme) } let(:existing) { Fabricate(:color_scheme) }
it "returns success" do it "returns success" do
ColorSchemeRevisor.expects(:revise).returns(existing) put "/admin/color_schemes/#{existing.id}.json", params: valid_params
put :update, params: valid_params.merge(id: existing.id), format: :json
expect(response.status).to eq(200) expect(response.status).to eq(200)
end
it "returns JSON" do existing.reload
ColorSchemeRevisor.expects(:revise).returns(existing) new_colors = valid_params[:color_scheme][:colors]
put :update, params: valid_params.merge(id: existing.id), format: :json updated_colors = existing.colors.map { |color| { name: color.name, hex: color.hex } }
expect(::JSON.parse(response.body)['id']).to be_present
expect(new_colors & updated_colors).to eq(new_colors)
expect(existing.name).to eq(valid_params[:color_scheme][:name])
end end
it "returns failure with invalid params" do it "returns failure with invalid params" do
color_scheme = Fabricate(:color_scheme) color_scheme = Fabricate(:color_scheme)
params = valid_params.merge(id: color_scheme.id) params = valid_params
params[:color_scheme][:colors][0][:name] = color_scheme.colors.first.name params[:color_scheme][:colors][0][:name] = color_scheme.colors.first.name
params[:color_scheme][:colors][0][:hex] = 'cool color please' params[:color_scheme][:colors][0][:hex] = 'cool color please'
put :update, params: params, format: :json
expect(response).not_to be_successful put "/admin/color_schemes/#{color_scheme.id}.json", params: params
expect(response.status).to eq(422)
expect(::JSON.parse(response.body)['errors']).to be_present expect(::JSON.parse(response.body)['errors']).to be_present
end end
end end
describe "destroy" do describe "#destroy" do
let!(:existing) { Fabricate(:color_scheme) } let!(:existing) { Fabricate(:color_scheme) }
it "returns success" do it "returns success" do
expect { expect {
delete :destroy, params: { id: existing.id }, format: :json delete "/admin/color_schemes/#{existing.id}.json"
}.to change { ColorScheme.count }.by(-1) }.to change { ColorScheme.count }.by(-1)
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end