cirandas.net

ref: master

test/functional/maps_controller_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require_relative '../test_helper'

class MapsControllerTest < ActionController::TestCase

  def setup
    @controller = MapsController.new

    @profile = create_user('test_profile').person
    @env = Environment.default
    login_as(@profile.identifier)
  end

  attr_reader :profile, :env

  should 'save profile address' do
    post :edit_location, :profile => profile.identifier, :profile_data => { 'address' => 'new address' }
    assert_equal 'new address', Profile['test_profile'].address
  end

  should 'save profile national_region_code' do

    national_region_code = '355030'
    city = 'Santo Afonso'
    state = 'Mato Grosso'

    fast_create(NationalRegion, :name => 'Brasil',
      :national_region_code => 'BR',
      :national_region_type_id => NationalRegionType::COUNTRY)

    parent_region = fast_create(NationalRegion, :name => state,
                                :national_region_code => '35',
                                :national_region_type_id => NationalRegionType::STATE)

    fast_create(NationalRegion, :name =>  city,
                                :national_region_code => national_region_code,
                                :national_region_type_id => NationalRegionType::CITY,
                                :parent_national_region_code => parent_region.national_region_code)

    post :edit_location, :profile => profile.identifier, :profile_data => {
      :address => 'new address',
      :country => 'BR',
      :city => city,
      :state => state
    }

    assert_equal national_region_code, Profile['test_profile'].national_region_code
  end

  should 'back when update address fail' do
    Profile.any_instance.stubs(:update!).returns(false)
    post :edit_location, :profile => profile.identifier, :profile_data => { 'address' => 'new address' }
    assert_nil profile.address
    assert_template 'edit_location'
  end

  should 'show page to edit location' do
    get :edit_location, :profile => profile.identifier
    assert_response :success
    assert_template 'edit_location'
  end

  should 'dispĺay form for address with profile address' do
    env.custom_person_fields = { 'location' => { 'active' => 'true' } }
    env.save!

    get :edit_location, :profile => profile.identifier
    assert_tag :tag => 'input', :attributes => { :name => 'profile_data[city]' }
  end

  should 'display only region categories for selection' do
    fast_create(Region, name: 'Region')
    fast_create(State, name: 'City', environment_id: env.id)
    fast_create(City, name: 'State', environment_id: env.id)
    fast_create(Category, name: 'Not a Region')

    get :edit_location, profile: profile.identifier
    assert_tag :tag => 'a', :attributes => { :class => 'select-subcategory-link' }, :content => 'Region'
    assert_tag :tag => 'a', :attributes => { :class => 'select-subcategory-link' }, :content => 'City'
    assert_tag :tag => 'a', :attributes => { :class => 'select-subcategory-link' }, :content => 'State'
    assert_no_tag :tag => 'a', :attributes => { :class => 'select-subcategory-link' }, :content => 'Not a Region'
  end

  should 'display only regions in the selected categories list' do
    region = fast_create(Region, name: 'Region')
    category = fast_create(Category, name: 'Not a Region')
    profile.update_attributes(category_ids: [region.id, category.id])

    get :edit_location, profile: profile.identifier
    assert_tag :tag => 'td', :content => region.name, :ancestor => { :tag => 'table', :attributes => { :id => 'selected-categories'}}
    assert_no_tag :tag => 'td', :content => category.name, :ancestor => { :tag => 'table', :attributes => { :id => 'selected-categories'}}
  end

  should 'autocomplete search_city' do

    city = 'Santo Afonso'
    state = 'Mato Grosso'

    parent_region = fast_create(NationalRegion, :name => state,
                                :national_region_code => '35',
                                :national_region_type_id => NationalRegionType::STATE)

    fast_create(NationalRegion, :name =>  city,
                                :national_region_code => '355030',
                                :national_region_type_id => NationalRegionType::CITY,
                                :parent_national_region_code => parent_region.national_region_code)

    get :search_city, :term => "San", :profile => profile.identifier

    json_response = ActiveSupport::JSON.decode(@response.body)

    label =  json_response[0]['label']
    category =  json_response[0]['category']

    assert_equal city, label
    assert_equal state, category
  end

  should 'autocomplete search_state' do

    state = 'Mato Grosso do Sul'


    fast_create(NationalRegion, :name => state,
                                :national_region_code => '36',
                                :national_region_type_id => NationalRegionType::STATE)


    get :search_state, :term => "Mat", :profile => profile.identifier

    json_response = ActiveSupport::JSON.decode(@response.body)

    label =  json_response[0]['label']

    assert_equal state, label
  end

  should 'display location fields along with the map' do
    Environment.any_instance.stubs(:custom_person_fields).returns({ 'location' => { 'active' => 'true' } })
    get :edit_location, :profile => profile.identifier

    assert_tag 'input', attributes: { id: 'profile_data_state' }
    assert_tag 'input', attributes: { id: 'profile_data_city' }
  end

  should 'accept blank address with lat and lng' do
    Environment.any_instance.stubs(:custom_person_fields).returns({ 'location' => { 'active' => 'true' } })
    post :edit_location, profile: profile.identifier, profile_data: { state: '', city: '', lat: 30, lng: 15 }

    profile.reload
    assert_equal 30, profile.lat
    assert_equal 15, profile.lng
  end
end