Changing util.js to export an object
This allows the methods to be spied on in tests
This commit is contained in:
parent
8a9a129856
commit
6c5b36f334
@ -1,4 +1,4 @@
|
|||||||
import { customEvent } from 'src/js/util.js';
|
import util from 'src/js/util.js';
|
||||||
import Regexper from 'src/js/regexper.js';
|
import Regexper from 'src/js/regexper.js';
|
||||||
import Parser from 'src/js/parser/javascript.js';
|
import Parser from 'src/js/parser/javascript.js';
|
||||||
import Snap from 'snapsvg';
|
import Snap from 'snapsvg';
|
||||||
@ -25,7 +25,7 @@ describe('regexper.js', function() {
|
|||||||
describe('#keypressListener', function() {
|
describe('#keypressListener', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.event = customEvent('keypress');
|
this.event = util.customEvent('keypress');
|
||||||
spyOn(this.event, 'preventDefault');
|
spyOn(this.event, 'preventDefault');
|
||||||
spyOn(this.regexper.form, 'dispatchEvent');
|
spyOn(this.regexper.form, 'dispatchEvent');
|
||||||
});
|
});
|
||||||
@ -100,7 +100,7 @@ describe('regexper.js', function() {
|
|||||||
describe('#documentKeypressListener', function() {
|
describe('#documentKeypressListener', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.event = customEvent('keyup');
|
this.event = util.customEvent('keyup');
|
||||||
this.regexper.runningParser = jasmine.createSpyObj('parser', ['cancel']);
|
this.regexper.runningParser = jasmine.createSpyObj('parser', ['cancel']);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ describe('regexper.js', function() {
|
|||||||
describe('#submitListener', function() {
|
describe('#submitListener', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.event = customEvent('submit');
|
this.event = util.customEvent('submit');
|
||||||
spyOn(this.event, 'preventDefault');
|
spyOn(this.event, 'preventDefault');
|
||||||
|
|
||||||
this.regexper.field.value = 'example value';
|
this.regexper.field.value = 'example value';
|
||||||
@ -191,7 +191,7 @@ describe('regexper.js', function() {
|
|||||||
describe('#updatePercentage', function() {
|
describe('#updatePercentage', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.event = customEvent('updateStatus', { percentage: 0.42 });
|
this.event = util.customEvent('updateStatus', { percentage: 0.42 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets the width of the progress bar', function() {
|
it('sets the width of the progress bar', function() {
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import { customEvent, normalizeBBox, spaceHorizontally, spaceVertically } from 'src/js/util.js';
|
import util from 'src/js/util.js';
|
||||||
|
|
||||||
describe('util.js', function() {
|
describe('util.js', function() {
|
||||||
|
|
||||||
describe('customEvent', function() {
|
describe('customEvent', function() {
|
||||||
|
|
||||||
it('sets the event type', function() {
|
it('sets the event type', function() {
|
||||||
var event = customEvent('example');
|
var event = util.customEvent('example');
|
||||||
expect(event.type).toEqual('example');
|
expect(event.type).toEqual('example');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets the event detail', function() {
|
it('sets the event detail', function() {
|
||||||
var event = customEvent('example', 'detail');
|
var event = util.customEvent('example', 'detail');
|
||||||
expect(event.detail).toEqual('detail');
|
expect(event.detail).toEqual('detail');
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ describe('util.js', function() {
|
|||||||
describe('normalizeBBox', function() {
|
describe('normalizeBBox', function() {
|
||||||
|
|
||||||
it('defaults the anchor keys to values from the bbox', function() {
|
it('defaults the anchor keys to values from the bbox', function() {
|
||||||
expect(normalizeBBox({
|
expect(util.normalizeBBox({
|
||||||
x: 'bbox x',
|
x: 'bbox x',
|
||||||
x2: 'bbox x2',
|
x2: 'bbox x2',
|
||||||
cy: 'bbox cy',
|
cy: 'bbox cy',
|
||||||
@ -53,7 +53,7 @@ describe('util.js', function() {
|
|||||||
spyOn(items[1], 'transform').and.callThrough();
|
spyOn(items[1], 'transform').and.callThrough();
|
||||||
spyOn(items[2], 'transform').and.callThrough();
|
spyOn(items[2], 'transform').and.callThrough();
|
||||||
|
|
||||||
spaceHorizontally(items, { padding: 5 });
|
util.spaceHorizontally(items, { padding: 5 });
|
||||||
|
|
||||||
expect(items[0].transform).toHaveBeenCalledWith(Snap.matrix()
|
expect(items[0].transform).toHaveBeenCalledWith(Snap.matrix()
|
||||||
.translate(0, 10));
|
.translate(0, 10));
|
||||||
@ -82,7 +82,7 @@ describe('util.js', function() {
|
|||||||
spyOn(items[1], 'transform').and.callThrough();
|
spyOn(items[1], 'transform').and.callThrough();
|
||||||
spyOn(items[2], 'transform').and.callThrough();
|
spyOn(items[2], 'transform').and.callThrough();
|
||||||
|
|
||||||
spaceVertically(items, { padding: 5 });
|
util.spaceVertically(items, { padding: 5 });
|
||||||
|
|
||||||
expect(items[0].transform).toHaveBeenCalledWith(Snap.matrix()
|
expect(items[0].transform).toHaveBeenCalledWith(Snap.matrix()
|
||||||
.translate(10, 0));
|
.translate(10, 0));
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { customEvent } from './util.js';
|
import util from './util.js';
|
||||||
import Regexper from './regexper.js';
|
import Regexper from './regexper.js';
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@ -8,7 +8,7 @@ import Regexper from './regexper.js';
|
|||||||
regexper.bindListeners();
|
regexper.bindListeners();
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.dispatchEvent(customEvent('hashchange'));
|
window.dispatchEvent(util.customEvent('hashchange'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { spaceVertically } from '../../util.js';
|
import util from '../../util.js';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import Q from 'q';
|
import Q from 'q';
|
||||||
export default {
|
export default {
|
||||||
@ -26,7 +26,7 @@ export default {
|
|||||||
return part.render(this.partContainer.group());
|
return part.render(this.partContainer.group());
|
||||||
}))
|
}))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
spaceVertically(this.elements, {
|
util.spaceVertically(this.elements, {
|
||||||
padding: 5
|
padding: 5
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { normalizeBBox, 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,8 @@ export default {
|
|||||||
definedProperties: {
|
definedProperties: {
|
||||||
_anchor: {
|
_anchor: {
|
||||||
get: function() {
|
get: function() {
|
||||||
var start = normalizeBBox(_.first(this.items).getBBox()),
|
var start = util.normalizeBBox(_.first(this.items).getBBox()),
|
||||||
end = normalizeBBox(_.last(this.items).getBBox()),
|
end = util.normalizeBBox(_.last(this.items).getBBox()),
|
||||||
matrix = this.transform().localMatrix;
|
matrix = this.transform().localMatrix;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -44,15 +44,15 @@ export default {
|
|||||||
var prev, next, paths;
|
var prev, next, paths;
|
||||||
|
|
||||||
this.items = items;
|
this.items = items;
|
||||||
spaceHorizontally(items, {
|
util.spaceHorizontally(items, {
|
||||||
padding: 10
|
padding: 10
|
||||||
});
|
});
|
||||||
|
|
||||||
prev = normalizeBBox(_.first(items).getBBox());
|
prev = util.normalizeBBox(_.first(items).getBBox());
|
||||||
paths = _.map(items.slice(1), item => {
|
paths = _.map(items.slice(1), item => {
|
||||||
var path;
|
var path;
|
||||||
|
|
||||||
next = normalizeBBox(item.getBBox());
|
next = util.normalizeBBox(item.getBBox());
|
||||||
path = `M${prev.ax2},${prev.ay}H${next.ax}`;
|
path = `M${prev.ax2},${prev.ay}H${next.ax}`;
|
||||||
prev = next;
|
prev = next;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { customEvent, normalizeBBox } from '../../util.js';
|
import util from '../../util.js';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import Q from 'q';
|
import Q from 'q';
|
||||||
|
|
||||||
@ -107,7 +107,7 @@ export default class Node {
|
|||||||
|
|
||||||
this.state.renderCounter--;
|
this.state.renderCounter--;
|
||||||
|
|
||||||
document.body.dispatchEvent(customEvent('updateStatus', {
|
document.body.dispatchEvent(util.customEvent('updateStatus', {
|
||||||
percentage: (this.state.maxCounter - this.state.renderCounter) / this.state.maxCounter
|
percentage: (this.state.maxCounter - this.state.renderCounter) / this.state.maxCounter
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { spaceVertically } from '../../util.js';
|
import util from '../../util.js';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import Q from 'q';
|
import Q from 'q';
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ export default {
|
|||||||
var containerBox,
|
var containerBox,
|
||||||
paths;
|
paths;
|
||||||
|
|
||||||
spaceVertically(this.matches, {
|
util.spaceVertically(this.matches, {
|
||||||
padding: 5
|
padding: 5
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { customEvent } from './util.js';
|
import util from './util.js';
|
||||||
import Parser from './parser/javascript.js';
|
import Parser from './parser/javascript.js';
|
||||||
import Snap from 'snapsvg';
|
import Snap from 'snapsvg';
|
||||||
import Q from 'q';
|
import Q from 'q';
|
||||||
@ -27,7 +27,7 @@ export default class Regexper {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.form.dispatchEvent(customEvent('submit'));
|
this.form.dispatchEvent(util.customEvent('submit'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
export function customEvent(name, detail) {
|
function customEvent(name, detail) {
|
||||||
var evt = document.createEvent('Event');
|
var evt = document.createEvent('Event');
|
||||||
evt.initEvent(name, true, true);
|
evt.initEvent(name, true, true);
|
||||||
evt.detail = detail;
|
evt.detail = detail;
|
||||||
@ -8,7 +8,7 @@ export function customEvent(name, detail) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function normalizeBBox(box) {
|
function normalizeBBox(box) {
|
||||||
return _.extend({
|
return _.extend({
|
||||||
ax: box.x,
|
ax: box.x,
|
||||||
ax2: box.x2,
|
ax2: box.x2,
|
||||||
@ -16,7 +16,7 @@ export function normalizeBBox(box) {
|
|||||||
}, box);
|
}, box);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function spaceHorizontally(items, options) {
|
function spaceHorizontally(items, options) {
|
||||||
var verticalCenter = 0;
|
var verticalCenter = 0;
|
||||||
|
|
||||||
options = _.defaults(options || {}, {
|
options = _.defaults(options || {}, {
|
||||||
@ -44,7 +44,7 @@ export function spaceHorizontally(items, options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function spaceVertically(items, options) {
|
function spaceVertically(items, options) {
|
||||||
var horizontalCenter = 0;
|
var horizontalCenter = 0;
|
||||||
|
|
||||||
options = _.defaults(options || {}, {
|
options = _.defaults(options || {}, {
|
||||||
|
Loading…
Reference in New Issue
Block a user