Skip to content

Commit

Permalink
[1419] Move controller specs to request specs #2 (#1484)
Browse files Browse the repository at this point in the history
* Move controller spec to request spec

* Renaming specs and changing diaper_drive factory

* diaper drive participants csv import spec
  • Loading branch information
moacirguedes committed Feb 6, 2020
1 parent 68d7319 commit f16cdb5
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 180 deletions.
90 changes: 0 additions & 90 deletions spec/controllers/diaper_drive_participants_controller_spec.rb

This file was deleted.

63 changes: 0 additions & 63 deletions spec/controllers/diaper_drives_controller_spec.rb

This file was deleted.

3 changes: 2 additions & 1 deletion spec/factories/diaper_drive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
factory :diaper_drive do
name { "Test Drive" }
start_date { Time.current }
end_date { Time.current }
organization { Organization.try(:first) || create(:organization) }
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'csv'
require 'rails_helper'

RSpec.describe DataExportsController, type: :controller do
RSpec.describe "DataExports", type: :request do
let(:default_params) do
{ organization_id: @organization.to_param }
end
Expand All @@ -12,13 +13,13 @@

describe "GET #csv" do
it "return empty data when no type is passed" do
get :csv, format: "csv", params: default_params
get csv_path(default_params, format: "csv")
expect(response.parsed_body).to be_empty
end

it "returns data when a valid type is requested" do
DataExport::SUPPORTED_TYPES.each do |type|
get :csv, format: "csv", params: default_params.merge(type: type)
get csv_path(default_params.merge(type: type, format: "csv"))
expect(response.parsed_body).to_not be_empty
end
end
Expand Down
132 changes: 132 additions & 0 deletions spec/requests/diaper_drive_participants_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
require 'rails_helper'

RSpec.describe "DiaperDriveParticipants", type: :request do
let(:default_params) do
{ organization_id: @organization.to_param }
end

context "While signed in" do
before do
sign_in(@user)
end

describe "GET #index" do
it "returns http success" do
get diaper_drive_participants_path(default_params)
expect(response).to be_successful
end
end

describe "GET #new" do
it "returns http success" do
get new_diaper_drive_participant_path(default_params)
expect(response).to be_successful
end
end

describe "GET #edit" do
it "returns http success" do
get edit_diaper_drive_participant_path(default_params.merge(id: create(:diaper_drive_participant, organization: @user.organization)))
expect(response).to be_successful
end
end

describe "POST #import_csv" do
let(:model_class) { DiaperDriveParticipant }

context "with a csv file" do
let(:file) { fixture_file_upload("#{model_class.name.underscore.pluralize}.csv", "text/csv") }
subject { post import_csv_diaper_drive_participants_path(default_params), params: { file: file } }

it "invokes .import_csv" do
expect(model_class).to respond_to(:import_csv).with(2).arguments
end

it "redirects" do
subject
expect(response).to be_redirect
end

it "presents a flash notice message" do
subject
expect(response).to have_notice "#{model_class.name.underscore.humanize.pluralize} were imported successfully!"
end
end

context "without a csv file" do
subject { post import_csv_diaper_drive_participants_path(default_params) }

it "redirects to :index" do
subject
expect(response).to be_redirect
end

it "presents a flash error message" do
subject
expect(response).to have_error "No file was attached!"
end
end

context "csv file with wrong headers" do
let(:file) { fixture_file_upload("wrong_headers.csv", "text/csv") }
subject { post import_csv_diaper_drive_participants_path(default_params), params: { file: file } }

it "redirects" do
subject
expect(response).to be_redirect
end

it "presents a flash error message" do
subject
expect(response).to have_error "Check headers in file!"
end
end
end

describe "GET #show" do
it "returns http success" do
get diaper_drive_participant_path(default_params.merge(id: create(:diaper_drive_participant, organization: @organization)))
expect(response).to be_successful
end
end

describe "XHR #create" do
it "successful create" do
post diaper_drive_participants_path(default_params.merge(diaper_drive_participant: { name: "test", email: "123@mail.ru" }, xhr: true))
expect(response).to be_successful
end

it "flash error" do
post diaper_drive_participants_path(default_params.merge(diaper_drive_participant: { name: "test" }, xhr: true))
expect(response).to be_successful
expect(response).to have_error(/try again/i)
end
end

describe "POST #create" do
it "successful create" do
post diaper_drive_participants_path(default_params.merge(diaper_drive_participant: { business_name: "businesstest",
contact_name: "test", email: "123@mail.ru" }))
expect(response).to redirect_to(diaper_drive_participants_path)
expect(response).to have_notice(/added!/i)
end

it "flash error" do
post diaper_drive_participants_path(default_params.merge(diaper_drive_participant: { name: "test" }, xhr: true))
expect(response).to be_successful
expect(response).to have_error(/try again/i)
end
end

context "Looking at a different organization" do
let(:object) { create(:diaper_drive_participant, organization: create(:organization)) }
include_examples "requiring authorization"
end
end

context "While not signed in" do
let(:object) { create(:diaper_drive_participant) }

include_examples "requiring authorization"
end
end
63 changes: 63 additions & 0 deletions spec/requests/diaper_drives_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rails_helper'

RSpec.describe "DiaperDrives", type: :request do
let(:default_params) do
{ organization_id: @organization.id.to_param }
end

context "While signed in >" do
let(:diaper_drive) { create(:diaper_drive) }
before do
sign_in(@user)
end

describe "GET #index" do
it "returns http success" do
get diaper_drives_path(default_params)
expect(response).to be_successful
end
end

describe "GET #new" do
it "returns http success" do
get new_diaper_drive_path(default_params)
expect(response).to be_successful
end
end

describe "POST#create" do
it "returns redirect http status" do
post diaper_drives_path(default_params.merge(diaper_drive: attributes_for(:diaper_drive)))
expect(response).to have_http_status(:redirect)
end
end

describe "PUT#update" do
it "returns redirect http status" do
put diaper_drive_path(default_params.merge(id: diaper_drive.id, diaper_drive: attributes_for(:diaper_drive)))
expect(response).to have_http_status(:redirect)
end
end

describe "GET #edit" do
it "returns http success" do
get edit_diaper_drive_path(default_params.merge(id: diaper_drive.id))
expect(response).to be_successful
end
end

describe "GET #show" do
it "returns http success" do
get diaper_drive_path(default_params.merge(id: diaper_drive.id))
expect(response).to be_successful
end
end

describe "DELETE #destroy" do
it "redirects to the index" do
delete diaper_drive_path(default_params.merge(id: diaper_drive.id))
expect(response).to redirect_to(diaper_drives_path)
end
end
end
end
Loading

0 comments on commit f16cdb5

Please sign in to comment.