#encoding: UTF-8
require 'spec_helper'

describe AttributeFieldsHelper do
  describe "#attribute_field" do
    context "In the HTML,there should always be 'value' tag in it's name"

    before(:all) do
      @user = User.where(email:'chris@rulingcom.com').first
    end #end before
    # binding.pry
    LIST[:markups].each do |markup|
        it "should have 'value' when it's #{markup[0]} (no locale)" do
          title = "標題"
          pre_set_markup_value = case markup[0]
          when "select","checkbox","radio_button"
            '{"1"=>{"en"=>"Male", "zh_tw"=>"男性"}, "2"=>{"en"=>"Female", "zh_tw"=>"女性"}, "3"=>{"en"=>"Not public", "zh_tw"=>"不公開"}}'
          else
            nil
          end

          attribute_field=AttributeField.find_or_create_by(key:'Test',markup: markup[0],markup_value: pre_set_markup_value,:title=>title)

          html = attribute_field.block_helper(@user,1)

          nodes = Nokogiri::HTML.fragment(html).search('*[@func="input_unit"]')

          nodes.each do |input|
            attri = input.attributes["name"] 
            attri = input.children().first.attributes["name"] if (markup[0] == 'checkbox'  ||  markup[0] == 'radio_button' )
            attri.to_s.should match /user\[.*attribute_values\]\[\d*\]\[value\].*/
            # user[attribute_values][1][id]
            # user[attribute_values][1][value][en]
          end #end of input loop
        end # end of it
    end #end markups

        LIST[:markups].each do |markup|
          if markup[1]["muti_lang_input_supprt"]
            [true,false].each do |locale_sat|
              title = locale_sat ? {"en"=>"Title", "zh_tw"=>"標題"} : "標題" 
              it "should have 'value' when it's #{markup[0]} for locale => #{locale_sat}" do
                pre_set_markup_value = case markup[0]
                when "select","checkbox","radio_button"
                  '{"1"=>{"en"=>"Male", "zh_tw"=>"男性"}, "2"=>{"en"=>"Female", "zh_tw"=>"女性"}, "3"=>{"en"=>"Not public", "zh_tw"=>"不公開"}}'
                else
                  nil
                end
                if (markup[0] == "text_field" || markup[0] == "text_area") && (locale_sat == false)
                  # binding.pry 
                end
                attribute_field=AttributeField.find_or_create_by(key:'Test',markup: markup[0],markup_value: pre_set_markup_value,:title=>title,:locale => locale_sat)
                
                html = attribute_field.block_helper(@user,1)
                
                # p html
                nodes = Nokogiri::HTML.fragment(html).search('*[@func="input_unit"]')
                nodes.each do |input|
                  input.attributes["name"].to_s.should match /user\[.*attribute_values\]\[\d*\]\[value\].*/
                  # user[attribute_values][1][id]
                  # user[attribute_values][1][value][en]
                end #end of input loop
                nodes = Nokogiri::HTML.fragment(html).search('*[@func="field_label"]')

                nodes.each do |input|
                  # p input.to_s
                  # binding.pry if 

                  input.children.text.should match "標題"
                end # end of field_label loop
              end # end of it
            end # end of T/F
          end# end of if support
    end #end markups


  end
end