2014-12-19 00:10:53 +00:00
|
|
|
import Parser from 'src/js/parser/javascript.js';
|
|
|
|
import regexpParser from 'src/js/parser/javascript/grammar.peg';
|
|
|
|
import Snap from 'snapsvg';
|
|
|
|
import Q from 'q';
|
|
|
|
|
|
|
|
describe('parser/javascript.js', function() {
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
this.parser = new Parser();
|
|
|
|
});
|
|
|
|
|
2014-12-20 15:47:41 +00:00
|
|
|
describe('#parse', function() {
|
2014-12-19 00:10:53 +00:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
spyOn(regexpParser, 'parse');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('parses the expression', function(done) {
|
|
|
|
this.parser.parse('example expression')
|
|
|
|
.then(() => {
|
|
|
|
expect(regexpParser.parse).toHaveBeenCalledWith('example expression');
|
|
|
|
})
|
2014-12-19 16:51:34 +00:00
|
|
|
.finally(done)
|
|
|
|
.done();
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('replaces newlines with "\\n"', function(done) {
|
|
|
|
this.parser.parse('multiline\nexpression')
|
|
|
|
.then(() => {
|
|
|
|
expect(regexpParser.parse).toHaveBeenCalledWith('multiline\\nexpression');
|
|
|
|
})
|
2014-12-19 16:51:34 +00:00
|
|
|
.finally(done)
|
|
|
|
.done();
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('resolves the returned promise with the parser instance', function(done) {
|
|
|
|
this.parser.parse('example expression')
|
|
|
|
.then(result => {
|
|
|
|
expect(result).toEqual(this.parser);
|
|
|
|
})
|
2014-12-19 16:51:34 +00:00
|
|
|
.finally(done)
|
|
|
|
.done();
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
2014-12-20 15:47:41 +00:00
|
|
|
it('rejects the returned promise with the exception thrown', function(done) {
|
|
|
|
this.parser.parse('/example')
|
|
|
|
.then(null, result => {
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
})
|
|
|
|
.finally(done)
|
|
|
|
.done();
|
|
|
|
});
|
|
|
|
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#render', function() {
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
this.renderPromise = Q.defer();
|
|
|
|
this.parser.parsed = jasmine.createSpyObj('parsed', ['render']);
|
|
|
|
this.parser.parsed.render.and.returnValue(this.renderPromise.promise);
|
|
|
|
|
2014-12-26 20:38:22 +00:00
|
|
|
this.svgBase = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1"></svt>';
|
2014-12-25 04:01:32 +00:00
|
|
|
this.svgContainer = document.createElement('div');
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
2014-12-25 04:01:32 +00:00
|
|
|
it('creates the SVG element', function() {
|
|
|
|
var svg;
|
|
|
|
|
2014-12-26 20:38:22 +00:00
|
|
|
this.parser.render(this.svgContainer, this.svgBase);
|
2014-12-25 04:01:32 +00:00
|
|
|
|
|
|
|
svg = this.svgContainer.querySelector('svg');
|
|
|
|
expect(svg.getAttribute('xmlns')).toEqual('http://www.w3.org/2000/svg');
|
|
|
|
expect(svg.getAttribute('version')).toEqual('1.1');
|
|
|
|
});
|
|
|
|
|
2014-12-19 00:10:53 +00:00
|
|
|
it('render the parsed expression', function() {
|
2014-12-26 20:38:22 +00:00
|
|
|
this.parser.render(this.svgContainer, this.svgBase);
|
2014-12-25 04:01:32 +00:00
|
|
|
expect(this.parser.parsed.render).toHaveBeenCalled();
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when rendering is complete', function() {
|
|
|
|
|
|
|
|
beforeEach(function(done) {
|
|
|
|
this.result = jasmine.createSpyObj('result', ['getBBox', 'transform']);
|
|
|
|
this.result.getBBox.and.returnValue({
|
|
|
|
x: 4,
|
|
|
|
y: 2,
|
|
|
|
width: 42,
|
|
|
|
height: 24
|
|
|
|
});
|
|
|
|
|
2014-12-26 20:38:22 +00:00
|
|
|
this.parser.render(this.svgContainer, this.svgBase);
|
2014-12-19 00:10:53 +00:00
|
|
|
this.renderPromise.resolve(this.result);
|
|
|
|
|
|
|
|
setTimeout(done, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('positions the renderd expression', function() {
|
|
|
|
expect(this.result.transform).toHaveBeenCalledWith(Snap.matrix()
|
|
|
|
.translate(6, 8));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets the dimensions of the image', function() {
|
2014-12-25 04:01:32 +00:00
|
|
|
var svg = this.svgContainer.querySelector('svg');
|
|
|
|
|
|
|
|
expect(svg.getAttribute('width')).toEqual('62');
|
|
|
|
expect(svg.getAttribute('height')).toEqual('44');
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#cancel', function() {
|
|
|
|
|
|
|
|
it('sets the cancelRender state to true', function() {
|
|
|
|
this.parser.cancel();
|
|
|
|
expect(this.parser.state.cancelRender).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-12-22 21:57:30 +00:00
|
|
|
describe('warnings property', function() {
|
|
|
|
|
|
|
|
it('returns the content of the warnings state variable', function() {
|
|
|
|
this.parser.state.warnings.push('example');
|
|
|
|
expect(this.parser.warnings).toEqual(['example']);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|