-
-
Save andrewk/c9a3a431624e910d9985 to your computer and use it in GitHub Desktop.
Count Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Interface for a count, affected by remote addition or subtraction | |
* event should send data with 'property' property, set to eg: -1 to lower by one | |
*/ | |
define(function(require) { | |
'use strict'; | |
var defineComponent = require('flight/lib/component'); | |
function Count() { | |
this.defaultAttrs({ | |
'event': null | |
}); | |
this.after('initialize', function() { | |
this.on(document, this.attr.event, this.update.bind(this)); | |
}); | |
this.update = function(event, data) { | |
this.$node.text( | |
parseInt(this.$node.text(), 10) + data.modifier | |
); | |
} | |
} | |
return defineComponent(Count); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describeComponent('component_ui/count', function () { | |
beforeEach(function () { | |
setupComponent('<span>8</span>', {'event': 'uiChangeCount'}); | |
}); | |
describe('receiving event', function() { | |
it('applies a positive modifier', function() { | |
$(document).trigger('uiChangeCount', {'modifier': 3}); | |
expect(this.component.$node).toHaveText('11'); | |
}); | |
it('applies a negative modifier', function() { | |
$(document).trigger('uiChangeCount', {'modifier': -1}); | |
expect(this.component.$node).toHaveText('7'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment