From c22b5298585e86a67773c6c19bca57e68c724375 Mon Sep 17 00:00:00 2001 From: sto Date: Sun, 22 Jun 2025 07:55:23 +0200 Subject: [PATCH] Add contest feature specs --- spec/features/contest_spec.rb | 75 +++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/spec/features/contest_spec.rb b/spec/features/contest_spec.rb index c2b39b2..aa2f88c 100644 --- a/spec/features/contest_spec.rb +++ b/spec/features/contest_spec.rb @@ -1,17 +1,78 @@ require 'rails_helper' RSpec.feature "Contests", type: :feature do - context "visiting the home page" do - let!(:user) { create(:user) } + let!(:user) { create(:user) } - before do - login(user) + before do + login(user) + end + + context "index" do + let!(:first_contest) { create(:contest, user: user) } + let!(:second_contest) { create(:contest, user: user) } + + it "should list existing contests and offer to open them" do + visit contests_path + + expect(page).to have_content(I18n.t("contests.index.title", username: user.username)) + expect(page).to have_content(first_contest.name) + expect(page).to have_content(second_contest.name) + + find(".stretched-link[href=\"/contests/#{first_contest.friendly_id}\"]").click + + expect(page).to have_current_path("/contests/#{first_contest.friendly_id}") end - it "should display the username" do - visit root_path + it "should offer to create a new contest" do + visit contests_path - expect(page).to have_content(user.username) + click_link I18n.t("contests.index.new_contest") + + expect(page).to have_current_path("/contests/new") + end + end + + context "new" do + it "should prevent creating contests without a name" do + visit new_contest_path + + click_button I18n.t("helpers.buttons.create") + + expect(page).to have_content(I18n.t("activerecord.errors.models.contest.attributes.name.blank")) + end + + it "should allow creating new contests with valid parameters" do + visit new_contest_path + + fill_in I18n.t("activerecord.attributes.contest.name"), with: "Contest name" + + click_button I18n.t("helpers.buttons.create") + + expect(page).to have_current_path(contest_path(Contest.find_by(name: "Contest name"))) + end + end + + context "edit" do + let!(:contest) { create(:contest, user: user) } + + it "should prevent editing contests without a name" do + visit edit_contest_path(contest) + + fill_in I18n.t("activerecord.attributes.contest.name"), with: "" + + click_button I18n.t("helpers.buttons.save") + + expect(page).to have_content(I18n.t("activerecord.errors.models.contest.attributes.name.blank")) + end + + it "should allow editing contests with valid parameters" do + visit edit_contest_path(contest) + + fill_in I18n.t("activerecord.attributes.contest.name"), with: "Contest name" + + click_button I18n.t("helpers.buttons.save") + + expect(page).to have_current_path(contest_path(contest)) end end end