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';
|
|
|
|
|
|
|
|
describe('parser/javascript.js', function() {
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-12-30 02:31:36 +00:00
|
|
|
this.container = document.createElement('div');
|
|
|
|
this.parser = new Parser(this.container);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('container property', function() {
|
|
|
|
|
|
|
|
it('sets the content of the element', function() {
|
|
|
|
var element = document.createElement('div');
|
|
|
|
this.parser.container = element;
|
|
|
|
|
|
|
|
expect(element.innerHTML).not.toEqual('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('keeps the original content if the keepContent option is set', function() {
|
|
|
|
var element = document.createElement('div');
|
|
|
|
element.innerHTML = 'example content';
|
|
|
|
|
|
|
|
this.parser.options.keepContent = true;
|
|
|
|
this.parser.container = element;
|
|
|
|
|
|
|
|
expect(element.innerHTML).toContain('example content');
|
|
|
|
expect(element.innerHTML).not.toEqual('example content');
|
|
|
|
});
|
|
|
|
|
2014-12-30 15:17:55 +00:00
|
|
|
it('adds the "svg-container" class', function() {
|
|
|
|
spyOn(this.parser, '_addClass');
|
|
|
|
this.parser.container = document.createElement('div');
|
|
|
|
expect(this.parser._addClass).toHaveBeenCalledWith('svg-container');
|
|
|
|
});
|
|
|
|
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
2014-12-20 15:47:41 +00:00
|
|
|
describe('#parse', function() {
|
2014-12-19 00:10:53 +00:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
spyOn(regexpParser, 'parse');
|
|
|
|
});
|
|
|
|
|
2014-12-30 02:31:36 +00:00
|
|
|
it('adds the "loading" class', function() {
|
|
|
|
spyOn(this.parser, '_addClass');
|
|
|
|
this.parser.parse('example expression');
|
|
|
|
expect(this.parser._addClass).toHaveBeenCalledWith('loading');
|
|
|
|
});
|
|
|
|
|
2014-12-19 00:10:53 +00:00
|
|
|
it('parses the expression', function(done) {
|
|
|
|
this.parser.parse('example expression')
|
|
|
|
.then(() => {
|
|
|
|
expect(regexpParser.parse).toHaveBeenCalledWith('example expression');
|
2015-03-14 21:11:14 +00:00
|
|
|
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');
|
2015-03-14 21:11:14 +00:00
|
|
|
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);
|
2015-03-14 21:11:14 +00:00
|
|
|
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) {
|
2015-03-14 21:11:14 +00:00
|
|
|
regexpParser.parse.and.throwError('fail');
|
|
|
|
this.parser.parse('(example')
|
2014-12-20 15:47:41 +00:00
|
|
|
.then(null, result => {
|
|
|
|
expect(result).toBeDefined();
|
2015-03-14 21:11:14 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-12-20 15:47:41 +00:00
|
|
|
});
|
|
|
|
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#render', function() {
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2015-03-14 21:11:14 +00:00
|
|
|
this.renderPromise = this.testablePromise();
|
2014-12-19 00:10:53 +00:00
|
|
|
this.parser.parsed = jasmine.createSpyObj('parsed', ['render']);
|
|
|
|
this.parser.parsed.render.and.returnValue(this.renderPromise.promise);
|
2014-12-25 04:01:32 +00:00
|
|
|
});
|
|
|
|
|
2014-12-19 00:10:53 +00:00
|
|
|
it('render the parsed expression', function() {
|
2014-12-30 02:31:36 +00:00
|
|
|
this.parser.render();
|
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() {
|
|
|
|
|
2014-12-27 20:57:00 +00:00
|
|
|
beforeEach(function() {
|
2014-12-19 00:10:53 +00:00
|
|
|
this.result = jasmine.createSpyObj('result', ['getBBox', 'transform']);
|
|
|
|
this.result.getBBox.and.returnValue({
|
|
|
|
x: 4,
|
|
|
|
y: 2,
|
|
|
|
width: 42,
|
|
|
|
height: 24
|
|
|
|
});
|
|
|
|
|
|
|
|
this.renderPromise.resolve(this.result);
|
|
|
|
});
|
|
|
|
|
2014-12-27 20:57:00 +00:00
|
|
|
it('positions the renderd expression', function(done) {
|
2014-12-30 02:31:36 +00:00
|
|
|
this.parser.render()
|
2014-12-27 20:57:00 +00:00
|
|
|
.then(() => {
|
|
|
|
expect(this.result.transform).toHaveBeenCalledWith(Snap.matrix()
|
|
|
|
.translate(6, 8));
|
2015-03-14 21:11:14 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
2014-12-27 20:57:00 +00:00
|
|
|
it('sets the dimensions of the image', function(done) {
|
2014-12-30 02:31:36 +00:00
|
|
|
this.parser.render()
|
2014-12-27 20:57:00 +00:00
|
|
|
.then(() => {
|
2014-12-30 02:31:36 +00:00
|
|
|
var svg = this.container.querySelector('svg');
|
2014-12-25 04:01:32 +00:00
|
|
|
|
2014-12-27 20:57:00 +00:00
|
|
|
expect(svg.getAttribute('width')).toEqual('62');
|
|
|
|
expect(svg.getAttribute('height')).toEqual('44');
|
2015-03-14 21:11:14 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-12-19 00:10:53 +00:00
|
|
|
});
|
|
|
|
|
2014-12-30 02:31:36 +00:00
|
|
|
it('removes the "loading" class', function(done) {
|
|
|
|
spyOn(this.parser, '_removeClass');
|
|
|
|
this.parser.render()
|
|
|
|
.then(() => {
|
|
|
|
expect(this.parser._removeClass).toHaveBeenCalledWith('loading');
|
2015-03-14 21:11:14 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-12-30 02:31:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('removes the progress element', function(done) {
|
|
|
|
this.parser.render()
|
|
|
|
.then(() => {
|
|
|
|
expect(this.container.querySelector('.loading')).toBeNull();
|
2015-03-14 21:11:14 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-12-30 02:31:36 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|