require 'spec_helper'
class MyFormBuilder
include Datagrid::FormBuilder
end
class MyTemplate
include ActionView::Helpers::FormHelper
end
describe Datagrid::FormBuilder do
let(:template) { ActionView::Base.new}
let(:view) { ActionView::Helpers::FormBuilder.new(:report, _grid, template, {}, Proc.new {|f| })}
describe ".datagrid_filter" do
it "should work for every filter type" do
Datagrid::Filters::FILTER_TYPES.each do |type, klass|
expect(Datagrid::FormBuilder.instance_methods.map(&:to_sym)).to include(klass.form_builder_helper_name)
end
end
subject do
view.datagrid_filter(_filter, _filter_options)
end
let(:_filter_options) { {} }
context "with default filter type" do
let(:_grid) {
test_report do
scope {Entry}
filter(:name)
end
}
let(:_filter) { :name }
it { should equal_to_dom(
''
)}
end
context "with integer filter type" do
let(:_filter) { :group_id }
let(:_grid) {
test_report do
scope {Entry}
filter(:group_id, :integer)
end
}
it { should equal_to_dom(
''
)}
end
context "with date filter type" do
let(:_filter) { :created_at }
let(:_grid) {
test_report do
scope {Entry}
filter(:created_at, :date)
end
}
it { should equal_to_dom(
''
)}
context "when special date format specified" do
around(:each) do |example|
_grid.created_at = Date.parse('2012-01-02')
with_date_format do
example.run
end
end
it { should equal_to_dom(
''
)}
end
end
context "with integer filter type and range option" do
let(:_filter) { :group_id }
let(:_grid) {
test_report(:group_id => _range) do
scope {Entry}
filter(:group_id, :integer, :range => true)
end
}
context "when datagrid_filter options has id" do
let(:_filter_options) { {:id => "hello"} }
let(:_range) { [1,2]}
it { should equal_to_dom(
'' +
' - ' +
''
)}
end
context "with only left bound" do
let(:_range) { [10, nil]}
it { should equal_to_dom(
'' +
' - ' +
''
)}
it { should be_html_safe }
end
context "with only right bound" do
let(:_range) { [nil, 10]}
it { should equal_to_dom(
'' +
' - ' +
''
)}
it { should be_html_safe }
end
context "with invalid range value" do
let(:_range) { 2..1 }
it { should equal_to_dom(
'' +
' - ' +
''
)}
end
context "when custom format translation specified" do
let(:_range) { nil }
around(:each) do |example|
I18n.load_path << File.expand_path('../../support/locale/custom_range_format.yml', __FILE__)
I18n.reload!
example.run
I18n.load_path.pop
I18n.reload!
end
it { should equal_to_dom(
'from to '
)}
end
context "when deprecated separator is specified" do
let(:_range) { nil }
around(:each) do |example|
I18n.load_path << File.expand_path('../../support/locale/deprecated_range_separator.yml', __FILE__)
I18n.reload!
example.run
I18n.load_path.pop
I18n.reload!
end
it { should equal_to_dom(
' | '
)}
end
end
context "with date filter type and range option" do
let(:_filter) { :created_at }
let(:_grid) {
test_report(:created_at => _range) do
scope {Entry}
filter(:created_at, :date, :range => true)
end
}
context "with only left bound" do
let(:_range) { ["2012-01-03", nil]}
it { should equal_to_dom(
'' +
' - ' +
''
)}
it { should be_html_safe }
end
context "when special date format specified" do
around(:each) do |example|
with_date_format do
example.run
end
end
let(:_range) { ["2013/01/01", '2013/02/02']}
it { should equal_to_dom(
'' +
' - ' +
''
)}
end
context "with only right bound" do
let(:_range) { [nil, "2012-01-03"]}
it { should equal_to_dom(
'' +
' - ' +
''
)}
it { should be_html_safe }
end
context "with invalid range value" do
let(:_range) { Date.parse('2012-01-02')..Date.parse('2012-01-01') }
it { should equal_to_dom(
'' +
' - ' +
''
)}
end
context "with blank range value" do
around(:each) do |example|
with_date_format do
example.run
end
end
let(:_range) { [nil, nil] }
it { should equal_to_dom(
'' +
' - ' +
''
)}
end
end
context "with enum filter type" do
let(:_filter) { :category }
let(:_grid) {
test_report do
scope {Entry}
filter(:category, :enum, :select => ["first", "second"])
filter(:category_without_include_blank, :enum, :select => ["first", "second"], :include_blank => false)
filter(:category_with_prompt, :enum, :select => ["first", "second"], :prompt => "My Prompt")
end
}
it { should equal_to_dom(
''
)}
context "when first option is selected" do
before(:each) do
_grid.category = "first"
end
it { should equal_to_dom(
''
)}
end
context "with include_blank option set to false" do
let(:_filter) { :category_without_include_blank }
it { should equal_to_dom(
''
)}
end
context "with prompt option" do
let(:_filter) { :category_with_prompt }
it { should equal_to_dom(
''
)}
end
context "with checkboxes option" do
let(:_filter) { :category_with_prompt }
it { should equal_to_dom(
''
)}
end
context "with checkboxes option" do
let(:_grid) do
test_report do
scope {Entry}
filter(:category, :enum, :select => ["first", "second"], :checkboxes => true)
end
end
let(:_filter) { :category }
if Rails.version >= "4.1"
it { should equal_to_dom(
'
'
)}
else
it { should equal_to_dom(
'
'
)}
end
end
end
context "with eboolean filter type" do
let(:_filter) { :disabled }
let(:_grid) do
test_report do
scope {Entry}
filter(:disabled, :eboolean)
end
end
it { should equal_to_dom(
''
)}
end
context "with string filter" do
let(:_grid) do
test_report do
scope {Entry}
filter(:name, :string)
end
end
let(:_filter) { :name }
it {should equal_to_dom('')}
context "when multiple option is set" do
let(:_grid) do
test_report(:name => "one,two") do
scope {Entry}
filter(:name, :string, :multiple => true)
end
end
let(:_filter) { :name }
it {should equal_to_dom('')}
end
end
context "with non multiple filter" do
let(:_grid) do
test_report do
scope {Entry}
filter(
:name, :enum,
:include_blank => false,
:multiple => false,
:select => []
)
end
end
let(:_filter) { :name }
it {should equal_to_dom('')}
end
context "with float filter type" do
let(:_grid) {
test_report do
scope {Entry}
filter(:group_id, :float)
end
}
let(:_filter) { :group_id }
it { should equal_to_dom(
''
)}
end
context "with enum multiple filter" do
let(:_grid) do
test_report do
scope {Entry}
filter(:group_id, :enum, :select => ['hello'], :multiple => true)
end
end
let(:_filter) { :group_id }
let(:expected_html) do
if ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR < 2
<<-HTML
HTML
else
<<-HTML
HTML
end
end
it { should equal_to_dom(expected_html) }
end
context "with column names filter" do
let(:_grid) do
test_report(:column_names => [:id, :name]) do
scope {Entry}
column_names_filter
column(:id)
column(:name)
column(:category)
end
end
let(:_filter) { :column_names }
let(:expected_html) do
if ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR < 2
<<-HTML
HTML
else
<<-HTML
HTML
end
end
it { should equal_to_dom(expected_html) }
end
context "with column_names_filter default given as symbols" do
let(:_grid) do
test_report() do
scope {Entry}
column_names_filter(:default => [:id, :name], :checkboxes => true)
column(:id)
column(:name)
column(:category)
end
end
let(:_filter) { :column_names }
let(:expected_html) do
<Id
DOM
end
it do
should equal_to_dom(expected_html)
end
end
context "with dynamic filter" do
let(:filter_options) do
{}
end
let(:_grid) do
options = filter_options
test_report do
scope {Entry}
filter(:condition, :dynamic, options)
end
end
let(:_filter) { :condition }
context "with no options" do
let(:expected_html) do
<<-HTML
HTML
end
it {should equal_to_dom(expected_html)}
end
context "when default option passed" do
let(:filter_options) do
{:select => [:id, :name], :default => [:id, '>=', 1]}
end
let(:expected_html) do
<<-HTML
HTML
end
it {should equal_to_dom(expected_html)}
end
context "when select option passed" do
let(:filter_options) do
{:select => [:id, :name]}
end
let(:expected_html) do
<<-HTML
HTML
end
it {should equal_to_dom(expected_html)}
end
end
end
describe ".datagrid_label" do
let(:_grid) do
test_report do
scope {Entry}
filter(:name, :string)
end
end
it "should generate label for filter" do
expect(view.datagrid_label(:name)).to equal_to_dom(
''
)
end
it "should pass options through to the helper" do
expect(view.datagrid_label(:name, :class => 'foo')).to equal_to_dom(
''
)
end
it "should support block" do
expect(view.datagrid_label(:name, :class => 'foo') { 'The Name' }).to equal_to_dom(
''
)
end
it "should support explicit label" do
expect(view.datagrid_label(:name, "The Name")).to equal_to_dom(
''
)
end
end
end