Add tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
class Command::GoToCard < Command
|
||||
store_accessor :data, :card_id
|
||||
|
||||
validates_presence_of :card_id
|
||||
validates_presence_of :card
|
||||
|
||||
def title
|
||||
"Visit card '#{card.title}'"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class Command::GoToUser < Command
|
||||
store_accessor :data, :user_id
|
||||
|
||||
validates_presence_of :user_id
|
||||
validates_presence_of :user
|
||||
|
||||
def title
|
||||
"View profile of '#{user.name}'"
|
||||
@@ -13,6 +13,6 @@ class Command::GoToUser < Command
|
||||
|
||||
private
|
||||
def user
|
||||
User.find(user_id)
|
||||
User.find_by_id(user_id)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,7 +22,7 @@ class Command::Parser::Context
|
||||
attr_reader :controller, :action, :params
|
||||
|
||||
def extract_url_components(url)
|
||||
uri = URI.parse(url)
|
||||
uri = URI.parse(url || "")
|
||||
route = Rails.application.routes.recognize_path(uri.path)
|
||||
@controller = route[:controller]
|
||||
@action = route[:action]
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
require "test_helper"
|
||||
|
||||
class Command::GoToCardTest < ActionDispatch::IntegrationTest
|
||||
include CommandTestHelper
|
||||
|
||||
setup do
|
||||
@card = cards(:logo)
|
||||
end
|
||||
|
||||
test "redirects to the card perma" do
|
||||
result = execute_command "#{@card.id}"
|
||||
|
||||
assert_equal @card, result.url
|
||||
end
|
||||
|
||||
test "results in a regular search if the card does not exist" do
|
||||
command = parse_command "123"
|
||||
assert command.valid?
|
||||
|
||||
result = command.execute
|
||||
assert_equal cards_path(indexed_by: "newest", terms: [ "123" ]), result.url
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
require "test_helper"
|
||||
|
||||
class Command::GoToUserTest < ActionDispatch::IntegrationTest
|
||||
include CommandTestHelper
|
||||
|
||||
test "redirects to the user perma" do
|
||||
result = execute_command "@kevin"
|
||||
|
||||
assert_equal users(:kevin), result.url
|
||||
end
|
||||
|
||||
test "results in an invalid command if the user does not exist" do
|
||||
command = parse_command "@not_a_user"
|
||||
assert !command.valid?
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
# The parser is tested through the tests of specific commands. See +Command::AssignTests+, etc.
|
||||
@@ -0,0 +1,17 @@
|
||||
require "test_helper"
|
||||
|
||||
class Command::SearchTest < ActionDispatch::IntegrationTest
|
||||
include CommandTestHelper
|
||||
|
||||
test "redirects to the cards index filtering by search terms" do
|
||||
result = execute_command "some text"
|
||||
|
||||
assert_equal cards_path(indexed_by: "newest", terms: [ "some text" ]), result.url
|
||||
end
|
||||
|
||||
test "respects existing filters" do
|
||||
result = execute_command "some text", context_url: "http://37signals.fizzy.localhost:3006/cards?collection_ids%5B%5D=#{collections(:writebook).id}"
|
||||
|
||||
assert_equal cards_path(indexed_by: "newest", collection_ids: [collections(:writebook).id], terms: [ "some text" ]), result.url
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
module CommandTestHelper
|
||||
def execute_command(string, context_url: nil, user: users(:david))
|
||||
parse_command(string, context_url: context_url, user: user).execute
|
||||
end
|
||||
|
||||
def parse_command(string, context_url: nil, user: users(:david))
|
||||
context = Command::Parser::Context.new(user, url: context_url)
|
||||
parser = Command::Parser.new(context)
|
||||
parser.parse(string).tap do |command|
|
||||
command.user = user if command
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user