Skip to content

Commit

Permalink
add tests for all exports of adopter
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzyma committed Nov 18, 2018
1 parent c0052b7 commit 0cc8ab7
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 9 deletions.
1 change: 1 addition & 0 deletions spec/SpecRunnerEs6.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
5 changes: 3 additions & 2 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
161 changes: 161 additions & 0 deletions spec/spec/utils/adopter.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
2 changes: 1 addition & 1 deletion src/elements/Dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
makeInstance,
register
} from '../utils/adopter.js'
import { find } from '../modules/core/selector'
import { find } from '../modules/core/selector.js'
import { globals } from '../utils/window.js'
import { map } from '../utils/utils.js'
import { ns } from '../modules/core/namespaces.js'
Expand Down
18 changes: 12 additions & 6 deletions src/utils/adopter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ export function makeInstance (element) {
if (element instanceof Base) return element

if (typeof element === 'object') {
return adopt(element)
return adopter(element)
}

if (element == null) {
return new elements[root]()
}

if (typeof element === 'string' && element.charAt(0) !== '<') {
return adopt(globals.document.querySelector(element))
return adopter(globals.document.querySelector(element))
}

var node = makeNode('svg')
node.innerHTML = 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
}
Expand Down Expand Up @@ -71,6 +71,12 @@ export function adopt (node) {
return element
}

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
Expand Down Expand Up @@ -123,9 +129,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) {
Expand Down

0 comments on commit 0cc8ab7

Please sign in to comment.