Skip to content

Commit f16a634

Browse files
YaroSpacejust3ws
authored andcommitted
added specs for all related code
added specs for success/filure notifications
1 parent 52f764a commit f16a634

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

app/controllers/teams_controller.rb

+13-13
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,31 @@ def new
7878

7979
def create
8080
team_params = params.require(:team).permit(:selected, :slug, :name)
81+
selected = team_params.fetch(:selected, nil)
82+
team_name = team_params.fetch(:name, '')
8183

82-
if team_params.fetch(:selected, nil) == 'true'
84+
@teams = Team.with_similar_names(team_name)
85+
86+
if selected == 'true'
8387
@team = Team.where(slug: team_params[:slug]).first
8488
render :create and return
8589
end
8690

87-
@teams = Team.with_similar_names(team_params[:name])
88-
unless @teams.empty?
89-
@new_team_name = team_params[:name]
91+
unless selected == 'false' || @teams.empty?
92+
@new_team_name = team_name
9093
render 'similar_teams' and return
9194
end
9295

93-
@team = Team.new(name: team_params[:name])
96+
@team = Team.new(name: team_name)
9497
if @team.save
95-
# TODO show flash on success or falure
9698
record_event('created team')
9799
@team.add_user(current_user)
98-
end
99-
end
100100

101-
def get_similar_teams(name)
102-
name.gsub!(/ \-\./, '.*')
103-
#TODO move to Team scope
104-
teams = Team.any_of({ :name => /#{name}.*/i }).limit(3).to_a
105-
teams.empty? ? nil : teams
101+
flash.now[:notice] = "Successfully created a team #{@team.name}"
102+
render :create
103+
else
104+
flash[:error] = "There was an error in creating a team #{@team.name}"
105+
end
106106
end
107107

108108
def edit

spec/controllers/teams_controller_spec.rb

+22-7
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,24 @@
3939
end
4040
end
4141

42-
describe "#create" , focus: true do
43-
let(:team) { Fabricate.build(:team) }
44-
45-
it 'renders an error message if action was unsuccessful' do
42+
describe "#create" do
43+
let(:team) { Fabricate.build(:team, name: 'team_name') }
4644

45+
before do
46+
allow(Team).to receive(:with_similar_names).and_return([])
4747
end
4848

4949
context 'a team is selected from a list of similar teams' do
50-
it 'renders a template with a choice of tariff plans when user selects a team' do
50+
it 'renders a template with a choice of tariff plans when user picks a name from existing names' do
51+
allow(Team).to receive(:where).and_return(['team_1', 'team_2'])
5152
post :create, :team => { selected: 'true', slug: 'team_name' }, format: :js
53+
54+
expect(assigns[:team]).to eq('team_1')
55+
expect(response).to render_template('create')
56+
end
57+
58+
it 'renders a template with a choice of tariff plans if user picks his own team name' do
59+
post :create, :team => { name: 'team_name', selected: 'false' }, format: :js
5260
expect(response).to render_template('create')
5361
end
5462
end
@@ -57,8 +65,6 @@
5765
let(:response) { post :create, :team => { name: 'team_name' }, format: :js }
5866

5967
before do
60-
allow(controller).to receive(:get_similar_teams).and_return(nil)
61-
6268
allow(Team).to receive(:new).and_return(team)
6369
allow(team).to receive(:save).and_return(true)
6470
allow(team).to receive(:add_user).and_return(true)
@@ -82,6 +88,13 @@
8288
it 'renders template with option to join' do
8389
expect(response).to be_success
8490
expect(response).to render_template('create')
91+
expect(flash[:notice]).to eq("Successfully created a team team_name")
92+
end
93+
94+
it 'renders failure notice' do
95+
allow(team).to receive(:save).and_return(false)
96+
response
97+
expect(flash[:error]).to eq("There was an error in creating a team team_name")
8598
end
8699
end
87100

@@ -93,6 +106,8 @@
93106

94107
it 'renders a template with a list of similar teams' do
95108
post :create, :team => { name: 'team_name' }, format: :js
109+
110+
expect(assigns[:new_team_name]).to eq('team_name')
96111
expect(response).to render_template('similar_teams')
97112
end
98113
end

0 commit comments

Comments
 (0)