github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/test/unit/specs/util/dom_spec.js (about)

     1  var _ = require('src/util')
     2  
     3  describe('Util - DOM', function () {
     4    var parent, child, target
     5  
     6    function div () {
     7      return document.createElement('div')
     8    }
     9  
    10    beforeEach(function () {
    11      parent = div()
    12      child = div()
    13      target = div()
    14      parent.appendChild(child)
    15    })
    16  
    17    it('inDoc', function () {
    18      expect(_.inDoc(target)).toBe(false)
    19      document.body.appendChild(target)
    20      expect(_.inDoc(target)).toBe(true)
    21      document.body.removeChild(target)
    22      expect(_.inDoc(target)).toBe(false)
    23    })
    24  
    25    it('inDoc (iframe)', function (done) {
    26      var f = document.createElement('iframe')
    27      f.onload = function () {
    28        f.contentWindow.document.body.appendChild(target)
    29        expect(_.inDoc(target)).toBe(true)
    30        document.body.removeChild(f)
    31        done()
    32      }
    33      document.body.appendChild(f)
    34      f.src = 'about:blank'
    35    })
    36  
    37    it('getAttr', function () {
    38      target.setAttribute('v-test', 'ok')
    39      var val = _.getAttr(target, 'v-test')
    40      expect(val).toBe('ok')
    41      expect(target.hasAttribute('v-test')).toBe(false)
    42    })
    43  
    44    it('before', function () {
    45      _.before(target, child)
    46      expect(target.parentNode).toBe(parent)
    47      expect(target.nextSibling).toBe(child)
    48    })
    49  
    50    it('after', function () {
    51      _.after(target, child)
    52      expect(target.parentNode).toBe(parent)
    53      expect(child.nextSibling).toBe(target)
    54    })
    55  
    56    it('after with sibling', function () {
    57      var sibling = div()
    58      parent.appendChild(sibling)
    59      _.after(target, child)
    60      expect(target.parentNode).toBe(parent)
    61      expect(child.nextSibling).toBe(target)
    62    })
    63  
    64    it('remove', function () {
    65      _.remove(child)
    66      expect(child.parentNode).toBeNull()
    67      expect(parent.childNodes.length).toBe(0)
    68    })
    69  
    70    it('prepend', function () {
    71      _.prepend(target, parent)
    72      expect(target.parentNode).toBe(parent)
    73      expect(parent.firstChild).toBe(target)
    74    })
    75  
    76    it('prepend to empty node', function () {
    77      parent.removeChild(child)
    78      _.prepend(target, parent)
    79      expect(target.parentNode).toBe(parent)
    80      expect(parent.firstChild).toBe(target)
    81    })
    82  
    83    it('replace', function () {
    84      _.replace(child, target)
    85      expect(parent.childNodes.length).toBe(1)
    86      expect(parent.firstChild).toBe(target)
    87    })
    88  
    89    it('on/off', function () {
    90      // IE requires element to be in document to fire events
    91      document.body.appendChild(target)
    92      var spy = jasmine.createSpy()
    93      _.on(target, 'click', spy)
    94      var e = document.createEvent('HTMLEvents')
    95      e.initEvent('click', true, true)
    96      target.dispatchEvent(e)
    97      expect(spy.calls.count()).toBe(1)
    98      expect(spy).toHaveBeenCalledWith(e)
    99      _.off(target, 'click', spy)
   100      target.dispatchEvent(e)
   101      expect(spy.calls.count()).toBe(1)
   102      document.body.removeChild(target)
   103    })
   104  
   105    it('addClass/removeClass', function () {
   106      var el = document.createElement('div')
   107      el.className = 'aa bb cc'
   108      _.removeClass(el, 'bb')
   109      expect(el.className).toBe('aa cc')
   110      _.removeClass(el, 'aa')
   111      expect(el.className).toBe('cc')
   112      _.addClass(el, 'bb')
   113      expect(el.className).toBe('cc bb')
   114      _.addClass(el, 'bb')
   115      expect(el.className).toBe('cc bb')
   116    })
   117  
   118    it('addClass/removeClass for SVG/IE9', function () {
   119      var el = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
   120      el.setAttribute('class', 'aa bb cc')
   121      _.removeClass(el, 'bb')
   122      expect(el.getAttribute('class')).toBe('aa cc')
   123      _.removeClass(el, 'aa')
   124      expect(el.getAttribute('class')).toBe('cc')
   125      _.addClass(el, 'bb')
   126      expect(el.getAttribute('class')).toBe('cc bb')
   127      _.addClass(el, 'bb')
   128      expect(el.getAttribute('class')).toBe('cc bb')
   129    })
   130  
   131    it('getOuterHTML for SVG', function () {
   132      var el = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
   133      el.setAttribute('class', 'aa bb cc')
   134      var html = _.getOuterHTML(el)
   135      var re = /<circle (xmlns="http:\/\/www\.w3\.org\/2000\/svg"\s)?class="aa bb cc"(\s?\/>|><\/circle>)/
   136      expect(re.test(html)).toBe(true)
   137    })
   138  })