<commit_before># Copyright (C) 2007, 2008, 2009, 2010, 2011 The Collaborative Software Foundation
#
# This file is part of TriSano.
#
# TriSano is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# TriSano is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TriSano. If not, see http://www.gnu.org/licenses/agpl-3.0.txt.

require File.dirname(__FILE__) + '/../spec_helper'


def configure_request
  request = mock('Object')
  request.stubs(:xhr?).returns(@xhr_request)
  @template = Object.new
  @template.extend(ApplicationHelper)
  @template.stubs(:request).returns(request)
end

def configure_radio_buttons
  @form_builder = ExtendedFormBuilder.new(nil, nil, @template, nil, nil)
  @radio_buttons = [{:id => 1, :export_conversion_value_id => 200},
                    {:id => 2, :export_conversion_value_id => 201}]
  @result = @form_builder.send(:rb_export_js, @radio_buttons, 'test_id')
end

def configure_drop_downs
  @form_builder = ExtendedFormBuilder.new(nil, nil, @template, nil, nil)
  @select_options = [{:value => 1, :export_conversion_value_id => 200},
                     {:value => 2, :export_conversion_value_id => 201}]

  @result = @form_builder.send(:dd_export_js, @select_options, 'hidden_id', 'select_id')
end

def shared_tests
  it "#{@xhr_request ? 'should not' : 'should'} delay execution until the dom loads" do
    @result.send(@xhr_request ? :should_not : :should) =~ /document.observe\('dom:loaded', function\(\) \{.*\}/m
  end

  it 'should wrap script in a script tag' do
    @result.should =~ /<script type="text\/javascript">.*<\/script>/m
  end

end

def radio_button_tests
  it 'should produce an observer for each radio button' do
    @result.should =~ /\$\('1'\).observe\('click', function\(\) \{/
    @result.should =~ /\$\('2'\).observe\('click', function\(\) \{/
  end

  it 'should write the conversion value to the "id" field for each observer' do
    @result.should =~ /\$\('test_id'\).writeAttribute\('value', '200'\)/
    @result.should =~ /\$\('test_id'\).writeAttribute\('value', '201'\)/
  end
end

def drop_down_tests
  it 'should observe the select field' do
    @result.should =~ /\$\('select_id'\).observe\('change', function\(\) \{/
  end

  it 'should write to option export values to the hidden field' do
    @result.should =~ /if \(this.value == '1'\) \{ \$\('hidden_id'\).writeAttribute\('value', '200'\) \}/
    @result.should =~ /if \(this.value == '2'\) \{ \$\('hidden_id'\).writeAttribute\('value', '201'\) \}/
  end
end


describe ExtendedFormBuilder, 'radio button export js' do

  describe 'during an html request' do

    before(:each) do
      @xhr_request = false
      configure_request
      configure_radio_buttons
    end

    shared_tests

    radio_button_tests

  end
end

describe ExtendedFormBuilder, 'drop down export js' do

  describe 'during an html request' do

    before(:each) do
      @xhr_request = false
      configure_request
      configure_drop_downs
    end

    shared_tests

    drop_down_tests

  end

end

describe ExtendedFormBuilder, 'radio button export js' do

  describe 'during an ajax request' do

    before(:each) do
      @xhr_request = true
      configure_request
      configure_radio_buttons
    end

    shared_tests

    radio_button_tests

  end
end

describe ExtendedFormBuilder, 'drop down export js' do

  describe 'during an html request' do

    before(:each) do
      @xhr_request = true
      configure_request
      configure_drop_downs
    end

    shared_tests

    drop_down_tests

  end

end

describe ExtendedFormBuilder, "rendering a dynamic question" do

  describe 'with a drop down select' do

    it 'should not raise an error with a nil select option value' do
      configure_request
      # don't care about rendering.
      @template.stubs(:select).returns('')
      @template.stubs(:hidden_field_tag).returns('')
      @template.stubs(:content_tag).returns('')
      object = mock('answer') do
        stubs(:id).returns(1)
        stubs(:question_id).returns(1)
        stubs(:new_record?).returns(true)
        stubs(:text_answer).returns('some answer')
      end
      question = mock('question') do
        stubs(:id).returns(1)
        stubs(:is_multi_valued?).returns(false)
        stubs(:data_type).returns(:drop_down)
        stubs(:question_text).returns("This is a question?")
      end
      question_element = mock('question_element') do
        stubs(:question).returns(question)
        stubs(:export_column).returns(nil)
	stubs(:is_required?).returns(false)
      end
      form_elements_cache = mock('form_elements_cache') do
        stubs(:children).returns([Object.new])
        stubs(:children_by_type).returns([])
      end
      select_options = [{:value => nil, :export_conversion_value_id => nil},
                        {:value => 1,   :export_conversion_value_id => 200},
                        {:value => 2,   :export_conversion_value_id => 201}]
      form_builder = ExtendedFormBuilder.new('object_name', object, @template, nil, nil)
      form_builder.stubs(:hidden_field).returns('')
      form_builder.stubs(:get_values).returns(select_options)
      form_builder.stubs(:follow_up_spinner_for).returns('')
      lambda do
        form_builder.send(:dynamic_question,
                          form_elements_cache,
                          question_element,
                          nil,
                          nil)
      end.should_not raise_error
    end
  end
end

describe ExtendedFormBuilder, "returning a core field" do
  before do
    configure_request
    @form_builder = ExtendedFormBuilder.new('morbidity_event', nil, @template, {}, nil)
    @core_field = Factory.create(:cmr_core_field,
                                 :key => 'morbidity_event[test_attribute]')
  end

  it "should return core field" do
    @form_builder.core_field(:test_attribute).should == @core_field
  end

  it "should return a sentinal core field if core field doesn't exist" do
    @form_builder.core_field(:bogus_attribute).should_not be_nil
  end<commit_msg>Fix form builder spec to handle new use of event<commit_after># Copyright (C) 2007, 2008, 2009, 2010, 2011 The Collaborative Software Foundation
#
# This file is part of TriSano.
#
# TriSano is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# TriSano is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TriSano. If not, see http://www.gnu.org/licenses/agpl-3.0.txt.

require File.dirname(__FILE__) + '/../spec_helper'


def configure_request
  request = mock('Object')
  request.stubs(:xhr?).returns(@xhr_request)
  @template = Object.new
  @template.extend(ApplicationHelper)
  @template.stubs(:request).returns(request)
end

def configure_radio_buttons
  @form_builder = ExtendedFormBuilder.new(nil, nil, @template, nil, nil)
  @radio_buttons = [{:id => 1, :export_conversion_value_id => 200},
                    {:id => 2, :export_conversion_value_id => 201}]
  @result = @form_builder.send(:rb_export_js, @radio_buttons, 'test_id')
end

def configure_drop_downs
  @form_builder = ExtendedFormBuilder.new(nil, nil, @template, nil, nil)
  @select_options = [{:value => 1, :export_conversion_value_id => 200},
                     {:value => 2, :export_conversion_value_id => 201}]

  @result = @form_builder.send(:dd_export_js, @select_options, 'hidden_id', 'select_id')
end

def shared_tests
  it "#{@xhr_request ? 'should not' : 'should'} delay execution until the dom loads" do
    @result.send(@xhr_request ? :should_not : :should) =~ /document.observe\('dom:loaded', function\(\) \{.*\}/m
  end

  it 'should wrap script in a script tag' do
    @result.should =~ /<script type="text\/javascript">.*<\/script>/m
  end

end

def radio_button_tests
  it 'should produce an observer for each radio button' do
    @result.should =~ /\$\('1'\).observe\('click', function\(\) \{/
    @result.should =~ /\$\('2'\).observe\('click', function\(\) \{/
  end

  it 'should write the conversion value to the "id" field for each observer' do
    @result.should =~ /\$\('test_id'\).writeAttribute\('value', '200'\)/
    @result.should =~ /\$\('test_id'\).writeAttribute\('value', '201'\)/
  end
end

def drop_down_tests
  it 'should observe the select field' do
    @result.should =~ /\$\('select_id'\).observe\('change', function\(\) \{/
  end

  it 'should write to option export values to the hidden field' do
    @result.should =~ /if \(this.value == '1'\) \{ \$\('hidden_id'\).writeAttribute\('value', '200'\) \}/
    @result.should =~ /if \(this.value == '2'\) \{ \$\('hidden_id'\).writeAttribute\('value', '201'\) \}/
  end
end


describe ExtendedFormBuilder, 'radio button export js' do

  describe 'during an html request' do

    before(:each) do
      @xhr_request = false
      configure_request
      configure_radio_buttons
    end

    shared_tests

    radio_button_tests

  end
end

describe ExtendedFormBuilder, 'drop down export js' do

  describe 'during an html request' do

    before(:each) do
      @xhr_request = false
      configure_request
      configure_drop_downs
    end

    shared_tests

    drop_down_tests

  end

end

describe ExtendedFormBuilder, 'radio button export js' do

  describe 'during an ajax request' do

    before(:each) do
      @xhr_request = true
      configure_request
      configure_radio_buttons
    end

    shared_tests

    radio_button_tests

  end
end

describe ExtendedFormBuilder, 'drop down export js' do

  describe 'during an html request' do

    before(:each) do
      @xhr_request = true
      configure_request
      configure_drop_downs
    end

    shared_tests

    drop_down_tests

  end

end

describe ExtendedFormBuilder, "rendering a dynamic question" do

  describe 'with a drop down select' do

    it 'should not raise an error with a nil select option value' do
      configure_request
      # don't care about rendering.
      @template.stubs(:select).returns('')
      @template.stubs(:hidden_field_tag).returns('')
      @template.stubs(:content_tag).returns('')
      object = mock('answer') do
        stubs(:id).returns(1)
        stubs(:question_id).returns(1)
        stubs(:new_record?).returns(true)
        stubs(:text_answer).returns('some answer')
      end
      question = mock('question') do
        stubs(:id).returns(1)
        stubs(:is_multi_valued?).returns(false)
        stubs(:data_type).returns(:drop_down)
        stubs(:question_text).returns("This is a question?")
      end
      question_element = mock('question_element') do
        stubs(:question).returns(question)
        stubs(:export_column).returns(nil)
      	stubs(:is_required?).returns(false)
      end
      event = mock('event') do
        stubs(:id).returns(1)
      end
      form_elements_cache = mock('form_elements_cache') do
        stubs(:children).returns([Object.new])
        stubs(:children_by_type).returns([])
      end
      select_options = [{:value => nil, :export_conversion_value_id => nil},
                        {:value => 1,   :export_conversion_value_id => 200},
                        {:value => 2,   :export_conversion_value_id => 201}]
      form_builder = ExtendedFormBuilder.new('object_name', object, @template, nil, nil)
      form_builder.stubs(:hidden_field).returns('')
      form_builder.stubs(:get_values).returns(select_options)
      form_builder.stubs(:follow_up_spinner_for).returns('')
      lambda do
        form_builder.send(:dynamic_question,
                          form_elements_cache,
                          question_element,
                          event,
                          nil)
      end.should_not raise_error
    end
  end
end

describe ExtendedFormBuilder, "returning a core field" do
  before do
    configure_request
    @form_builder = ExtendedFormBuilder.new('morbidity_event', nil, @template, {}, nil)
    @core_field = Factory.create(:cmr_core_field,
                                 :key => 'morbidity_event[test_attribute]')
  end

  it "should return core field" do
    @form_builder.core_field(:test_attribute).should == @core_field
  end

  it "should return a sentinal core field if core field doesn't exist" do
    @form_builder.core_field(:bogus_attribute).should_not be_nil
  end
{'fraction_non_alphanumeric': 0.11932426234520715, 'fraction_numerical': 0.010166641186362941, 'mean_word_length': 3.157292659675882, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5038281', 'n_tokens_mistral': 4148, 'n_tokens_neox': 3887, 'n_words': 1079}