regexper-static/spec/parser/javascript_spec.js

174 lines
4.8 KiB
JavaScript
Raw Normal View History

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() {
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-20 15:47:41 +00:00
describe('#parse', function() {
beforeEach(function() {
spyOn(regexpParser, 'parse');
});
it('adds the "loading" class', function() {
spyOn(this.parser, '_addClass');
this.parser.parse('example expression');
expect(this.parser._addClass).toHaveBeenCalledWith('loading');
});
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();
});
});
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();
});
});
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-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
});
});
describe('#render', function() {
beforeEach(function() {
2015-03-14 21:11:14 +00:00
this.renderPromise = this.testablePromise();
this.parser.parsed = jasmine.createSpyObj('parsed', ['render']);
this.parser.parsed.render.and.returnValue(this.renderPromise.promise);
});
it('render the parsed expression', function() {
this.parser.render();
expect(this.parser.parsed.render).toHaveBeenCalled();
});
describe('when rendering is complete', function() {
beforeEach(function() {
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);
});
it('positions the renderd expression', function(done) {
this.parser.render()
.then(() => {
expect(this.result.transform).toHaveBeenCalledWith(Snap.matrix()
.translate(6, 8));
2015-03-14 21:11:14 +00:00
done();
});
});
it('sets the dimensions of the image', function(done) {
this.parser.render()
.then(() => {
2015-11-01 15:28:37 +00:00
let svg = this.container.querySelector('svg');
expect(svg.getAttribute('width')).toEqual('62');
expect(svg.getAttribute('height')).toEqual('44');
2015-03-14 21:11:14 +00:00
done();
});
});
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();
});
});
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();
});
});
});
});
describe('#cancel', function() {
it('sets the cancelRender state to true', function() {
this.parser.cancel();
expect(this.parser.state.cancelRender).toEqual(true);
});
});
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']);
});
});
});