Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Comment < ActiveRecord::Base
validates_presence_of :author
validates_presence_of :text
end
34 changes: 34 additions & 0 deletions spec/features/add_new_comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "rails_helper"
require "features/shared/examples"
require "features/shared/contexts"

feature "Add new comment" do
context "from main page", page: :main, js: true do
context "via Horizontal Form", form: :horizontal do
include_examples "New Comment Submission"
end
context "via Inline Form", form: :inline do
include_examples "New Comment Submission"
end
context "via Stacked Form", form: :stacked do
include_examples "New Comment Submission"
end
end

context "from simple page", page: :simple, js: true do
context "via Horizontal Form", form: :horizontal do
include_examples "New Comment Submission"
end
context "via Inline Form", form: :inline do
include_examples "New Comment Submission"
end
context "via the Stacked Form", form: :stacked do
include_examples "New Comment Submission"
end
end

context "from classic page", page: :classic do
background { click_link "New Comment" }
include_examples "New Comment Submission"
end
end
131 changes: 0 additions & 131 deletions spec/features/comments_spec.rb

This file was deleted.

14 changes: 14 additions & 0 deletions spec/features/destroy_comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "rails_helper"
require "features/shared/contexts"

feature "Destroy a comment", existing_comment: true do
context "from classic page", page: :classic do
let(:comment) { Comment.first }

scenario "clicking destroy link destroys comment" do
click_link "Destroy", href: comment_path(comment)
expect(page).to_not have_css(".comment", text: comment.author)
expect(page).to_not have_css(".comment", text: comment.text)
end
end
end
39 changes: 39 additions & 0 deletions spec/features/edit_comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "rails_helper"
require "features/shared/examples"
require "features/shared/contexts"

feature "Edit a comment", existing_comment: true do
let(:comment) { Comment.first }

context "from classic page", page: :classic do
background { click_link "Edit", match: :first }

context "when edit is submitted" do
let(:edited_name) { "Abraham Lincoln" }
include_context "Form Submitted", name: :edited_name

scenario "comment is updated" do
expect(page).to have_css(".comment", text: :edited_name)
expect(page).to have_success_message
end
end

context "when edit is submitted with blank fields", blank_form_submitted: true do
scenario "comment is not updated" do
expect(page).not_to have_success_message
expect(page).to have_failure_message
expect(page).not_to have_css(".comment", text: "")
end
end
end
end

private

def have_success_message # rubocop:disable Style/PredicateName
have_css("#notice", "Comment was successfully created.")
end

def have_failure_message # rubocop:disable Style/PredicateName
have_css("#error_explanation")
end
46 changes: 46 additions & 0 deletions spec/features/shared/contexts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "rails_helper"

# Pages
shared_context "Main Page", page: :main do
background { visit root_path }
end
shared_context "Simple Page", page: :simple do
background { visit simple_path }
end
shared_context "Classic Page", page: :classic do
background { visit comments_path }
end

# Forms
shared_context "Horizontal Form", form: :horizontal do
background { click_link "Horizontal Form" }
end
shared_context "Inline Form", form: :inline do
background { click_link "Inline Form" }
end
shared_context "Stacked Form", form: :stacked do
background { click_link "Stacked Form" }
end

# Form Submission
shared_context "Form Submitted", form_submitted: true do |name: "Spicoli", text: "dude!"|
let(:hint_name) { "Your Name" }
let(:hint_text) { "Say something using markdown..." }
let(:name) { name }
let(:text) { text }

background do
fill_in hint_name, with: name
fill_in hint_text, with: text
click_button "Post"
end
end

shared_context "Form Submitted with Blank Fields", blank_form_submitted: true do
include_context "Form Submitted", name: "", text: ""
end

# Fixtures
shared_context "Existing Comment", existing_comment: true do
before { Comment.create(author: "John Doe", text: "Hello there!") }
end
32 changes: 32 additions & 0 deletions spec/features/shared/examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "rails_helper"
require "features/shared/contexts"

shared_examples "New Comment Submission" do
context "when the new comment is submitted" do
let(:name) { "John Smith" }
let(:text) { "Hello there!" }
include_context "Form Submitted", name: :name, text: :text

scenario "comment is added" do
expect(page).to have_css(".comment", text: name)
expect(page).to have_css(".comment", text: text)
end
end

context "when the new comment is submmited with blank fields", blank_form_submitted: true do
let!(:comments_count) { all(".comment").size }

scenario "comment is not added" do
expect(page).to have_selector(".comment", count: comments_count)
end
end

context "with iframe text" do
let(:iframe_text) { "<iframe src=\"http://www.w3schools.com\"></iframe>" }
include_context "Form Submitted", text: :iframe_text

scenario "doesn't add an iframe" do
expect(page).not_to have_css("iframe")
end
end
end