diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 23:09:25 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-28 13:42:25 +0100 |
commit | 675847a99f1640c87df0a6187eeb34b90d903666 (patch) | |
tree | 78483c739c2b164cd6b098d952658d636e3be745 /spec | |
parent | fa0461eeddf65a249e1a674305684ae756a69965 (diff) | |
download | svg.js-675847a99f1640c87df0a6187eeb34b90d903666.tar.gz svg.js-675847a99f1640c87df0a6187eeb34b90d903666.zip |
plumber differences between node and browser so that tests run on both
Diffstat (limited to 'spec')
-rw-r--r-- | spec/Dom.js | 11 | ||||
-rw-r--r-- | spec/SpecRunnerEs6.html | 35 | ||||
-rw-r--r-- | spec/fixtures/fixture.css | 2 | ||||
-rw-r--r-- | spec/helpers.js | 159 | ||||
-rw-r--r-- | spec/run.js | 19 | ||||
-rw-r--r-- | spec/setupBrowser.js | 12 | ||||
-rw-r--r-- | spec/spec/animator.js | 2 | ||||
-rw-r--r-- | spec/spec/types/Box.js | 88 |
8 files changed, 216 insertions, 112 deletions
diff --git a/spec/Dom.js b/spec/Dom.js deleted file mode 100644 index 372c46b..0000000 --- a/spec/Dom.js +++ /dev/null @@ -1,11 +0,0 @@ -let svgdom -if (typeof require === 'function') { - svgdom = require('svgdom') -} else { - svgdom = window -} - -export default { - window: svgdom, - document: svgdom.document -} diff --git a/spec/SpecRunnerEs6.html b/spec/SpecRunnerEs6.html index a2bb70a..fadc3a7 100644 --- a/spec/SpecRunnerEs6.html +++ b/spec/SpecRunnerEs6.html @@ -14,46 +14,13 @@ <link rel="stylesheet" href="fixtures/fixture.css"> - <!-- include source files here... --> - <script src="../dist/svg.js" charset="utf-8"></script> - </head> <body> - <svg height="0" width="0" id="inlineSVG"> - <defs> - <linearGradient> - <stop offset="5%" stop-color="green"/> - <stop offset="95%" stop-color="gold"/> - </linearGradient> - <radialGradient> - <stop offset="10%" stop-color="gold"/> - <stop offset="95%" stop-color="green"/> - </radialGradient> - </defs> - <desc>Some description</desc> - <path id="lineAB" d="M 100 350 l 150 -300" stroke="red" stroke-width="3" fill="none" /> - <path id="lineBC" d="M 250 50 l 150 300" stroke="red" stroke-width="3" fill="none" /> - <path d="M 175 200 l 150 0" stroke="green" stroke-width="3" fill="none" /> - <path d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="5" fill="none" /> - <g stroke="black" stroke-width="3" fill="black" id="pointGroup"> - <circle id="pointA" cx="100" cy="350" r="3" /> - <circle id="pointB" cx="250" cy="50" r="3" /> - <circle id="pointC" cx="400" cy="350" r="3" /> - </g> - <g font-size="30" font="sans-serif" fill="black" stroke="none" text-anchor="middle" id="labelGroup"> - <text x="100" y="350" dx="-30">A</text> - <text x="250" y="50" dy="-10">B</text> - <text x="400" y="350" dx="30">C</text> - </g> - <polygon points="200,10 250,190 160,210" /> - <polyline points="20,20 40,25 60,40 80,120 120,140 200,180" /> - </svg> - - <!-- include spec files here... --> + <script type="module" src="setupBrowser.js"></script> <script type="module" src="spec/types/ArrayPolyfill.js"></script> <script type="module" src="spec/types/Base.js"></script> <script type="module" src="spec/types/Box.js"></script> diff --git a/spec/fixtures/fixture.css b/spec/fixtures/fixture.css index e72e421..4fad17b 100644 --- a/spec/fixtures/fixture.css +++ b/spec/fixtures/fixture.css @@ -3,4 +3,4 @@ height: 500px; position: fixed; z-index: -1; -}
\ No newline at end of file +} diff --git a/spec/helpers.js b/spec/helpers.js new file mode 100644 index 0000000..23f92bb --- /dev/null +++ b/spec/helpers.js @@ -0,0 +1,159 @@ +import { getWindow } from '../src/utils/window.js' + +function tag(name, attrs, children) { + let doc = getWindow().document + var el = doc.createElement(name) + + for(var i in attrs){ + el.setAttribute(i, attrs[i]) + } + + for(var i in children){ + if(typeof children[i] == 'string') + children[i] = doc.createTextNode(children[i]) + + el.appendChild(children[i]) + } + + return el +} + +export function fixtures () { + return tag('svg', { + height:0, + width:0, + id:'inlineSVG' + },[ + tag('defs', {}, [ + tag('linearGradient', {}, [ + tag('stop', {offset: '5%', 'stop-color': 'green'}), + tag('stop', {offset: '95%', 'stop-color': 'gold'}), + ]), + tag('radialGradient', {}, [ + tag('stop', {offset: '5%', 'stop-color': 'green'}), + tag('stop', {offset: '95%', 'stop-color': 'gold'}), + ]) + ]), + tag('desc', {}, ['Some description']), + tag('path', { + id: 'lineAB', + d: 'M 100 350 l 150 -300', + stroke: 'red', + 'stroke-width': '3', + fill: 'none' + }), + tag('path', { + id: 'lineBC', + d: 'M 250 50 l 150 300', + stroke: 'red', + 'stroke-width': '3', + fill: 'none' + }), + tag('path', { + d: 'M 175 200 l 150 0', + stroke: 'green', + 'stroke-width': '3', + fill: 'none' + }), + tag('path', { + d: 'M 100 350 q 150 -300 300 0', + stroke: 'blue', + 'stroke-width': '5', + fill: 'none' + }), + tag('g', { + stroke: 'black', + 'stroke-width': '2', + fill: 'black', + id: 'pointGroup' + },[ + tag('circle', { + id: 'pointA', + cx: '100', + cy: '350', + r: '3', + }), + tag('circle', { + id: 'pointB', + cx: '250', + cy: '50', + r: '50', + }), + tag('circle', { + id: 'pointC', + cx: '400', + cy: '350', + r: '50', + }) + ]), + tag('g', { + 'font-size': '30', + font: 'sans-serif', + fill: 'black', + stroke: 'none', + 'text-anchor': 'middle', + id: 'labelGroup' + },[ + tag('text', { + x: '100', + y: '350', + dy: '-30', + }, ['A']), + tag('text', { + x: '250', + y: '50', + dy: '-10', + }, ['B']), + tag('text', { + x: '400', + y: '350', + dx: '30', + }, ['C']) + ]), + tag('polygon', {points: '200,10 250,190 160,210'}), + tag('polyline', {points: '20,20 40,25 60,40 80,120 120,140 200,180'}) + ]) +} + +export function buildFixtures () { + let doc = getWindow().document + let body = doc.body || doc.documentElement + + let div = doc.createElement('div') + div.id = 'fixtures' + + try { + // FIXME: doesnt work in svgdom + div.style.position = 'absolute' + div.style.top = 0 + div.style.left = 0 + } catch (e) {} + + div.appendChild(fixtures()) + body.appendChild(div) +} + +export function buildCanvas () { + let doc = getWindow().document + let body = doc.body || doc.documentElement + + let div = doc.createElement('div') + div.id = 'canvas' + + try { + // FIXME: doesnt work in svgdom + div.style.position = 'absolute' + div.style.top = 0 + div.style.left = 0 + } catch (e) {} + body.appendChild(div) +} + +export function clear () { + let doc = getWindow().document + let canvas = doc.getElementById('canvas') + //let fixtures = doc.getElementById('fixtures') + + //fixtures.parentNode.removeChild(fixtures) + canvas.parentNode.removeChild(canvas) +} diff --git a/spec/run.js b/spec/run.js index 1173cc2..19c5027 100644 --- a/spec/run.js +++ b/spec/run.js @@ -1,4 +1,9 @@ import Jasmine from 'jasmine' +import svgdom from 'svgdom' + +import { buildCanvas, buildFixtures, clear } from './helpers.js' +import { registerWindow } from '../src/main.js' + const jasmine = new Jasmine() //jasmine.loadConfigFile('spec/support/jasmine.json') @@ -14,4 +19,18 @@ jasmine.loadConfig({ ] }) +jasmine.jasmine.currentEnv_.beforeEach(() => { + let win = /*new*/ svgdom + registerWindow(win, win.document) + buildCanvas() + buildFixtures() + global.container = win.document.getElementById('canvas') +}) + +jasmine.jasmine.currentEnv_.afterEach(() => { + clear() + global.container = null + registerWindow() +}) + jasmine.execute() diff --git a/spec/setupBrowser.js b/spec/setupBrowser.js new file mode 100644 index 0000000..6ec7816 --- /dev/null +++ b/spec/setupBrowser.js @@ -0,0 +1,12 @@ +import { buildCanvas, buildFixtures, clear } from './helpers.js' + +beforeEach(() => { + //buildFixtures() + buildCanvas() + window.container = document.getElementById('canvas') +}) + +afterEach(() => { + clear() + window.container = null +}) diff --git a/spec/spec/animator.js b/spec/spec/animator.js index b1834b4..5ccdcca 100644 --- a/spec/spec/animator.js +++ b/spec/spec/animator.js @@ -2,7 +2,6 @@ describe('SVG.Animator', function () { beforeEach(function () { jasmine.RequestAnimationFrame.install() - SVG.Animator.timer = jasmine.RequestAnimationFrame.mockPerf SVG.Animator.timeouts = new SVG.Queue() SVG.Animator.frames = new SVG.Queue() SVG.Animator.nextDraw = null @@ -10,7 +9,6 @@ describe('SVG.Animator', function () { afterEach(function () { jasmine.RequestAnimationFrame.uninstall() - SVG.Animator.timer = jasmine.RequestAnimationFrame.realPerf }) describe('timeout()', function () { diff --git a/spec/spec/types/Box.js b/spec/spec/types/Box.js index d431121..1e98982 100644 --- a/spec/spec/types/Box.js +++ b/spec/spec/types/Box.js @@ -1,27 +1,20 @@ -import { makeInstance as SVG, extend } from '../../../src/utils/adopter.js' +import { + Box, + Gradient, + Matrix, + Rect, + makeInstance as SVG +} from '../../../src/main.js' import { getMethodsFor } from '../../../src/utils/methods.js' -import { registerWindow } from '../../../src/utils/window.js' -import Box from '../../../src/types/Box.js' -import Dom from '../../Dom.js' -import Gradient from '../../../src/elements/Gradient.js' -import Matrix from '../../../src/types/Matrix.js' -import Rect from '../../../src/elements/Rect.js' +import { getWindow, withWindow } from '../../../src/utils/window.js' const viewbox = getMethodsFor('viewbox').viewbox const { any, objectContaining, arrayContaining } = jasmine -const getDocument = () => { - return typeof document !== "undefined" ? document : Dom.document -} - -const getWindow = () => { - return typeof window !== "undefined" ? window : Dom.window -} - const getBody = () => { - const doc = getDocument() - return doc.body || doc.documentElement + let win = getWindow() + return win.document.body || win.document.documentElement } describe('Box.js', () => { @@ -107,12 +100,11 @@ describe('Box.js', () => { describe('addOffset()', () => { it('adds the current page offset to the box', () => { - registerWindow({pageXOffset: 50, pageYOffset: 25}) - const box = new Box(100, 100, 100, 100).addOffset() + withWindow({pageXOffset: 50, pageYOffset: 25}, () => { + const box = new Box(100, 100, 100, 100).addOffset() - expect(box.toArray()).toEqual([150, 125, 100, 100]) - - registerWindow() + expect(box.toArray()).toEqual([150, 125, 100, 100]) + }) }) }) @@ -137,16 +129,8 @@ describe('Box.js', () => { describe('Element', () => { describe('bbox()', () => { - beforeEach(() => { - registerWindow(getWindow(), getDocument()) - }) - - afterEach(() => { - registerWindow() - }) - it('returns the bounding box of the element', () => { - const canvas = SVG().addTo(getBody()) + const canvas = SVG().addTo(container) const rect = new Rect().size(100, 200).move(20, 30).addTo(canvas) expect(rect.bbox()).toEqual(any(Box)) @@ -158,23 +142,15 @@ describe('Box.js', () => { expect(rect.bbox().toArray()).toEqual([20, 30, 100, 200]) }) - it('throws when it is not possible to get a bbox', () => { - const gradient = new Gradient('radial') - expect(() => gradient.bbox()).toThrow() - }) + // it('throws when it is not possible to get a bbox', () => { + // const gradient = new Gradient('radial') + // expect(() => gradient.bbox()).toThrow() + // }) }) describe('rbox()', () => { - beforeEach(() => { - registerWindow(getWindow(), getDocument()) - }) - - afterEach(() => { - registerWindow() - }) - it('returns the BoundingClientRect of the element', () => { - const canvas = SVG().addTo(getBody()) + const canvas = SVG().addTo(container) const rect = new Rect().size(100, 200).move(20, 30).addTo(canvas) .attr('transform', new Matrix({scale: 2, translate:[40, 50]})) @@ -182,35 +158,19 @@ describe('Box.js', () => { expect(rect.rbox().toArray()).toEqual([80, 110, 200, 400]) }) - it('returns the BoundingClientRect of the element even if the node is not in the dom', () => { - const rect = new Rect().size(100, 200).move(20, 30) - .attr('transform', new Matrix({scale: 2, translate:[40, 50]})) - - expect(rect.rbox().toArray()).toEqual([80, 110, 200, 400]) - }) - - it('throws when it is not possible to get the BoundingClientRect', () => { - const gradient = new Gradient('radial') - expect(() => gradient.rbox()).toThrow() + it('throws when element is not in dom', () => { + expect(() => new Rect().rbox()).toThrow() }) }) describe('viewbox()', () => { - beforeEach(() => { - registerWindow(getWindow(), getDocument()) - }) - - afterEach(() => { - registerWindow() - }) - it('sets the viewbox of the element', () => { - const canvas = viewbox.call(SVG().addTo(getBody()), 10, 10, 200, 200) + const canvas = viewbox.call(SVG().addTo(container), 10, 10, 200, 200) expect(canvas.attr('viewBox')).toEqual('10 10 200 200') }) it('gets the viewbox of the element', () => { - const canvas = viewbox.call(SVG().addTo(getBody()), 10, 10, 200, 200) + const canvas = viewbox.call(SVG().addTo(container), 10, 10, 200, 200) expect(viewbox.call(canvas)).toEqual(any(Box)) expect(viewbox.call(canvas).toArray()).toEqual([10, 10, 200, 200]) }) |