2013-10-24 12:30:45 +08:00
|
|
|
describe('UNIT: Utils', function () {
|
2013-11-13 02:45:54 +08:00
|
|
|
|
|
|
|
|
var utils = require('seed/src/utils')
|
2013-10-24 12:30:45 +08:00
|
|
|
|
|
|
|
|
describe('hash', function () {
|
|
|
|
|
|
|
|
|
|
it('should return an Object with null prototype', function () {
|
|
|
|
|
var hash = utils.hash()
|
|
|
|
|
assert.strictEqual(Object.getPrototypeOf(hash), null)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
2013-11-15 02:12:34 +08:00
|
|
|
describe('attr', function () {
|
|
|
|
|
|
|
|
|
|
var el = document.createElement('div')
|
|
|
|
|
el.setAttribute('sd-transition-class', 'test')
|
|
|
|
|
|
|
|
|
|
it('should append the prefix and return the attribute value', function () {
|
|
|
|
|
var val = utils.attr(el, 'transition-class')
|
|
|
|
|
assert.strictEqual(val, 'test')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should remove the attribute', function () {
|
|
|
|
|
assert.notOk(el.hasAttribute('sd-transition-class'))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should work with different prefix', function () {
|
|
|
|
|
|
|
|
|
|
Seed.config({ prefix: 'test' })
|
|
|
|
|
|
|
|
|
|
var el = document.createElement('div')
|
|
|
|
|
el.setAttribute('test-transition-class', 'test')
|
|
|
|
|
var val = utils.attr(el, 'transition-class')
|
|
|
|
|
assert.strictEqual(val, 'test')
|
|
|
|
|
assert.notOk(el.hasAttribute('test-transition-class'))
|
|
|
|
|
|
|
|
|
|
Seed.config({ prefix: 'sd' })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
2013-10-24 12:30:45 +08:00
|
|
|
describe('defProtected', function () {
|
|
|
|
|
|
|
|
|
|
it('should define a protected property', function () {
|
|
|
|
|
var a = {}
|
|
|
|
|
utils.defProtected(a, 'test', 1)
|
|
|
|
|
|
2013-10-24 12:45:21 +08:00
|
|
|
var keys = []
|
2013-10-24 12:30:45 +08:00
|
|
|
for (var key in a) {
|
2013-10-24 12:45:21 +08:00
|
|
|
keys.push(key)
|
2013-10-24 12:30:45 +08:00
|
|
|
}
|
2013-10-24 12:45:21 +08:00
|
|
|
assert.strictEqual(keys.length, 0, 'inenumerable')
|
2013-10-24 12:30:45 +08:00
|
|
|
assert.strictEqual(JSON.stringify(a), '{}', 'unstringifiable')
|
|
|
|
|
|
|
|
|
|
a.test = 2
|
|
|
|
|
assert.strictEqual(a.test, 1, 'unconfigurable')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should take enumerable option', function () {
|
|
|
|
|
var a = {}
|
|
|
|
|
utils.defProtected(a, 'test', 1, true)
|
|
|
|
|
|
2013-10-24 12:45:21 +08:00
|
|
|
var keys = []
|
2013-10-24 12:30:45 +08:00
|
|
|
for (var key in a) {
|
2013-10-24 12:45:21 +08:00
|
|
|
keys.push(key)
|
2013-10-24 12:30:45 +08:00
|
|
|
}
|
2013-10-24 12:45:21 +08:00
|
|
|
assert.strictEqual(keys.length, 1, 'enumerable')
|
|
|
|
|
assert.strictEqual(keys[0], 'test')
|
2013-10-24 12:30:45 +08:00
|
|
|
assert.strictEqual(JSON.stringify(a), '{"test":1}', 'stringifiable')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('typeOf', function () {
|
|
|
|
|
|
|
|
|
|
it('should return correct type', function () {
|
|
|
|
|
var tof = utils.typeOf
|
|
|
|
|
assert.equal(tof({}), 'Object')
|
|
|
|
|
assert.equal(tof([]), 'Array')
|
|
|
|
|
assert.equal(tof(1), 'Number')
|
|
|
|
|
assert.equal(tof(''), 'String')
|
|
|
|
|
assert.equal(tof(true), 'Boolean')
|
2013-10-24 12:45:21 +08:00
|
|
|
// phantomjs weirdness
|
|
|
|
|
assert.ok(tof(null) === 'Null' || tof(null) === 'DOMWindow')
|
|
|
|
|
assert.ok(tof(undefined) === 'Undefined' || tof(undefined) === 'DOMWindow')
|
2013-10-24 12:30:45 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('toText', function () {
|
|
|
|
|
|
|
|
|
|
var txt = utils.toText
|
|
|
|
|
|
2013-10-26 06:22:22 +08:00
|
|
|
it('should do nothing for strings, numbers and booleans', function () {
|
2013-10-24 12:30:45 +08:00
|
|
|
assert.strictEqual(txt('hihi'), 'hihi')
|
|
|
|
|
assert.strictEqual(txt(123), 123)
|
2013-10-26 06:22:22 +08:00
|
|
|
assert.strictEqual(txt(true), true)
|
|
|
|
|
assert.strictEqual(txt(false), false)
|
2013-10-24 12:30:45 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should output empty string if value is not string or number', function () {
|
|
|
|
|
assert.strictEqual(txt({}), '')
|
|
|
|
|
assert.strictEqual(txt([]), '')
|
|
|
|
|
assert.strictEqual(txt(undefined), '')
|
|
|
|
|
assert.strictEqual(txt(null), '')
|
|
|
|
|
assert.strictEqual(txt(NaN), '')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('extend', function () {
|
|
|
|
|
|
|
|
|
|
it('should extend the obj with extension obj', function () {
|
|
|
|
|
var a = {a: 1}, b = {a: {}, b: 2}
|
|
|
|
|
utils.extend(a, b)
|
|
|
|
|
assert.strictEqual(a.a, b.a)
|
|
|
|
|
assert.strictEqual(a.b, b.b)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should respect the protective option', function () {
|
|
|
|
|
var a = {a: 1}, b = {a: {}, b: 2}
|
|
|
|
|
utils.extend(a, b, true)
|
|
|
|
|
assert.strictEqual(a.a, 1)
|
|
|
|
|
assert.strictEqual(a.b, b.b)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
2013-11-15 00:36:50 +08:00
|
|
|
describe('toFragment', function () {
|
2013-10-25 23:28:51 +08:00
|
|
|
|
|
|
|
|
it('should convert a string tempalte to a documentFragment', function () {
|
|
|
|
|
var template = '<div class="a">hi</div><p>ha</p>',
|
2013-11-15 00:36:50 +08:00
|
|
|
frag = utils.toFragment(template)
|
2013-10-25 23:28:51 +08:00
|
|
|
assert.ok(frag instanceof window.DocumentFragment)
|
|
|
|
|
assert.equal(frag.querySelector('.a').textContent, 'hi')
|
|
|
|
|
assert.equal(frag.querySelector('p').textContent, 'ha')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should also work if the string is an ID selector', function () {
|
|
|
|
|
var id = 'utils-template-to-fragment',
|
|
|
|
|
template = '<div class="a">hi</div><p>ha</p>',
|
|
|
|
|
el = document.createElement('template')
|
|
|
|
|
el.id = id
|
|
|
|
|
el.innerHTML = template
|
|
|
|
|
document.getElementById('test').appendChild(el)
|
|
|
|
|
|
2013-11-15 00:36:50 +08:00
|
|
|
var frag = utils.toFragment('#' + id)
|
2013-10-25 23:28:51 +08:00
|
|
|
assert.ok(frag instanceof window.DocumentFragment)
|
|
|
|
|
assert.equal(frag.querySelector('.a').textContent, 'hi')
|
|
|
|
|
assert.equal(frag.querySelector('p').textContent, 'ha')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
2013-11-15 00:36:50 +08:00
|
|
|
describe('toConstructor', function () {
|
2013-10-25 23:28:51 +08:00
|
|
|
|
2013-11-15 00:36:50 +08:00
|
|
|
it('should convert an non-VM object to a VM constructor', function () {
|
|
|
|
|
var a = { test: 1 },
|
|
|
|
|
A = utils.toConstructor(a)
|
|
|
|
|
assert.ok(A.prototype instanceof Seed)
|
|
|
|
|
assert.strictEqual(A.options, a)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should return the argument if it is already a consutructor', function () {
|
|
|
|
|
var A = utils.toConstructor(Seed)
|
|
|
|
|
assert.strictEqual(A, Seed)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('processOptions', function () {
|
|
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
|
partials: {
|
2013-10-25 23:28:51 +08:00
|
|
|
a: '#utils-template-to-fragment',
|
|
|
|
|
b: '<div class="a">hi</div><p>ha</p>'
|
2013-11-15 00:36:50 +08:00
|
|
|
},
|
|
|
|
|
components: {
|
|
|
|
|
a: { scope: { data: 1 } },
|
|
|
|
|
b: { scope: { data: 2 } }
|
|
|
|
|
},
|
|
|
|
|
template: '<a>{{hi}}</a>'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it('should convert string partials to fragment nodes', function () {
|
|
|
|
|
|
|
|
|
|
// call it here
|
|
|
|
|
utils.processOptions(options)
|
|
|
|
|
|
|
|
|
|
var partials = options.partials
|
2013-10-25 23:28:51 +08:00
|
|
|
for (var key in partials) {
|
|
|
|
|
var frag = partials[key]
|
|
|
|
|
assert.ok(frag instanceof window.DocumentFragment)
|
|
|
|
|
assert.equal(frag.querySelector('.a').textContent, 'hi')
|
|
|
|
|
assert.equal(frag.querySelector('p').textContent, 'ha')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2013-11-15 00:36:50 +08:00
|
|
|
it('should convert string template to fragment node', function () {
|
|
|
|
|
assert.ok(options.template instanceof window.DocumentFragment)
|
|
|
|
|
assert.equal(options.template.querySelector('a').textContent, '{{hi}}')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should convert plain object components to constructors', function () {
|
|
|
|
|
var components = options.components
|
|
|
|
|
assert.ok(components.a.prototype instanceof Seed)
|
|
|
|
|
assert.strictEqual(components.a.options.scope.data, 1)
|
|
|
|
|
assert.ok(components.b.prototype instanceof Seed)
|
|
|
|
|
assert.strictEqual(components.b.options.scope.data, 2)
|
|
|
|
|
})
|
|
|
|
|
|
2013-10-25 23:28:51 +08:00
|
|
|
})
|
|
|
|
|
|
2013-10-24 12:30:45 +08:00
|
|
|
})
|