Permalink
Browse files

added radio_group test and radio_group onChange fix

  • Loading branch information...
1 parent 34875a6 commit 3932f2e61a3db338bb5c303b67032991b781a2d4 @jamesstrate jamesstrate committed Mar 28, 2013
Showing with 103 additions and 3 deletions.
  1. +3 −1 src/js/radio_group.js
  2. +97 −0 test/radio_group.js
  3. +3 −2 test/test.html
View
4 src/js/radio_group.js
@@ -59,10 +59,12 @@
},
_onChange : function(item) {
+ //check if item selected actually changed
+ var changed = this.selectedItem !== item;
this._setSelectedItem(item);
this.render();
- if(_(this.options.onChange).isFunction()) this.options.onChange(item);
+ if(_(this.options.onChange).isFunction() && changed) this.options.onChange(item);
return false;
},
View
97 test/radio_group.js
@@ -0,0 +1,97 @@
+$(document).ready(function() {
+
+ module("Backbone.UI.RadioGroup");
+
+ Backbone.UI.setMobile(false);
+
+ var regions = new Backbone.Collection([{
+ name: 'Americas',
+ notes: 'Bright'
+ }, {
+ name: 'Africa',
+ notes: 'Fruity'
+ }]);
+
+ test("withoutDataBinding", function(){
+
+ var radio = new Backbone.UI.RadioGroup({
+ content: 'test',
+ alternatives: regions,
+ altLabelContent: 'name'
+ }).render();
+
+ $(radio.el).find('li a').eq(1).click();
+
+ ok($(radio.el).find('li a').eq(1).hasClass('selected'));
+
+ });
+
+ test("withDataBinding", function(){
+
+ var coffee = new Backbone.Model({
+ roaster: 'Counter Culture',
+ name: 'Baroida',
+ roastedOn: new Date(2012, 2, 28, 6, 30),
+ acidic: true,
+ region: regions.at(0)
+ });
+
+ var radio = new Backbone.UI.RadioGroup({
+ model : coffee,
+ content: 'region',
+ alternatives: regions,
+ altLabelContent: 'name'
+ }).render();
+
+ //click to select and check model
+ $(radio.el).find('li a').eq(1).click();
+ equal(coffee.get('region').get('name'),'Africa');
+
+ //update model and check if radio button changed
+ coffee.set({
+ region: regions.at(0)
+ });
+ ok($(radio.el).find('li a').eq(0).hasClass('selected'));
+
+ });
+
+ test("onChange", function(){
+ var changeCount = 0;
+
+ var coffee = new Backbone.Model({
+ roaster: 'Counter Culture',
+ name: 'Baroida',
+ roastedOn: new Date(2012, 2, 28, 6, 30),
+ acidic: true,
+ region: regions.at(0)
+ });
+
+ var radio = new Backbone.UI.RadioGroup({
+ model : coffee,
+ content: 'region',
+ alternatives: regions,
+ altLabelContent: 'name',
+ onChange: function() { changeCount++; }
+ }).render();
+
+ //make 2 selections of the same
+ $(radio.el).find('li a').eq(1).click();
+ $(radio.el).find('li a').eq(1).click();
+ $(radio.el).find('li a').eq(1).click();
+
+ //change count should be 1
+ equal(changeCount,1);
+
+ changeCount=0;
+
+ //make 3 different selections
+ $(radio.el).find('li a').eq(0).click();
+ $(radio.el).find('li a').eq(1).click();
+ $(radio.el).find('li a').eq(0).click();
+
+ //change count should be 3
+ equal(changeCount,3);
+
+ });
+
+});
View
5 test/test.html
@@ -41,13 +41,14 @@
<script src="button.js"></script>
<script src="calendar.js"></script>
<script src="checkbox.js"></script>
- <script src="pulldown.js"></script>
<script src="date_picker.js"></script>
<script src="link.js"></script>
<script src="text_area.js"></script>
<script src="text_field.js"></script>
<script src="time_picker.js"></script>
- <script src="menu.js"></script>
+ <script src="menu.js"></script>
+ <script src="pulldown.js"></script>
+ <script src="radio_group.js"></script>
<link rel='stylesheet' type='text/css' href='../src/css/button.css'>
<link rel='stylesheet' type='text/css' href='../src/css/calendar.css'>

0 comments on commit 3932f2e

Please sign in to comment.