1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionview/test/template/record_identifier_test.rb
claudiob b26338e83b Better tests for AV::RecordIdentifier
This commit intends to clarify the scope of ActionView::RecordIdentifier
methods `dom_id` and `dom_class`.

Most of the current documentation comes from da257eb8 (7 years ago) when
the decoupling of ActionView, ActiveRecord and ActiveModel was not a concern.

Since then, steps have been taken to reach such decoupling.
Therefore I think it's important to show that ActionView::RecordIdentifier
**does not strictly depend on the ActiveRecord API**:
any class `Post` implementing `post.to_key` and `post.model_name.param_key`
will work.

This commit adds a test to prove that ActionView::RecordIdentifier methods
can also be used on objects that do not subclass ActiveRecord::Base.
2014-12-23 01:59:25 +01:00

91 lines
2.2 KiB
Ruby

require 'abstract_unit'
require 'controller/fake_models'
class RecordIdentifierTest < ActiveSupport::TestCase
include ActionView::RecordIdentifier
def setup
@klass = Comment
@record = @klass.new
@singular = 'comment'
@plural = 'comments'
end
def test_dom_id_with_new_record
assert_equal "new_#{@singular}", dom_id(@record)
end
def test_dom_id_with_new_record_and_prefix
assert_equal "custom_prefix_#{@singular}", dom_id(@record, :custom_prefix)
end
def test_dom_id_with_saved_record
@record.save
assert_equal "#{@singular}_1", dom_id(@record)
end
def test_dom_id_with_prefix
@record.save
assert_equal "edit_#{@singular}_1", dom_id(@record, :edit)
end
def test_dom_class
assert_equal @singular, dom_class(@record)
end
def test_dom_class_with_prefix
assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix)
end
def test_dom_id_as_singleton_method
@record.save
assert_equal "#{@singular}_1", ActionView::RecordIdentifier.dom_id(@record)
end
def test_dom_class_as_singleton_method
assert_equal @singular, ActionView::RecordIdentifier.dom_class(@record)
end
end
class RecordIdentifierWithoutActiveModelTest < ActiveSupport::TestCase
include ActionView::RecordIdentifier
def setup
@record = Plane.new
end
def test_dom_id_with_new_record
assert_equal "new_airplane", dom_id(@record)
end
def test_dom_id_with_new_record_and_prefix
assert_equal "custom_prefix_airplane", dom_id(@record, :custom_prefix)
end
def test_dom_id_with_saved_record
@record.save
assert_equal "airplane_1", dom_id(@record)
end
def test_dom_id_with_prefix
@record.save
assert_equal "edit_airplane_1", dom_id(@record, :edit)
end
def test_dom_class
assert_equal 'airplane', dom_class(@record)
end
def test_dom_class_with_prefix
assert_equal "custom_prefix_airplane", dom_class(@record, :custom_prefix)
end
def test_dom_id_as_singleton_method
@record.save
assert_equal "airplane_1", ActionView::RecordIdentifier.dom_id(@record)
end
def test_dom_class_as_singleton_method
assert_equal 'airplane', ActionView::RecordIdentifier.dom_class(@record)
end
end