require 'mocha/test_unit' require File.dirname(File.expand_path(__FILE__)) + '/test_helper' class MockTemplate include ActionView::Helpers::FormHelper include ActionView::Helpers::FormOptionsHelper attr_accessor :output_buffer def surround(start_html, end_html, &block) "#{start_html}#{block.call}#{end_html}" end end class GovUkDateFieldsTest < ActiveSupport::TestCase def setup @employee = Employee.new(dob: Date.new(1963, 12, 7), joined: Date.new(2015, 4, 1)) @template = MockTemplate.new @form_builder = ActionView::Helpers::FormBuilder.new(:employee, @employee, @template, {} ) end test "error raised if invalid options given" do err = assert_raise do GovUkDateFields::FormFields.new(@form_builder, :employee, :dob, rubbish: 'some_value') end end test "basic_output_without_fieldset" do date_fields = GovUkDateFields::FormFields.new(@form_builder, :employee, :dob) assert_html_equal(date_fields.raw_output, expected_basic_output_without_fieldset) end test 'placeholder_output_without_fieldset' do date_fields = GovUkDateFields::FormFields.new(@form_builder, :employee, :dob, {placeholders: { day: 'DAY', month: 'MTH', year: 'YEAR' } }) assert_html_equal(date_fields.raw_output, expected_placeholder_output_without_fieldset) end test 'fieldset_output_with_form_hint' do date_fields = GovUkDateFields::FormFields.new(@form_builder, :employee, :dob, {legend_text: 'Date of birth', legend_class: 'govuk_legend_class', form_hint_text: 'In the form: dd mm yyyy'}) assert_html_equal(date_fields.raw_output, expected_fieldset_output_with_form_hint) end test 'fieldset output with legend class' do date_fields = GovUkDateFields::FormFields.new(@form_builder, :employee, :joined, {legend_text: 'Joining date', legend_class: 'date-legend-class', form_hint_text: 'For example, 31 3 1980'}) assert_html_equal(date_fields.raw_output, expected_fieldset_output_with_legend_class) end test 'fieldset with id' do date_fields = GovUkDateFields::FormFields.new(@form_builder, :employee, :joined, {legend_text: 'Joining date', id: 'employee_date_joined'}) assert_html_equal(date_fields.raw_output, expected_fieldset_output_with_id) end test 'fieldset with error_class and message' do @employee.errors[:joined] << "Invalid joining date" @employee.errors[:joined] << "Joining date must be in the past" date_fields = GovUkDateFields::FormFields.new(@form_builder, :employee, :joined, {legend_text: 'Joining date', id: 'employee_date_joined'}) assert_html_equal(date_fields.raw_output, expected_fieldset_output_with_error_class_and_message) end test 'fieldset with error_class and supplied error messages' do @employee.errors[:joined] << "invalid" @employee.errors[:joined] << "must_be_in_past" date_fields = GovUkDateFields::FormFields.new(@form_builder, :employee, :joined, {legend_text: 'Joining date', id: 'employee_date_joined', error_messages: ['Invalid joining date', 'Joining date must be in the past']}) assert_html_equal(date_fields.raw_output, expected_fieldset_output_with_error_class_and_message) end test "squash_html" do html = " This is some text \n