From 8a9a12985673775de94d2f043cbe03a95f4ee38e Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sat, 20 Dec 2014 10:18:00 -0500 Subject: [PATCH] Adding tests for CharsetRange nodes --- spec/parser/javascript/charset_range_spec.js | 60 ++++++++++++++++++++ src/js/parser/javascript/charset_range.js | 13 +++-- src/js/util.js | 7 +++ 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 spec/parser/javascript/charset_range_spec.js diff --git a/spec/parser/javascript/charset_range_spec.js b/spec/parser/javascript/charset_range_spec.js new file mode 100644 index 0000000..40420df --- /dev/null +++ b/spec/parser/javascript/charset_range_spec.js @@ -0,0 +1,60 @@ +import javascript from 'src/js/parser/javascript/parser.js'; +import util from 'src/js/util.js'; +import Q from 'q'; + +describe('parser/javascript/charset_range.js', function() { + + it('parses "a-z" as a CharsetRange', function() { + var parser = new javascript.Parser('a-z'); + expect(parser.__consume__charset_range()).toEqual(jasmine.objectContaining({ + type: 'charset-range', + first: jasmine.objectContaining({ + textValue: 'a' + }), + last: jasmine.objectContaining({ + textValue: 'z' + }) + })); + }); + + describe('#_render', function() { + + beforeEach(function() { + var parser = new javascript.Parser('a-z'); + this.node = parser.__consume__charset_range(); + + this.node.container = jasmine.createSpyObj('cotnainer', ['addClass', 'text', 'group']); + this.node.container.text.and.returnValue('hyphen'); + + this.firstDeferred = Q.defer(); + this.lastDeferred = Q.defer(); + + spyOn(this.node.first, 'render').and.returnValue(this.firstDeferred.promise); + spyOn(this.node.last, 'render').and.returnValue(this.lastDeferred.promise); + spyOn(util, 'spaceHorizontally'); + }); + + it('renders a hyphen', function() { + this.node._render(); + expect(this.node.container.text).toHaveBeenCalledWith(0, 0, '-'); + }); + + it('spaces the items horizontally', function(done) { + this.firstDeferred.resolve(); + this.lastDeferred.resolve(); + + this.node._render() + .then(() => { + expect(util.spaceHorizontally).toHaveBeenCalledWith([ + this.node.first, + 'hyphen', + this.node.last + ], { padding: 5 }); + }) + .finally(done) + .done(); + }); + + }); + +}); diff --git a/src/js/parser/javascript/charset_range.js b/src/js/parser/javascript/charset_range.js index bb08ea6..2b69d1b 100644 --- a/src/js/parser/javascript/charset_range.js +++ b/src/js/parser/javascript/charset_range.js @@ -1,4 +1,4 @@ -import { spaceHorizontally } from '../../util.js'; +import util from '../../util.js'; import _ from 'lodash'; import Q from 'q'; @@ -8,8 +8,7 @@ export default { _render() { var contents = [ this.first, - this.container.text() - .attr({ text: '-' }), + this.container.text(0, 0, '-'), this.last ]; @@ -17,9 +16,11 @@ export default { this.first.render(this.container.group()), this.last.render(this.container.group()) ]) - .then(spaceHorizontally.bind(this, contents, { - padding: 5 - })); + .then(() => { + util.spaceHorizontally(contents, { + padding: 5 + }); + }); }, setup() { diff --git a/src/js/util.js b/src/js/util.js index 7996264..fa1dea4 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -70,3 +70,10 @@ export function spaceVertically(items, options) { .translate(horizontalCenter - item.getBBox().cx, 0)); } } + +export default { + customEvent, + normalizeBBox, + spaceHorizontally, + spaceVertically +};