From 6c5b36f3340c7985ba7c3dda49c77350f8442e05 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sat, 20 Dec 2014 10:25:32 -0500 Subject: [PATCH] Changing util.js to export an object This allows the methods to be spied on in tests --- spec/regexper_spec.js | 10 +++++----- spec/util_spec.js | 12 ++++++------ src/js/main.js | 4 ++-- src/js/parser/javascript/charset.js | 4 ++-- src/js/parser/javascript/match.js | 12 ++++++------ src/js/parser/javascript/node.js | 4 ++-- src/js/parser/javascript/regexp.js | 4 ++-- src/js/regexper.js | 4 ++-- src/js/util.js | 8 ++++---- 9 files changed, 31 insertions(+), 31 deletions(-) diff --git a/spec/regexper_spec.js b/spec/regexper_spec.js index 47a1e7d..f4fc70e 100644 --- a/spec/regexper_spec.js +++ b/spec/regexper_spec.js @@ -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() { diff --git a/spec/util_spec.js b/spec/util_spec.js index dd3f57c..b555364 100644 --- a/spec/util_spec.js +++ b/spec/util_spec.js @@ -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)); diff --git a/src/js/main.js b/src/js/main.js index 7c1b246..c014cb4 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -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')); }); } }()); diff --git a/src/js/parser/javascript/charset.js b/src/js/parser/javascript/charset.js index 7b3019d..ff20a88 100644 --- a/src/js/parser/javascript/charset.js +++ b/src/js/parser/javascript/charset.js @@ -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 }); diff --git a/src/js/parser/javascript/match.js b/src/js/parser/javascript/match.js index 4a5b681..d093d23 100644 --- a/src/js/parser/javascript/match.js +++ b/src/js/parser/javascript/match.js @@ -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; diff --git a/src/js/parser/javascript/node.js b/src/js/parser/javascript/node.js index 802fc15..28a2fa6 100644 --- a/src/js/parser/javascript/node.js +++ b/src/js/parser/javascript/node.js @@ -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 })); diff --git a/src/js/parser/javascript/regexp.js b/src/js/parser/javascript/regexp.js index 5a6b51f..2574af0 100644 --- a/src/js/parser/javascript/regexp.js +++ b/src/js/parser/javascript/regexp.js @@ -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 }); diff --git a/src/js/regexper.js b/src/js/regexper.js index f291055..8ccde89 100644 --- a/src/js/regexper.js +++ b/src/js/regexper.js @@ -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')); } } diff --git a/src/js/util.js b/src/js/util.js index fa1dea4..66eb3dd 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -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 || {}, {