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