Adding tests for CharsetRange nodes

This commit is contained in:
Jeff Avallone 2014-12-20 10:18:00 -05:00
parent d27831a265
commit 8a9a129856
3 changed files with 74 additions and 6 deletions

View File

@ -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();
});
});
});

View File

@ -1,4 +1,4 @@
import { spaceHorizontally } from '../../util.js'; import util from '../../util.js';
import _ from 'lodash'; import _ from 'lodash';
import Q from 'q'; import Q from 'q';
@ -8,8 +8,7 @@ export default {
_render() { _render() {
var contents = [ var contents = [
this.first, this.first,
this.container.text() this.container.text(0, 0, '-'),
.attr({ text: '-' }),
this.last this.last
]; ];
@ -17,9 +16,11 @@ export default {
this.first.render(this.container.group()), this.first.render(this.container.group()),
this.last.render(this.container.group()) this.last.render(this.container.group())
]) ])
.then(spaceHorizontally.bind(this, contents, { .then(() => {
util.spaceHorizontally(contents, {
padding: 5 padding: 5
})); });
});
}, },
setup() { setup() {

View File

@ -70,3 +70,10 @@ export function spaceVertically(items, options) {
.translate(horizontalCenter - item.getBBox().cx, 0)); .translate(horizontalCenter - item.getBBox().cx, 0));
} }
} }
export default {
customEvent,
normalizeBBox,
spaceHorizontally,
spaceVertically
};