Browse Source

fixed #3, select other province should clean children selects.

saberma 10 years ago
parent
commit
17dc9d92d3

+ 5 - 7
app/assets/javascripts/china_city/jquery.china_city.js.coffee

@@ -3,14 +3,12 @@
     @each ->
       selects = $(@).find('.city-select')
       selects.change ->
-        next_select = selects.eq(selects.index(@) + 1)
-        if !$(@).val() # empty all children city
-          $("option:gt(0)", next_select).remove()
-          next_select.change()
-        else if next_select[0] # init next child
+        $this = $(@)
+        next_selects = selects.slice(selects.index(@) + 1) # empty all children city
+        $("option:gt(0)", next_selects).remove()
+        if next_selects.first()[0] and $this.val() # init next child
           $.get "/china_city/#{$(@).val()}", (data) ->
-            $("option:gt(0)", next_select).remove()
-            next_select[0].options.add(new Option(option[0], option[1])) for option in data
+            next_selects.first()[0].options.add(new Option(option[0], option[1])) for option in data
 
   $ ->
     $('.city-group').china_city()

+ 44 - 15
spec/features/china_city_spec.rb

@@ -15,21 +15,50 @@ feature 'china city', js: true do
     end
   end
 
-  scenario 'clean' do
-    visit '/china_city'
-    within '.rails-helper' do
-      select '广东省'
-      select '深圳市'
-      select '南山区'
-      select '--城市--'
-      # save_and_open_page
-      expect(find('.city-district').value).to be_blank
-
-      select '深圳市'
-      select '南山区'
-      select '--省份--'
-      expect(find('.city-city').value).to be_blank
-      expect(find('.city-district').value).to be_blank
+  describe 'clean' do
+    before do
+      visit '/china_city'
+      within '.rails-helper' do
+        select '广东省'
+        select '深圳市'
+        select '南山区'
+      end
+    end
+    context 'select empty parent' do
+      scenario 'city and district' do
+        within '.rails-helper' do
+          select '--省份--'
+          expect(find('.city-city').value).to be_blank
+          expect(find('.city-district').value).to be_blank
+          expect(all('.city-city option').size).to eql 1
+          expect(all('.city-district option').size).to eql 1
+        end
+      end
+      scenario 'district' do
+        within '.rails-helper' do
+          select '--城市--'
+          expect(find('.city-district').value).to be_blank
+          expect(all('.city-district option').size).to eql 1
+        end
+      end
+    end
+    context 'select other parent' do
+      scenario 'city and district' do
+        within '.rails-helper' do
+          select '江苏省'
+          expect(find('.city-city').value).to be_blank
+          expect(find('.city-district').value).to be_blank
+          expect(all('.city-city option').size).to eql 14
+          expect(all('.city-district option').size).to eql 1
+        end
+      end
+      scenario 'district' do
+        within '.rails-helper' do
+          select '广州市'
+          expect(find('.city-district').value).to be_blank
+          expect(all('.city-district option').size).to eql 14
+        end
+      end
     end
   end
 end