require 'test_helper'
class ListItemPresenterTest < ActiveSupport::TestCase
context "A list item presenter instance" do
setup do
@customer = Factory(:customer)
@product_list = Factory(:product_list, :customer => @customer)
@list_item = Factory(:list_item, :product_list => @product_list)
@list_item_presenter = ListItemPresenter.new(:list_item => @list_item, :customer => @customer)
end
should "know what templates are available to pick from" do
product_templates = stub
product_templates.expects(:by_customer_department).with(@product_list.customer_department).returns([1,2,3])
@customer.expects(:product_templates).returns(product_templates)
assert_equal [1,2,3], @list_item_presenter.available_templates
end
should "know product_template_id" do
@list_item.stubs(:product_variant).returns(nil)
assert_nil @list_item_presenter.product_template_id
product_variant = stub
product_variant.expects(:product_template_id).returns(6)
@list_item.stubs(:product_variant).returns(product_variant)
assert_equal 6, @list_item_presenter.product_template_id
end
should "know the available sources for the form" do
assert_equal [:template, :standard_product], @list_item_presenter.available_sources
end
context "calculating source" do
context "by parsing" do
setup do
# make parse_source public so we can test it cleanly
ListItemPresenter.send :public, :parse_source
@list_item_presenter.stubs(:available_sources).returns([:one, :two, :three])
end
should "return the first available source if no source is supplied" do
assert_equal :one, @list_item_presenter.parse_source
end
should "return a symbolized version of what's passed if present in available_sources" do
assert_equal :three, @list_item_presenter.parse_source("three")
end
should "return the first available source if source isn't included" do
assert_equal :one, @list_item_presenter.parse_source(123)
end
end
should "allow parsing of input when there is an argument to source" do
@list_item_presenter.expects(:parse_source).with("PASSED SOURCE").returns("PARSED")
assert_equal "PARSED", @list_item_presenter.source("PASSED SOURCE")
end
should "allow assignment of source during initialization" do
ListItemPresenter.any_instance.expects(:parse_source).with("MY SOURCE").returns("PARSED")
@list_item_presenter = ListItemPresenter.new(:list_item => @list_item, :customer => @customer, :source => "MY SOURCE")
assert_equal "PARSED", @list_item_presenter.source
end
end
should "use method_missing to pick up all other method calls" do
@list_item.expects(:another_method).returns("result")
assert_equal "result", @list_item_presenter.another_method
end
end
end