Skip to content

Commit

Permalink
Add TrackControllers specs
Browse files Browse the repository at this point in the history
  • Loading branch information
AEtherC0r3 committed Jul 13, 2017
1 parent cee2bdd commit 111bf9c
Show file tree
Hide file tree
Showing 4 changed files with 438 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/tracks_controller.rb
Expand Up @@ -31,7 +31,7 @@ def create

def update
if @track.update_attributes(track_params)
redirect_to admin_conference_program_tracks_path(conference_id: @conference.short_title),
redirect_to conference_program_tracks_path(conference_id: @conference.short_title),
notice: 'Track request successfully updated.'
else
flash.now[:error] = "Track request update failed: #{@track.errors.full_messages.join('. ')}."
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/tracks/index.html.haml
Expand Up @@ -42,9 +42,9 @@
N/A
%td
- if track.self_organized?
= check_box_tag "#{@conference.short_title}_#{track.id}", track.id, track.cfp_active,
= check_box_tag "#{@conference.short_title}_#{track.short_name}", track.id, track.cfp_active,
class: 'switch-checkbox', method: :patch,
url: toggle_cfp_inclusion_admin_conference_program_track_path(@conference.short_title, id: track.id)+"?included=",
url: toggle_cfp_inclusion_admin_conference_program_track_path(@conference.short_title, id: track.short_name)+"?included=",
data: { size: 'small',
on_color: 'success',
off_color: 'warning',
Expand Down
256 changes: 256 additions & 0 deletions spec/controllers/admin/tracks_controller_spec.rb
@@ -0,0 +1,256 @@
require 'spec_helper'

describe Admin::TracksController do
let(:admin) { create(:admin) }

let(:conference) { create(:conference) }
let!(:track) { create(:track, program: conference.program, color: '#800080') }
let!(:self_organized_track) { create(:track, :self_organized, program: conference.program) }

before :each do
sign_in(admin)
end

describe 'GET #index' do
before :each do
get :index, conference_id: conference.short_title
end

it 'assigns @tracks with the correct values' do
expect(assigns(:tracks).length).to eq 2
expect(assigns(:tracks).include?(track)).to eq true
expect(assigns(:tracks).include?(self_organized_track)).to eq true
end

it 'renders the index template' do
expect(response).to render_template :index
end
end

describe 'GET #show' do
before :each do
get :show, conference_id: conference.short_title, id: track.short_name
end

it 'assigns the correct track' do
expect(assigns(:track)).to eq track
end

it 'renders the show template' do
expect(response).to render_template :show
end
end

describe 'GET #new' do
before :each do
get :new, conference_id: conference.short_title
end

it 'assigns a new track with the correct conference' do
expect(assigns(:track)).to be_a Track
expect(assigns(:track).new_record?).to eq true
expect(assigns(:track).program_id).to eq conference.program.id
end

it 'renders the new template' do
expect(response).to render_template :new
end
end

describe 'POST #create' do
context 'saves successfuly' do
before :each do
post :create, track: attributes_for(:track), conference_id: conference.short_title
end

it 'assigns a new track with the correct conference' do
expect(assigns(:track)).to be_a Track
expect(assigns(:track).new_record?).to eq false
expect(assigns(:track).program_id).to eq conference.program.id
end

it 'redirects to admin tracks index path' do
expect(response).to redirect_to admin_conference_program_tracks_path(conference_id: conference.short_title)
end

it 'shows success message in flash notice' do
expect(flash[:notice]).to match('Track successfully created.')
end

it 'creates new track' do
expect(Track.find(assigns(:track).id)).to be_a Track
end
end

context 'save fails' do
before :each do
allow_any_instance_of(Track).to receive(:save).and_return(false)
post :create, track: attributes_for(:track, short_name: 'my_track'), conference_id: conference.short_title
end

it 'assigns a new track with the correct conference' do
expect(assigns(:track)).to be_a Track
expect(assigns(:track).new_record?).to eq true
expect(assigns(:track).program_id).to eq conference.program.id
end

it 'renders the new template' do
expect(response).to render_template :new
end

it 'shows error in flash message' do
expect(flash[:error]).to match("Creating Track failed: #{assigns(:track).errors.full_messages.join('. ')}.")
end

it 'does not create a new track' do
expect(conference.program.tracks.find_by(short_name: 'my_track')).to eq nil
end
end
end

describe 'GET #edit' do
before :each do
get :edit, conference_id: conference.short_title, id: track.short_name
end

it 'assigns the correct track' do
expect(assigns(:track)).to eq track
end

it 'renders the show template' do
expect(response).to render_template :edit
end
end

describe 'PATCH #update' do
context 'updates successfully' do
before :each do
patch :update, track: attributes_for(:track, color: '#FF0000'),
conference_id: conference.short_title,
id: track.short_name
end

it 'assigns the correct track' do
expect(assigns(:track)).to eq track
end

it 'redirects to admin tracks index path' do
expect(response).to redirect_to admin_conference_program_tracks_path(conference_id: conference.short_title)
end

it 'shows success message in flash notice' do
expect(flash[:notice]).to match('Track successfully updated.')
end

it 'updates the track' do
track.reload
expect(track.color).to eq '#FF0000'
end
end

context 'update fails' do
before :each do
allow_any_instance_of(Track).to receive(:save).and_return(false)
patch :update, track: attributes_for(:track, color: '#FF0000'),
conference_id: conference.short_title,
id: track.short_name
end

it 'assigns the correct track' do
expect(assigns(:track)).to eq track
end

it 'renders edit template' do
expect(response).to render_template :edit
end

it 'shows error in flash message' do
expect(flash[:error]).to match("Track update failed: #{assigns(:track).errors.full_messages.join('. ')}.")
end

it 'does not update the track' do
track.reload
expect(track.color).to eq '#800080'
end
end
end

describe 'DELETE #destroy' do
context 'deletes successfully' do
before :each do
delete :destroy, conference_id: conference.short_title, id: track.short_name
end

it 'redirects to admin tracks index path' do
expect(response).to redirect_to admin_conference_program_tracks_path(conference_id: conference.short_title)
end

it 'shows success message in flash notice' do
expect(flash[:notice]).to match('Track successfully deleted.')
end

it 'deletes the track' do
expect(Track.find_by(id: track)).to eq nil
end
end

context 'delete fails' do
before :each do
allow_any_instance_of(Track).to receive(:destroy).and_return(false)
delete :destroy, conference_id: conference.short_title, id: track.short_name
end

it 'assigns the correct track' do
expect(assigns(:track)).to eq track
end

it 'redirects to admin tracks index path' do
expect(response).to redirect_to admin_conference_program_tracks_path(conference_id: conference.short_title)
end

it 'shows error in flash message' do
expect(flash[:error]).to match("Track couldn't be deleted. #{track.errors.full_messages.join('. ')}.")
end

it 'does not delete the track' do
expect(Track.find(track.id)).to eq track
end
end
end

describe 'PATCH #toggle_cfp_inclusion' do
context 'cfp_active is false' do
before :each do
self_organized_track.cfp_active = false
self_organized_track.save!
patch :toggle_cfp_inclusion, conference_id: conference.short_title, id: self_organized_track.short_name
self_organized_track.reload
end

it 'assigns the correct track' do
expect(assigns(:track)).to eq self_organized_track
end

it 'becomes true' do
expect(self_organized_track.cfp_active).to eq true
end
end

context 'cfp_active is true' do
before :each do
self_organized_track.cfp_active = true
self_organized_track.save!
patch :toggle_cfp_inclusion, conference_id: conference.short_title, id: self_organized_track.short_name
self_organized_track.reload
end

it 'assigns the correct track' do
expect(assigns(:track)).to eq self_organized_track
end

it 'becomes false' do
expect(self_organized_track.cfp_active).to eq false
end
end
end
end

0 comments on commit 111bf9c

Please sign in to comment.