diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-18 19:13:28 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-28 13:47:39 +0100 |
commit | b5bd292b7d042690336e08dcd423894449c36c66 (patch) | |
tree | 57e262db049c4505a32c56fd7516b087ee8d4cf3 | |
parent | 57d7dd0232b283874f09bb08e6d05ccfc6d1da24 (diff) | |
download | svg.js-b5bd292b7d042690336e08dcd423894449c36c66.tar.gz svg.js-b5bd292b7d042690336e08dcd423894449c36c66.zip |
add tests for all exports of adopter
-rw-r--r-- | spec/SpecRunnerEs6.html | 1 | ||||
-rw-r--r-- | spec/helpers.js | 5 | ||||
-rw-r--r-- | spec/spec/utils/adopter.js | 161 | ||||
-rw-r--r-- | src/utils/adopter.js | 18 |
4 files changed, 177 insertions, 8 deletions
diff --git a/spec/SpecRunnerEs6.html b/spec/SpecRunnerEs6.html index fadc3a7..cca33c0 100644 --- a/spec/SpecRunnerEs6.html +++ b/spec/SpecRunnerEs6.html @@ -24,6 +24,7 @@ <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> + <script type="module" src="spec/utils/adopter.js"></script> </body> </html> diff --git a/spec/helpers.js b/spec/helpers.js index 23f92bb..a584b6a 100644 --- a/spec/helpers.js +++ b/spec/helpers.js @@ -152,8 +152,9 @@ export function buildCanvas () { export function clear () { let doc = getWindow().document let canvas = doc.getElementById('canvas') - //let fixtures = doc.getElementById('fixtures') + let fixtures = doc.getElementById('fixtures') - //fixtures.parentNode.removeChild(fixtures) + // remove if present + fixtures && fixtures.parentNode.removeChild(fixtures) canvas.parentNode.removeChild(canvas) } diff --git a/spec/spec/utils/adopter.js b/spec/spec/utils/adopter.js new file mode 100644 index 0000000..e038a5d --- /dev/null +++ b/spec/spec/utils/adopter.js @@ -0,0 +1,161 @@ +const { any, createSpy, objectContaining } = jasmine + +import { + makeNode, + makeInstance, + nodeOrNew, + register, + getClass, + eid, + extend, + wrapWithAttrCheck, + Rect, + Element, + adopt, + root +} from '../../../src/main.js' + +import { getWindow } from '../../../src/utils/window.js' +import { mockAdopt } from '../../../src/utils/adopter.js' +import { buildFixtures } from '../../helpers.js' + +const Node = getWindow().Node + +describe('Adopter.js', () => { + describe('makeNode()', () => { + it('creates a node of the specified type', () => { + let rect = makeNode('rect') + expect(rect).toEqual(any(Node)) + expect(rect.nodeName).toBe('rect') + }) + }) + + describe('makeInstance()', () => { + const adoptSpy = createSpy('adopt') + + beforeEach(() => { + adoptSpy.calls.reset() + mockAdopt(adoptSpy) + }) + + afterEach(() => { + mockAdopt() + }) + + it('creates a root-object when no argument given', () => { + let doc = makeInstance() + + expect(doc).toEqual(any(getClass(root))) + expect(doc).toEqual(any(Element)) + }) + + it('returns a given svg.js object directly', () => { + let rect = new Rect() + let samerect = makeInstance(rect) + expect(rect).toBe(samerect) + }) + + it('creates an element from passed svg string', () => { + makeInstance('<rect width="200px">') + + expect(adoptSpy).toHaveBeenCalledWith(any(Node)) + expect(adoptSpy).toHaveBeenCalledWith(objectContaining({nodeName: 'rect'})) + }) + + it('searches for element in dom if selector given', () => { + buildFixtures() + + let path = getWindow().document.getElementById('lineAB') + + makeInstance('#lineAB') + makeInstance('#doesNotExist') + + expect(adoptSpy).toHaveBeenCalledWith(path) + expect(adoptSpy).toHaveBeenCalledWith(null) + }) + + it('calls adopt when passed a node', () => { + makeInstance(makeNode('rect')) + + expect(adoptSpy).toHaveBeenCalledWith(any(Node)) + expect(adoptSpy).toHaveBeenCalledWith(objectContaining({nodeName: 'rect'})) + }) + }) + + describe('nodeOrNew()', () => { + it('creates a node of node argument is null', () => { + let rect = nodeOrNew('rect', null) + expect(rect).toEqual(any(Node)) + expect(rect.nodeName).toBe('rect') + }) + + it('returns the node if one is passed', () => { + let div = document.createElement('div') + expect(nodeOrNew('something', div)).toBe(div) + }) + }) + + describe('register()/getClass()', () => { + it('sets and gets a class from the class register', () => { + const a = class {} + register(a) + expect(getClass('a')).toBe(a) + }) + }) + + describe('edi()', () => { + it('returns a unique id', () => { + expect(eid('foo')).not.toBe(eid('foo')) + }) + }) + + describe('extend()', () => { + it('adds all functions in the given object to the target object', () => { + const a = class {} + + extend(a, { + test () { this.prop = 'test'; return this } + }) + + expect(typeof a.prototype.test).toBe('function') + expect(new a().test().prop).toBe('test') + }) + it('accepts and extend multiple modules at once', () => { + const a = class {} + const b = class {} + const c = class {} + + extend([a, b, c], { + test () { this.prop = 'test'; return this } + }) + + expect(typeof a.prototype.test).toBe('function') + expect(new a().test().prop).toBe('test') + expect(typeof b.prototype.test).toBe('function') + expect(new b().test().prop).toBe('test') + expect(typeof c.prototype.test).toBe('function') + expect(new c().test().prop).toBe('test') + }) + }) + + describe('wrapWithAttrCheck()', () => { + it('wraps a function so that it calles an attr function if an object is passed', () => { + const attrSpy = createSpy('attr') + + const a = class {} + extend(a, { + test: wrapWithAttrCheck(function () { + this.prop = 'test'; return this + }), + attr: attrSpy + }) + + const obj = {} + + expect(new a().test().prop).toBe('test') + expect(attrSpy).not.toHaveBeenCalled() + new a().test(obj) + expect(attrSpy).toHaveBeenCalledWith(obj) + }) + }) +}) diff --git a/src/utils/adopter.js b/src/utils/adopter.js index a4c60de..5c1b0b6 100644 --- a/src/utils/adopter.js +++ b/src/utils/adopter.js @@ -17,7 +17,7 @@ export function makeInstance (element) { if (element instanceof Base) return element if (typeof element === 'object') { - return adopt(element) + return adopter(element) } if (element == null) { @@ -25,7 +25,7 @@ export function makeInstance (element) { } if (typeof element === 'string' && element.charAt(0) !== '<') { - return adopt(globals.document.querySelector(element)) + return adopter(globals.document.querySelector(element)) } var node = makeNode('svg') @@ -33,7 +33,7 @@ export function makeInstance (element) { // We can use firstChild here because we know, // that the first char is < and thus an element - element = adopt(node.firstChild) + element = adopter(node.firstChild) return element } @@ -65,6 +65,12 @@ export function adopt (node) { return new elements[className](node) } +let adopter = adopt + +export function mockAdopt(mock = adopt) { + adopter = mock +} + export function register (element, name = element.name, asRoot = false) { elements[name] = element if (asRoot) elements[root] = element @@ -117,9 +123,9 @@ export function extend (modules, methods, attrCheck) { } } -export function extendWithAttrCheck (...args) { - extend(...args, true) -} +// export function extendWithAttrCheck (...args) { +// extend(...args, true) +// } export function wrapWithAttrCheck (fn) { return function (...args) { |