describe('UNIT: API', function () { var utils = require('vue/src/utils'), nextTick = utils.nextTick describe('config()', function () { var config = require('vue/src/config') it('should work when changing prefix', function () { var testId = 'config-1' Vue.config({ prefix: 'test' }) mock(testId, '') new Vue({ el: '#' + testId, data: { test: testId } }) assert.strictEqual(document.querySelector('#' + testId + ' span').innerHTML, testId) }) it('should get', function () { assert.strictEqual(Vue.config('debug'), false) }) it('should set', function () { Vue.config('test', 1) assert.strictEqual(config.test, 1) }) after(function () { Vue.config({ prefix: 'v' }) }) }) describe('require()', function () { it('should expose internal modules', function () { var c = Vue.require('config'), cc = require('vue/src/config') assert.strictEqual(c, cc) }) }) describe('use()', function () { it('should install a plugin via its install function', function () { var called = false Vue.use({ install: function (vue) { called = true assert.strictEqual(vue, Vue) } }) assert.ok(called) }) it('should install a plugin if its a function itself', function () { var called = false Vue.use(function (vue) { called = true assert.strictEqual(vue, Vue) }) assert.ok(called) }) }) describe('filter()', function () { var reverse = function (input) { return input.split('').reverse().join('') } it('should create custom filter', function () { var testId = 'filter-1', msg = '12345' Vue.filter('reverse', reverse) mock(testId, '{{ test | reverse }}') new Vue({ el: '#' + testId, data: { test: msg } }) assert.strictEqual(document.querySelector('#' + testId).innerHTML, '54321') }) it('should return filter function if only one arg is given', function () { var f = Vue.filter('reverse') assert.strictEqual(f, reverse) }) }) describe('directive()', function () { var dirTest it('should create custom directive with update() function only', function () { var testId = 'directive-1', msg = 'wowow' Vue.directive('test', function (value) { this.el.setAttribute(testId, value + '123') }) mock(testId, '') new Vue({ el: '#' + testId, data: { test: msg } }) var el = document.querySelector('#' + testId + ' span') assert.strictEqual(el.getAttribute(testId), msg + '123') }) it('should create custom directive with object', function () { var testId = 'directive-2', msg = 'wowaaaa?' dirTest = { bind: function () { this.el.setAttribute(testId + 'bind', 'bind') }, update: function (value) { this.el.setAttribute(testId + 'update', value + 'update') }, unbind: function () { this.el.removeAttribute(testId + 'bind') } } Vue.directive('test2', dirTest) mock(testId, '') var vm = new Vue({ el: '#' + testId, data: { test: msg } }), el = document.querySelector('#' + testId + ' span') assert.strictEqual(el.getAttribute(testId + 'bind'), 'bind', 'should have called bind()') assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update()') vm.$destroy() // assuming this works assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()') }) it('should not bind directive if no update() is provided', function () { var called = false Vue.directive('test-literal', { bind: function () { called = true assert.strictEqual(this.expression, 'hihi') assert.notOk(this.binding) } }) new Vue({ template: '
' }) assert.ok(called) }) it('should return directive object/fn if only one arg is given', function () { var dir = Vue.directive('test2') assert.strictEqual(dir, dirTest) }) }) describe('component()', function () { var testId = 'api-component-test', testId2 = testId + '2', opts = { className: 'hihi', data: { hi: 'ok' } }, Test = Vue.extend(opts) it('should register a Component constructor', function () { Vue.component(testId, Test) assert.strictEqual(utils.components[testId], Test) }) it('should also work with option objects', function () { Vue.component(testId2, opts) assert.ok(utils.components[testId2].prototype instanceof Vue) }) it('should retrieve the VM if has only one arg', function () { assert.strictEqual(Vue.component(testId), Test) }) it('should work with v-component', function () { mock(testId, 'hi
', replace: true }) var t = new Test({ el: '#' + testId }) assert.strictEqual(t.$el.tagName, 'P') assert.strictEqual(t.$el.textContent, 'hi') assert.strictEqual(t.$el.parentNode, parent) var now = document.getElementById(testId) assert.strictEqual(now, null) }) it('should replace an off DOM Vue\'s $el', function () { var Test = Vue.extend({ template: 'hi
', replace: true }) var t = new Test() assert.strictEqual(t.$el.tagName, 'P') assert.strictEqual(t.$el.textContent, 'hi') }) it('should not work if template has more than one child node', function () { var Test = Vue.extend({ template: 'hi
ho
', replace: true }) var t = new Test() assert.notStrictEqual(t.$el.tagName, 'P') assert.strictEqual(t.$el.innerHTML, 'hi
ho
') }) }) describe('DOM element options', function () { it('should not accept el as an extension option', function () { var el = document.createElement('div'), Test = Vue.extend({ el: el }), t = new Test() assert.notStrictEqual(t.$el, el) }) it('should create el with options: tagName, id, className and attributes', function () { var Test = Vue.extend({ tagName: 'p', id: 'extend-test', className: 'extend', attributes: { 'test': 'hi', 'v-text': 'hoho' }, data: { hoho: 'what' } }) var t = new Test() assert.strictEqual(t.$el.nodeName, 'P', 'tagName should match') assert.strictEqual(t.$el.id, 'extend-test', 'id should match') assert.strictEqual(t.$el.className, 'extend', 'className should match') assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work') assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work') }) it('should ignore tagName when el is passed as an instance option', function () { var el = document.createElement('div'), Test = Vue.extend({ tagName: 'p', id: 'extend-test', className: 'extend', attributes: { 'test': 'hi', 'v-text': 'hoho' }, data: { hoho: 'what' } }), t = new Test({ el: el }) assert.strictEqual(t.$el, el, 'should use instance el') assert.notStrictEqual(t.$el.nodeName, 'P', 'tagName should NOT match') assert.strictEqual(t.$el.id, 'extend-test', 'id should match') assert.strictEqual(t.$el.className, 'extend', 'className should match') assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work') assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work') }) }) describe('template', function () { var raw = '{{hello}}haha' it('should take direct string template and work', function () { var Test = Vue.extend({ tagName: 'p', template: raw, data: { hello: 'Ahaha' } }), vm = new Test(), text1 = vm.$el.querySelector('span').textContent, text2 = vm.$el.querySelector('a').textContent assert.strictEqual(vm.$el.nodeName, 'P') assert.strictEqual(text1, 'Ahaha') assert.strictEqual(text2, 'haha') }) it('should take a #id and work', function () { var testId = 'template-test', tpl = document.createElement('script') tpl.id = testId tpl.type = 'text/template' tpl.innerHTML = raw document.getElementById('test').appendChild(tpl) var Test = Vue.extend({ template: '#' + testId, data: { hello: testId } }) var t = new Test() assert.strictEqual(t.$el.querySelector('span').textContent, testId) }) it('should be overwritable', function () { var Test = Vue.extend({ template: '{{name}}
{{name}}
{{b}}
' }, data: { a: 'hi', b: 'ho' } }) var t = new Test() assert.strictEqual(t.$el.querySelector('a').textContent, 'hi') assert.strictEqual(t.$el.querySelector('p').textContent, 'ho') }) }) describe('transitions', function () { it('should get called during transitions', function (done) { var enterCalled = false, leaveCalled = false var t = new Vue({ attributes: { 'v-show': 'show', 'v-transition': 'test' }, transitions: { test: { enter: function (el, done) { enterCalled = true done() }, leave: function (el, done) { leaveCalled = true done() } } }, data: { show: false } }) document.body.appendChild(t.$el) t.show = true nextTick(function () { assert.ok(enterCalled) assert.strictEqual(t.$el.style.display, '') t.show = false nextTick(function () { assert.ok(leaveCalled) assert.strictEqual(t.$el.style.display, 'none') t.$destroy() done() }) }) }) }) describe('hooks', function () { describe('created', function () { it('should be called before compile', function () { var called = false, Test = Vue.extend({ created: function (options) { assert.ok(options.ok) called = true }}) new Test({ ok: true }) assert.ok(called) }) }) describe('ready', function () { it('should be called after compile with options', function () { var called = false, hook = function (options) { assert.ok(options.ok) assert.notOk(this.$compiler.init) called = true }, Test = Vue.extend({ ready: hook }) new Test({ ok: true }) assert.ok(called) }) }) describe('beforeDestroy', function () { it('should be called before a vm is destroyed', function () { var called1 = false, called2 = false var Test = Vue.extend({ beforeDestroy: function () { called1 = true } }) var test = new Test() test.$on('hook:beforeDestroy', function () { called2 = true }) test.$destroy() assert.ok(called1) assert.ok(called2) }) }) describe('afterDestroy', function () { it('should be called after a vm is destroyed', function () { var called1 = false, called2 = false, Test = Vue.extend({ afterDestroy: function () { assert.notOk(this.$el.parentNode) called1 = true } }) var test = new Test() document.body.appendChild(test.$el) test.$on('hook:afterDestroy', function () { called2 = true }) test.$destroy() assert.ok(called1) assert.ok(called2) }) }) describe('enteredView', function () { it('should be called after enter view', function () { var called1 = false, called2 = false, test = new Vue({ enteredView: function () { assert.strictEqual(this.$el.parentNode, document.getElementById('test')) called1 = true } }) test.$on('hook:enteredView', function () { called2 = true }) test.$appendTo('#test') assert.ok(called1) assert.ok(called2) }) }) describe('leftView', function () { it('should be called after left view', function () { var called1 = false, called2 = false, test = new Vue({ leftView: function () { assert.strictEqual(this.$el.parentNode, null) called1 = true } }) test.$on('hook:leftView', function () { called2 = true }) document.getElementById('test').appendChild(test.$el) test.$remove() assert.ok(called1) assert.ok(called2) }) }) describe('Hook inheritance', function () { it('should merge hooks with parent Class', function () { var called = [] var Parent = Vue.extend({ created: function () { called.push('parent') } }) var Child = Parent.extend({ created: function () { called.push('child') } }) new Child({ created: function () { called.push('instance') } }) assert.deepEqual(called, ['parent', 'child', 'instance']) }) }) }) }) }) })