/* global define, describe, it, beforeEach, expect */ define(['update-dom'], function(updateDOM) { "use strict"; var n = function(s) { return s .toLowerCase() .replace(/\s+/g, ' ') .replace(/=([^"][^> ]*)/g, '="$1"'); }; describe('muuUpdateDom', function() { var target; beforeEach(function() { target = document.createElement('div'); }); it('adds new text nodes', function() { updateDOM(target, 'Hallo'); expect(target.innerHTML).to.equal('Hallo'); }); it('adds new elements', function() { target.innerHTML = 'hallo'; updateDOM(target, 'hallo world'); expect(n(target.innerHTML)).to.equal('hallo world'); }); it('removes existing elements', function() { target.innerHTML = 'hallo world'; updateDOM(target, 'hallo'); expect(n(target.innerHTML)).to.equal('hallo'); }); it('replaces elements by text nodes', function() { target.innerHTML = 'hallo'; updateDOM(target, 'hallo'); expect(n(target.innerHTML)).to.equal('hallo'); }); it('replaces elements by other elements', function() { target.innerHTML = 'hallo'; updateDOM(target, '
hallo
'); expect(n(target.innerHTML)).to.equal('
hallo
'); }); it('can replace and add elements in the same parent', function() { target.innerHTML = '1'; updateDOM(target, '
1
2
'); expect(n(target.innerHTML)).to.equal('
1
2
'); }); it('can replace more than one element in the same parent', function() { target.innerHTML = '1 2'; updateDOM(target, '
1
2
'); expect(n(target.innerHTML)).to.equal('
1
2
'); }); it('can replace and remove elements in the same parent', function() { target.innerHTML = '1
2
'; updateDOM(target, '
1
'); expect(n(target.innerHTML)).to.equal('
1
'); }); it('adds new attributes', function() { target.innerHTML = 'hallo'; updateDOM(target, 'hallo'); expect(n(target.innerHTML)).to.equal('hallo'); }); it('removes existing attributes', function() { target.innerHTML = 'hallo'; updateDOM(target, 'hallo'); expect(n(target.innerHTML)).to.equal('hallo'); }); it('preserves all classes prefixed with "muu-"', function() { target.innerHTML = 'hallo'; updateDOM(target, 'hallo'); expect(n(target.innerHTML)).to.equal('hallo'); }); it('preserves input value', function() { target.innerHTML = ''; target.querySelector('input').value = '1'; updateDOM(target, ''); expect(n(target.innerHTML)).to.contain('class="test"'); expect(target.querySelector('input').value).to.equal('1'); }); it('preserves input checked on checkbox', function() { target.innerHTML = ''; target.querySelector('input').checked = true; updateDOM(target, ''); expect(n(target.innerHTML)).to.contain('class="test"'); expect(target.querySelector('input').checked).to.be.ok(); }); it('does not change children of .muu-isolate', function() { target.innerHTML = '
haha
'; updateDOM(target, '
huhu
'); expect(n(target.innerHTML)).to.contain('
haha
'); }); it('does change attributes of .muu-isolate', function() { target.innerHTML = '
'; updateDOM(target, '
'); expect(n(target.innerHTML)).to.be('
'); }); }); });