Skip to content

Commit

Permalink
Added specs for new validations
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusf committed Jul 14, 2015
1 parent 6b35a83 commit 3be3d55
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 20 deletions.
1 change: 1 addition & 0 deletions spec/factories/course_condition_achievements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
class: Course::Condition::Achievement.name, aliases: [:achievement_condition] do
course
achievement
association :conditional, factory: :course_achievement
end
end
1 change: 1 addition & 0 deletions spec/factories/course_condition_levels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
class: Course::Condition::Level.name, aliases: [:level_condition] do
course
minimum_level 1
association :conditional, factory: :course_level
end
end
7 changes: 3 additions & 4 deletions spec/features/course/achievement_listing_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'rails_helper'

RSpec.describe 'Course: Achievements', type: :feature do
subject { page }
RSpec.feature 'Course: Achievements' do

let!(:instance) { create(:instance) }

Expand All @@ -20,8 +19,8 @@
visit course_achievements_path(course)
end

context 'management buttons' do
it { is_expected.to have_link(nil, href: new_course_achievement_path(course)) }
it 'management buttons' do
expect(page).to have_link(nil, href: new_course_achievement_path(course))
end

it 'shows all achievements' do
Expand Down
70 changes: 54 additions & 16 deletions spec/features/course/achievement_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,60 @@
end
end

scenario 'I can create an achievement condition' do
achievement = create(:course_achievement, course: course)
visit edit_course_achievement_path(course, achievement)
condition_achievement = create(:course_achievement, course: course)
expect(page).to have_selector(:link_or_button,
I18n.t('course.condition.achievements.new.header'))
click_link I18n.t('course.condition.achievements.new.header')
expect(current_path).
to eq(new_course_achievement_condition_achievement_path(course, achievement))
find('#condition_achievement_achievement_id').
find(:css, "option[value='#{condition_achievement.id}']").
select_option
click_button I18n.t('helpers.submit.condition_achievement.create')
expect(current_path).to eq edit_course_achievement_path(course, achievement)
expect(page).to have_selector('tr.condition > td:nth-child(2)',
text: condition_achievement.title)
describe 'I can create an achievement condition' do
scenario 'if there is a valid achievement which can serve as a condition' do
achievement = create(:course_achievement, course: course)
visit edit_course_achievement_path(course, achievement)
condition_achievement = create(:course_achievement, course: course)
expect(page).to have_selector(:link,
I18n.t('course.condition.achievements.new.header'))
click_link I18n.t('course.condition.achievements.new.header')
expect(current_path).
to eq(new_course_achievement_condition_achievement_path(course, achievement))
find('#condition_achievement_achievement_id').
find(:css, "option[value='#{condition_achievement.id}']").
select_option
click_button I18n.t('helpers.submit.condition_achievement.create')
expect(current_path).to eq edit_course_achievement_path(course, achievement)
expect(page).to have_selector('tr.condition > td:nth-child(2)',
text: condition_achievement.title)
end

scenario 'unless there are no valid achievements' do
Course::Condition::Achievement.destroy_all
Course::Achievement.destroy_all
achievement = create(:course_achievement, course: course)
visit edit_course_achievement_path(course, achievement)

# The creation link won't be shown
expect(page).not_to have_selector(:link,
I18n.t('course.condition.achievements.new.header'))
end

scenario 'unless it has already been added or is the current one' do
_, achievement, other_achievement = create_achievement_condition
visit edit_course_achievement_path(course, achievement)
click_link I18n.t('course.condition.achievements.new.header')
expect(page).not_to have_selector('condition_achievement_achievement_id >' \
"option[value='#{other_achievement.id}']")
expect(page).not_to have_selector('#condition_achievement_achievement_id >' \
"option[value='#{achievement.id}']")
end

scenario 'unless no achievement was selected' do
achievement = create(:course_achievement, course: course)
visit edit_course_achievement_path(course, achievement)
click_link I18n.t('course.condition.achievements.new.header')
expect(current_path).
to eq(new_course_achievement_condition_achievement_path(course, achievement))
find('#condition_achievement_achievement_id').
find(:css, "option[value='']").select_option
click_button I18n.t('helpers.submit.condition_achievement.create')
expect(current_path).
to eq(course_achievement_condition_achievements_path(course.id, achievement.id))
expect(page).
to have_text(I18n.t('course.condition.errors.none_selected'))
end
end

scenario 'I can edit an achievement condition' do
Expand Down
35 changes: 35 additions & 0 deletions spec/models/course/condition/achievement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,39 @@

RSpec.describe Course::Condition::Achievement, type: :model do
it { is_expected.to act_as(:condition).class_name(Course::Condition.name) }
it { is_expected.to respond_to(:name) }

let!(:instance) { create(:instance) }
with_tenant(:instance) do

let(:course) { create(:course) }

describe 'validations' do
it 'is invalid without an achievement' do
expect(subject.valid?).to be_falsey
end
it 'is invalid when its achievement is its conditional, causing a circular reference' do
conditional = create(:course_achievement, course: course)
subject.achievement = conditional
subject.conditional = conditional
expect(subject.valid?).to be_falsey
end
it "is invalid when its achievement is included in its conditional's conditions" do
achievement = create(:course_achievement, course: course)
condition = create(:course_condition_achievement, course: course,
achievement: achievement)
conditional = create(:course_achievement, course: course, conditions: [condition])
subject.conditional = conditional
subject.achievement = achievement
expect(subject.valid?).to be_falsey
end
end

describe '#name' do
it 'returns the title of the achievement' do
subject.achievement = create(:course_achievement, course: course)
expect(subject.name).to eq(subject.achievement.title)
end
end
end
end
6 changes: 6 additions & 0 deletions spec/models/course/condition/level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

RSpec.describe Course::Condition::Level, type: :model do
it { is_expected.to act_as(:condition).class_name(Course::Condition.name) }
it { is_expected.to respond_to(:name) }

it do
subject.minimum_level = 10
expect(subject.name).to eq('Level 10')
end
end

0 comments on commit 3be3d55

Please sign in to comment.