Browse Source

tests for templateToFragment and convertPartials

Evan You 12 years ago
parent
commit
d191ce3062
2 changed files with 45 additions and 1 deletions
  1. 1 1
      src/utils.js
  2. 44 0
      test/unit/specs/utils.js

+ 1 - 1
src/utils.js

@@ -83,7 +83,7 @@ var utils = module.exports = {
      */
      */
     templateToFragment: function (template) {
     templateToFragment: function (template) {
         if (template.charAt(0) === '#') {
         if (template.charAt(0) === '#') {
-            var templateNode = document.querySelector(template)
+            var templateNode = document.getElementById(template.slice(1))
             if (!templateNode) return
             if (!templateNode) return
             template = templateNode.innerHTML
             template = templateNode.innerHTML
         }
         }

+ 44 - 0
test/unit/specs/utils.js

@@ -98,4 +98,48 @@ describe('UNIT: Utils', function () {
 
 
     })
     })
 
 
+    describe('templateToFragment', function () {
+        
+        it('should convert a string tempalte to a documentFragment', function () {
+            var template = '<div class="a">hi</div><p>ha</p>',
+                frag = utils.templateToFragment(template)
+            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)
+
+            var frag = utils.templateToFragment('#' + id)
+            assert.ok(frag instanceof window.DocumentFragment)
+            assert.equal(frag.querySelector('.a').textContent, 'hi')
+            assert.equal(frag.querySelector('p').textContent, 'ha')
+        })
+
+    })
+
+    describe('convertPartials', function () {
+        
+        it('should convert a hash object of strings to fragments', function () {
+            var partials = {
+                a: '#utils-template-to-fragment',
+                b: '<div class="a">hi</div><p>ha</p>'
+            }
+            utils.convertPartials(partials)
+            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')
+            }
+        })
+
+    })
+
 })
 })