diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 09:36:48 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-28 13:41:21 +0100 |
commit | e03e0323edd1e6580a355952d6cfc1a4436b42df (patch) | |
tree | 5d1845fddf8ae283e9b852147eb200ed60218315 | |
parent | 58fd0d68bc8b46b23f8cdc65190f08d4e524eb57 (diff) | |
download | svg.js-e03e0323edd1e6580a355952d6cfc1a4436b42df.tar.gz svg.js-e03e0323edd1e6580a355952d6cfc1a4436b42df.zip |
started to add es6 tests using svgdom as fallback in case no dom is found
-rw-r--r-- | package-lock.json | 70 | ||||
-rw-r--r-- | package.json | 6 | ||||
-rw-r--r-- | spec/Dom.js | 5 | ||||
-rw-r--r-- | spec/run.js | 17 | ||||
-rw-r--r-- | spec/spec/types/ArrayPolyfill.js | 25 | ||||
-rw-r--r-- | spec/spec/types/Base.js | 9 | ||||
-rw-r--r-- | spec/spec/types/Box.js | 221 |
7 files changed, 292 insertions, 61 deletions
diff --git a/package-lock.json b/package-lock.json index e7a88bc..95b4822 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1051,11 +1051,6 @@ "dev": true, "optional": true }, - "animation-frame-polyfill": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/animation-frame-polyfill/-/animation-frame-polyfill-1.0.1.tgz", - "integrity": "sha1-X1rZk6eHlL0Xas3lttzmKGdBDJ0=" - }, "ansi-align": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ansi-align/-/ansi-align-2.0.0.tgz", @@ -4348,12 +4343,6 @@ "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==", "dev": true }, - "graceful-readlink": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz", - "integrity": "sha1-TK+tdrxi8C+gObL5Tpo906ORpyU=", - "dev": true - }, "growl": { "version": "1.10.5", "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", @@ -4698,12 +4687,6 @@ "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==", "dev": true }, - "innersvg-polyfill": { - "version": "0.0.2", - "resolved": "https://registry.npmjs.org/innersvg-polyfill/-/innersvg-polyfill-0.0.2.tgz", - "integrity": "sha1-i9M2G0UpHqOMv8brcYI+7RiQrzk=", - "dev": true - }, "inquirer": { "version": "6.2.0", "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-6.2.0.tgz", @@ -5099,6 +5082,16 @@ } } }, + "jasmine": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-3.3.0.tgz", + "integrity": "sha512-haZzMvmoWSI2VCKfDgPqyEOPBQA7C1fgtIMgKNU4hVMcrVkWU5NPOWQqOTA6mVFyKcSUUrnkXu/ZEgY0bRnd6A==", + "dev": true, + "requires": { + "glob": "^7.0.6", + "jasmine-core": "~3.3.0" + } + }, "jasmine-core": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.3.0.tgz", @@ -7108,37 +7101,6 @@ } } }, - "rollup-plugin-uglify-es": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/rollup-plugin-uglify-es/-/rollup-plugin-uglify-es-0.0.1.tgz", - "integrity": "sha1-5FZE8raFpZq9uTY0ByB6A6e1qbc=", - "dev": true, - "requires": { - "uglify-es": "3.0.3" - }, - "dependencies": { - "commander": { - "version": "2.9.0", - "resolved": "http://registry.npmjs.org/commander/-/commander-2.9.0.tgz", - "integrity": "sha1-nJkJQXbhIkDLItbFFGCYQA/g99Q=", - "dev": true, - "requires": { - "graceful-readlink": ">= 1.0.0" - } - }, - "uglify-es": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/uglify-es/-/uglify-es-3.0.3.tgz", - "integrity": "sha1-Y8yEqpRos0lzpIh3h8ZMAaiodXY=", - "dev": true, - "requires": { - "commander": "~2.9.0", - "source-map": "~0.5.1", - "uglify-to-browserify": "~1.0.0" - } - } - } - }, "rollup-pluginutils": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.3.3.tgz", @@ -7982,9 +7944,8 @@ } }, "svgdom": { - "version": "0.0.15", - "resolved": "https://registry.npmjs.org/svgdom/-/svgdom-0.0.15.tgz", - "integrity": "sha1-djlZYSQUjJ8eBlntNGgtN5s2dO4=", + "version": "github:svgdotjs/svgdom#a3643a60e67933a9d5ea439d068c432d50ce308f", + "from": "github:svgdotjs/svgdom", "dev": true, "requires": { "fontkit": "^1.5.4", @@ -8346,13 +8307,6 @@ } } }, - "uglify-to-browserify": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz", - "integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=", - "dev": true, - "optional": true - }, "ultron": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz", diff --git a/package.json b/package.json index 3c8366e..e89abd7 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "server": "npx http-server ./ -d", "test": "npx karma start .config/karma.conf.js", "test:ci": "karma start .config/karma.conf.saucelabs.js", + "test:quick": "node -r esm ./spec/run.js || true", "build:polyfills": "npx rollup -c .config/rollup.polyfills.js", "prepublishOnly": "npm run build && npm run build:polyfills && npm test", "postPublish": "echo Please upload a zip to the github release containing the dist, license and changelog" @@ -82,7 +83,7 @@ "eslint-plugin-standard": "^4.0.0", "esm": "^3.0.84", "http-server": "^0.11.1", - "innersvg-polyfill": "0.0.2", + "jasmine": "^3.3.0", "jasmine-core": "^3.3.0", "karma": "^3.1.1", "karma-chrome-launcher": "^2.2.0", @@ -97,8 +98,7 @@ "rollup-plugin-node-resolve": "^3.4.0", "rollup-plugin-terser": "^3.0.0", "rollup-plugin-uglify": "^6.0.0", - "rollup-plugin-uglify-es": "0.0.1", - "svgdom": "latest" + "svgdom": "github:svgdotjs/svgdom" }, "browserslist": [ "last 1 version", diff --git a/spec/Dom.js b/spec/Dom.js new file mode 100644 index 0000000..e7574d5 --- /dev/null +++ b/spec/Dom.js @@ -0,0 +1,5 @@ +import svgdom from 'svgdom' +export default { + window: svgdom, + document: svgdom.document +} diff --git a/spec/run.js b/spec/run.js new file mode 100644 index 0000000..1173cc2 --- /dev/null +++ b/spec/run.js @@ -0,0 +1,17 @@ +import Jasmine from 'jasmine' +const jasmine = new Jasmine() + +//jasmine.loadConfigFile('spec/support/jasmine.json') + +jasmine.loadConfig({ + "spec_dir": "spec/spec", + "spec_files": [ + "types/*.js", + // "!(helpers).js" + ], + "helpers": [ + // "helpers.js" + ] +}) + +jasmine.execute() diff --git a/spec/spec/types/ArrayPolyfill.js b/spec/spec/types/ArrayPolyfill.js new file mode 100644 index 0000000..5609629 --- /dev/null +++ b/spec/spec/types/ArrayPolyfill.js @@ -0,0 +1,25 @@ +import { subClassArray } from '../../../src/types/ArrayPolyfill.js' +const { any, createSpy } = jasmine + +describe('ArrayPolyfill.js', function() { + describe('subClassArray()', function () { + it('creates a new class inherited from Array', () => { + const myArray = subClassArray('myArray', Array, ) + expect(new myArray()).toEqual(any(Array)) + expect(new myArray()).toEqual(any(myArray)) + }) + + it('sets the name attribute of the class correctly', () => { + const myArray = subClassArray('myArray', Array) + expect(myArray.name).toEqual('myArray') + }) + + it('calls the given function on construction', () => { + const spy = createSpy() + const myArray = subClassArray('myArray', Array, spy) + + new myArray(1,2,3,4) + expect(spy).toHaveBeenCalledWith(1, 2, 3, 4) + }) + }) +}) diff --git a/spec/spec/types/Base.js b/spec/spec/types/Base.js new file mode 100644 index 0000000..09ef453 --- /dev/null +++ b/spec/spec/types/Base.js @@ -0,0 +1,9 @@ +import Base from '../../../src/types/Base.js' + +const { any } = jasmine + +describe('Base.js', () => { + it('holds the base class', () => { + expect(new Base).toEqual(any(Base)) + }) +}) diff --git a/spec/spec/types/Box.js b/spec/spec/types/Box.js new file mode 100644 index 0000000..689a3a3 --- /dev/null +++ b/spec/spec/types/Box.js @@ -0,0 +1,221 @@ +import { makeInstance as SVG, extend } from '../../../src/utils/adopter.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' + +const bbox = getMethodsFor('Element').bbox +const rbox = getMethodsFor('Element').rbox +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 +} + +describe('Box.js', () => { + describe('()', () => { + it('creates a new Box with default attributes', () => { + const box = new Box() + expect(box).toEqual(any(Box)) + expect(box).toEqual(objectContaining({ + width: 0, height: 0, x: 0, y: 0, w: 0, h: 0, cx: 0, cy: 0, x2: 0, y2: 0 + })) + }) + }) + + describe('init()', () => { + it('inits or reinits the box according to input', () => { + expect(new Box().init(1,2,3,4).toArray()).toEqual([1,2,3,4]) + }) + + it('works with array input', () => { + expect(new Box().init([1,2,3,4]).toArray()).toEqual([1,2,3,4]) + }) + + it('works with 3 arguments as input', () => { + expect(new Box().init(1,2,3,4).toArray()).toEqual([1,2,3,4]) + }) + + it('works with string input', () => { + expect(new Box().init('1,2,3,4').toArray()).toEqual([1,2,3,4]) + }) + + it('creates a new box from parsed string with exponential values', function() { + expect(new Box().init('-1.12e1 1e-2 +2e2 +.3e+4').toArray()) + .toEqual([-11.2, 0.01, 200, 3000]) + }) + + it('works with object input', () => { + expect(new Box().init({x: 1, y: 2, width: 3, height: 4}).toArray()) + .toEqual([1,2,3,4]) + }) + + it('calculates all derived values correctly', () => { + expect(new Box().init(2, 4, 6, 8)).toEqual(objectContaining({ + cx: 5, cy: 8, x2: 8, y2: 12, w: 6, h: 8 + })) + }) + + it('can handle input with left instead of x and top instead of y', () => { + expect(new Box().init({left: 1, top: 2, width: 3, height: 4}).toArray()) + .toEqual([1,2,3,4]) + }) + }) + + describe('merge()', () => { + it('merges various bounding boxes', () => { + var box1 = new Box(50, 50, 100, 100) + var box2 = new Box(300, 400, 100, 100) + var box3 = new Box(500, 100, 100, 100) + var merged = box1.merge(box2).merge(box3) + + expect(merged.toArray()).toEqual([50, 50, 550, 450]) + }) + + it('returns a new instance', () => { + var box1 = new Box(50, 50, 100, 100) + var box2 = new Box(300, 400, 100, 100) + var merged = box1.merge(box2) + + expect(merged).toEqual(any(Box)) + }) + }) + + describe('transform()', () => { + it('transforms the box with given matrix', () => { + var box1 = new Box(50, 50, 100, 100).transform(new Matrix(1,0,0,1,20,20)) + var box2 = new Box(50, 50, 100, 100).transform(new Matrix(2,0,0,2,0,0)) + var box3 = new Box(-200, -200, 100, 100).transform(new Matrix(1,0,0,1,-20,-20)) + + expect(box1.toArray()).toEqual([70, 70, 100, 100]) + expect(box2.toArray()).toEqual([100, 100, 200, 200]) + expect(box3.toArray()).toEqual([-220, -220, 100, 100]) + }) + }) + + 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() + + expect(box.toArray()).toEqual([150, 125, 100, 100]) + + registerWindow() + }) + }) + + describe('toString()', () => { + it('returns a string representation of the box', () => { + expect(new Box(1,2,3,4).toString()).toBe('1 2 3 4') + }) + }) + + describe('toArray()', () => { + it('returns an array representation of the box', () => { + expect(new Box(1,2,3,4).toArray()).toEqual([1,2,3,4]) + }) + }) + + describe('isNulled()', () => { + it('checks if the box consists of only zeros', () => { + expect(new Box().isNulled()).toBe(true) + expect(new Box(1,2,3,4).isNulled()).toBe(false) + }) + }) + + describe('Element', () => { + describe('bbox()', () => { + beforeEach(() => { + registerWindow(getWindow(), getDocument()) + }) + + afterEach(() => { + registerWindow() + }) + + it('returns the bounding box of the element', () => { + const canvas = SVG().addTo(getBody()) + const rect = new Rect().size(100, 200).move(20, 30).addTo(canvas) + + expect(bbox.call(rect)).toEqual(any(Box)) + expect(bbox.call(rect).toArray()).toEqual([20, 30, 100, 200]) + }) + + it('returns the bounding box of the element even if the node is not in the dom', () => { + const rect = new Rect().size(100, 200).move(20, 30) + expect(bbox.call(rect).toArray()).toEqual([20, 30, 100, 200]) + }) + + it('throws when it is not possible to get a bbox', () => { + const gradient = new Gradient('radial') + expect(() => bbox.call(gradient)).toThrow() + }) + }) + + describe('rbox()', () => { + beforeEach(() => { + registerWindow(getWindow(), getDocument()) + }) + + afterEach(() => { + registerWindow() + }) + + it('returns the BoundingClientRect of the element', () => { + const canvas = SVG().addTo(getBody()) + const rect = new Rect().size(100, 200).move(20, 30).addTo(canvas) + .attr('transform', new Matrix({scale: 2, translate:[40, 50]})) + + expect(rbox.call(rect)).toEqual(any(Box)) + expect(rbox.call(rect).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(rbox.call(rect).toArray()).toEqual([80, 110, 200, 400]) + }) + + it('throws when it is not possible to get the BoundingClientRect', () => { + const gradient = new Gradient('radial') + expect(() => rbox.call(gradient)).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) + 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) + expect(viewbox.call(canvas)).toEqual(any(Box)) + expect(viewbox.call(canvas).toArray()).toEqual([10, 10, 200, 200]) + }) + }) + }) +}) |