Skip to content

Instantly share code, notes, and snippets.

@andrewk andrewk/count.js Secret

Created Jul 23, 2014
Embed
What would you like to do?
Count Component
/**
* 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);
});
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
You can’t perform that action at this time.