Kaynağa Gözat

test for DOM API

Evan You 11 yıl önce
ebeveyn
işleme
52bbdf36ea

+ 4 - 0
gruntfile.js

@@ -35,6 +35,10 @@ module.exports = function (grunt) {
       dev: {
         files: ['src/**/*.js'],
         tasks: ['dev']
+      },
+      test: {
+        files: ['test/unit/specs/**/*.js'],
+        tasks: ['build-test']
       }
     },
 

+ 11 - 12
src/api/dom.js

@@ -6,7 +6,7 @@ var transition = require('../transition')
  *
  * @param {Node} target
  * @param {Function} [cb]
- * @param {Boolean} [withTransition]
+ * @param {Boolean} [withTransition] - defaults to true
  */
 
 exports.$appendTo = function (target, cb, withTransition) {
@@ -21,7 +21,7 @@ exports.$appendTo = function (target, cb, withTransition) {
  *
  * @param {Node} target
  * @param {Function} [cb]
- * @param {Boolean} [withTransition]
+ * @param {Boolean} [withTransition] - defaults to true
  */
 
 exports.$prependTo = function (target, cb, withTransition) {
@@ -38,7 +38,7 @@ exports.$prependTo = function (target, cb, withTransition) {
  *
  * @param {Node} target
  * @param {Function} [cb]
- * @param {Boolean} [withTransition]
+ * @param {Boolean} [withTransition] - defaults to true
  */
 
 exports.$before = function (target, cb, withTransition) {
@@ -53,7 +53,7 @@ exports.$before = function (target, cb, withTransition) {
  *
  * @param {Node} target
  * @param {Function} [cb]
- * @param {Boolean} [withTransition]
+ * @param {Boolean} [withTransition] - defaults to true
  */
 
 exports.$after = function (target, cb, withTransition) {
@@ -69,19 +69,18 @@ exports.$after = function (target, cb, withTransition) {
  * Remove instance from DOM
  *
  * @param {Function} [cb]
- * @param {Boolean} [withTransition]
+ * @param {Boolean} [withTransition] - defaults to true
  */
 
 exports.$remove = function (cb, withTransition) {
-  var canRemove = this._isAttached && _.inDoc(this.$el)
-  if (!canRemove) {
-    if (cb) cb()
-    return
-  }
+  var inDoc = this._isAttached && _.inDoc(this.$el)
+  // if we are not in document, no need to check
+  // for transitions
+  if (!inDoc) withTransition = false
   var op
   var self = this
   var realCb = function () {
-    self._callHook('detached')
+    if (inDoc) self._callHook('detached')
     if (cb) cb()
   }
   if (
@@ -107,7 +106,7 @@ exports.$remove = function (cb, withTransition) {
  * @param {Element} target
  * @param {Function} op
  * @param {Function} [cb]
- * @param {Boolean} [withTransition]
+ * @param {Boolean} [withTransition] - defaults to true
  */
 
 function insert (vm, target, op, cb, withTransition) {

+ 11 - 0
src/util/dom.js

@@ -31,6 +31,17 @@ exports.attr = function (node, attr) {
   return val
 }
 
+/**
+ * Append child to target
+ *
+ * @param {Element} el
+ * @param {Element} target
+ */
+
+exports.append = function (el, target) {
+  target.appendChild(el)
+}
+
 /**
  * Insert el before target
  *

+ 145 - 0
test/unit/specs/api/dom_spec.js

@@ -0,0 +1,145 @@
+/**
+ * We are not testing transition-related stuff here,
+ * those are tested in transition_spec.js.
+ */
+
+var Vue = require('../../../../src/vue')
+var _ = require('../../../../src/util')
+
+if (_.inBrowser) {
+  describe('DOM API', function () {
+
+    var vm1, vm2, parent, target, sibling, empty
+    beforeEach(function () {
+      parent = document.createElement('div')
+      target = document.createElement('div')
+      sibling = document.createElement('div')
+      empty = document.createElement('div')
+      parent.appendChild(target)
+      parent.appendChild(sibling)
+      var el = document.createElement('div')
+      vm = new Vue({ el: el })
+      // block instance
+      var frag = document.createDocumentFragment()
+      frag.appendChild(document.createElement('p'))
+      frag.appendChild(document.createElement('span'))
+      vm2 = new Vue({
+        el: frag
+      })
+    })
+    
+    describe('$appendTo', function () {
+      
+      it('normal instance', function () {
+        vm.$appendTo(parent)
+        expect(parent.childNodes.length).toBe(3)
+        expect(parent.lastChild).toBe(vm.$el)
+      })
+
+      it('block instance', function () {
+        vm2.$appendTo(parent)
+        expect(parent.childNodes.length).toBe(6)
+        expect(parent.childNodes[2]).toBe(vm2.$el)
+        expect(parent.childNodes[3].tagName).toBe('P')
+        expect(parent.childNodes[4].tagName).toBe('SPAN')
+        expect(parent.lastChild).toBe(vm2._blockEnd)
+      })
+
+    })
+
+    describe('$prependTo', function () {
+      
+      it('normal instance', function () {
+        vm.$prependTo(parent)
+        expect(parent.childNodes.length).toBe(3)
+        expect(parent.firstChild).toBe(vm.$el)
+        vm.$prependTo(empty)
+        expect(empty.childNodes.length).toBe(1)
+        expect(empty.firstChild).toBe(vm.$el)
+      })
+
+      it('block instance', function () {
+        vm2.$prependTo(parent)
+        expect(parent.childNodes.length).toBe(6)
+        expect(parent.childNodes[0]).toBe(vm2.$el)
+        expect(parent.childNodes[1].tagName).toBe('P')
+        expect(parent.childNodes[2].tagName).toBe('SPAN')
+        expect(parent.childNodes[3]).toBe(vm2._blockEnd)
+        // empty
+        vm2.$prependTo(empty)
+        expect(empty.childNodes.length).toBe(4)
+        expect(empty.childNodes[0]).toBe(vm2.$el)
+        expect(empty.childNodes[1].tagName).toBe('P')
+        expect(empty.childNodes[2].tagName).toBe('SPAN')
+        expect(empty.childNodes[3]).toBe(vm2._blockEnd)
+      })
+
+    })
+
+    describe('$before', function () {
+      
+      it('normal instance', function () {
+        vm.$before(sibling)
+        expect(parent.childNodes.length).toBe(3)
+        expect(parent.childNodes[1]).toBe(vm.$el)
+      })
+
+      it('block instance', function () {
+        vm2.$before(sibling)
+        expect(parent.childNodes.length).toBe(6)
+        expect(parent.childNodes[1]).toBe(vm2.$el)
+        expect(parent.childNodes[2].tagName).toBe('P')
+        expect(parent.childNodes[3].tagName).toBe('SPAN')
+        expect(parent.childNodes[4]).toBe(vm2._blockEnd)
+      })
+
+    })
+
+    describe('$after', function () {
+      
+      it('normal instance', function () {
+        vm.$after(target)
+        expect(parent.childNodes.length).toBe(3)
+        expect(parent.childNodes[1]).toBe(vm.$el)
+      })
+
+      it('block instance', function () {
+        vm2.$after(target)
+        expect(parent.childNodes.length).toBe(6)
+        expect(parent.childNodes[1]).toBe(vm2.$el)
+        expect(parent.childNodes[2].tagName).toBe('P')
+        expect(parent.childNodes[3].tagName).toBe('SPAN')
+        expect(parent.childNodes[4]).toBe(vm2._blockEnd)
+      })
+
+    })
+
+    describe('$remove', function () {
+      
+      it('normal instance', function () {
+        vm.$before(sibling)
+        expect(parent.childNodes.length).toBe(3)
+        expect(parent.childNodes[1]).toBe(vm.$el)
+        vm.$remove()
+        expect(parent.childNodes.length).toBe(2)
+        expect(parent.childNodes[0]).toBe(target)
+        expect(parent.childNodes[1]).toBe(sibling)
+      })
+
+      it('block instance', function () {
+        vm2.$before(sibling)
+        expect(parent.childNodes.length).toBe(6)
+        expect(parent.childNodes[1]).toBe(vm2.$el)
+        expect(parent.childNodes[2].tagName).toBe('P')
+        expect(parent.childNodes[3].tagName).toBe('SPAN')
+        expect(parent.childNodes[4]).toBe(vm2._blockEnd)
+        vm2.$remove()
+        expect(parent.childNodes.length).toBe(2)
+        expect(parent.childNodes[0]).toBe(target)
+        expect(parent.childNodes[1]).toBe(sibling)
+      })
+
+    })
+
+  })
+}

+ 1 - 1
test/unit/specs/parse/path_spec.js

@@ -22,7 +22,7 @@ function pathMatch (a, b) {
   return true
 }
 
-describe('Path', function () {
+describe('Path Parser', function () {
   
   it('parse', function () {
     assertPath('', [])

+ 6 - 0
test/unit/specs/util/dom_spec.js

@@ -58,6 +58,12 @@ if (_.inBrowser) {
       expect(parent.childNodes.length).toBe(0)
     })
 
+    it('append', function () {
+      _.append(target, parent)
+      expect(target.parentNode).toBe(parent)
+      expect(parent.lastChild).toBe(target)
+    })
+
     it('prepend', function () {
       _.prepend(target, parent)
       expect(target.parentNode).toBe(parent)