var transclude = require('../../../../src/compile/transclude')
var _ = require('../../../../src/util')
if (_.inBrowser) {
describe('Transclude', function () {
var el, options
beforeEach(function () {
el = document.createElement('div')
options = {}
spyOn(_, 'warn')
})
it('normal', function () {
var res = transclude(el, options)
expect(res).toBe(el)
})
it('template', function () {
options.template = '{{hi}}'
var res = transclude(el, options)
expect(res).toBe(el)
expect(res.innerHTML).toBe('{{hi}}')
})
it('template invalid', function () {
options.template = '#non-existent-stuff'
var res = transclude(el, options)
expect(res).toBeUndefined()
expect(_.warn).toHaveBeenCalled()
})
it('template replace', function () {
el.className = 'hello'
options.template = '
{{hi}}
'
options.replace = true
var res = transclude(el, options)
expect(res).not.toBe(el)
expect(res.tagName).toBe('DIV')
expect(res.className).toBe('hello')
expect(res.innerHTML).toBe('{{hi}}')
})
it('block instance', function () {
var frag = document.createDocumentFragment()
frag.appendChild(el)
var res = transclude(frag, options)
expect(res).toBe(frag)
expect(res.childNodes.length).toBe(3)
expect(res.firstChild.nodeType).toBe(8)
expect(res.lastChild.nodeType).toBe(8)
expect(res.childNodes[1]).toBe(el)
})
it('template element', function () {
var tpl = document.createElement('template')
tpl.innerHTML = '123
'
var res = transclude(tpl, options)
expect(res.childNodes.length).toBe(3)
expect(res.firstChild.nodeType).toBe(8)
expect(res.lastChild.nodeType).toBe(8)
expect(res.childNodes[1].textContent).toBe('123')
})
it('content transclusion', function () {
el.innerHTML = 'hi
'
options.template = '
'
var res = transclude(el, options)
expect(res.firstChild.tagName).toBe('DIV')
expect(res.firstChild.firstChild.tagName).toBe('P')
expect(res.firstChild.firstChild.textContent).toBe('hi')
})
it('fallback content', function () {
options.template = 'fallback
'
var res = transclude(el, options)
expect(res.firstChild.tagName).toBe('P')
expect(res.firstChild.textContent).toBe('fallback')
})
it('content transclusion with replace', function () {
el.innerHTML = 'hi
'
options.template = ''
options.replace = true
var res = transclude(el, options)
expect(res).not.toBe(el)
expect(res.firstChild.tagName).toBe('DIV')
expect(res.firstChild.firstChild.tagName).toBe('P')
expect(res.firstChild.firstChild.textContent).toBe('hi')
})
it('block instance content transclusion', function () {
el.innerHTML = 'hi
ho'
options.template = ''
var res = transclude(el, options)
expect(res.firstChild.tagName).toBe('DIV')
expect(res.childNodes[1].tagName).toBe('P')
expect(res.childNodes[2].tagName).toBe('SPAN')
})
})
}