Skip to content

Commit

Permalink
Fix how value is set
Browse files Browse the repository at this point in the history
Was setting values of `on` and `off`, even though there was no colon
  • Loading branch information
Ilya Radchenko committed Mar 31, 2015
1 parent 10478da commit 51be04f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/components/x-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ export default Ember.Component.extend({

wasToggled: on('init', observer('toggled', function () {
var toggled = this.get('toggled');
var offState = this.get('off').substr(this.get('off').indexOf(':') + 1) || false;
var onState = this.get('on').substr(this.get('on').indexOf(':') + 1) || true;
var offIndex = this.get('off').indexOf(':');
var onIndex = this.get('on').indexOf(':');
var offState = offIndex > -1 ? this.get('off').substr(offIndex + 1) : false;
var onState = onIndex > -1 ? this.get('on').substr(onIndex + 1) : true;

this.sendAction('toggle', toggled);

Expand All @@ -58,8 +60,10 @@ export default Ember.Component.extend({
valueObserver: on('init', observer('value', function() {
Ember.run.debounce(this, function () {
var value = this.get('value');
var offState = this.get('off').substr(this.get('off').indexOf(':') + 1) || false;
var onState = this.get('on').substr(this.get('on').indexOf(':') + 1) || true;
var offIndex = this.get('off').indexOf(':');
var onIndex = this.get('on').indexOf(':');
var offState = offIndex > -1 ? this.get('off').substr(offIndex + 1) : false;
var onState = onIndex > -1 ? this.get('on').substr(onIndex + 1) : true;

if (value === onState) {
this.set('toggled', true);
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/components/x-toggle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
moduleForComponent,
test
} from 'ember-qunit';
import Ember from 'ember';

moduleForComponent('x-toggle', {
// specify the other units that are required for this test
Expand All @@ -19,3 +20,23 @@ test('it renders', function(assert) {
this.render();
assert.equal(component._state, 'inDOM');
});

test('changing value changes state', function (assert) {
assert.expect(3);

var component = this.subject();

assert.equal(this.$('input.x-toggle').attr('checked'), false, 'unchecked by default');

Ember.run(function () {
component.set('value', true);
});

assert.equal(this.$('input.x-toggle').attr('checked'), true, 'checked when value: true');

Ember.run(function () {
component.set('value', false);
});

assert.equal(this.$('input.x-toggle').attr('checked'), false, 'unchecked when value: false');
});

0 comments on commit 51be04f

Please sign in to comment.