github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/test/unit/specs/api/dom_spec.js (about) 1 /** 2 * We are not testing transition-related stuff here, 3 * those are tested in transition_spec.js. 4 */ 5 6 var Vue = require('src') 7 var _ = require('src/util') 8 9 describe('DOM API', function () { 10 var vm, vm2, parent, target, sibling, empty, spy 11 beforeEach(function () { 12 spy = jasmine.createSpy('dom') 13 parent = document.createElement('div') 14 target = document.createElement('div') 15 sibling = document.createElement('div') 16 empty = document.createElement('div') 17 parent.appendChild(target) 18 parent.appendChild(sibling) 19 var el = document.createElement('div') 20 vm = new Vue({ el: el }) 21 // fragment instance 22 var frag = document.createDocumentFragment() 23 frag.appendChild(document.createElement('p')) 24 frag.appendChild(document.createElement('span')) 25 vm2 = new Vue({ 26 el: frag 27 }) 28 }) 29 30 describe('$appendTo', function () { 31 it('normal instance', function () { 32 vm.$appendTo(parent, spy) 33 expect(parent.childNodes.length).toBe(3) 34 expect(parent.lastChild).toBe(vm.$el) 35 expect(spy.calls.count()).toBe(1) 36 }) 37 38 it('fragment instance', function () { 39 vm2.$appendTo(parent, spy) 40 expect(parent.childNodes.length).toBe(6) 41 expect(parent.childNodes[2]).toBe(vm2._fragmentStart) 42 expect(parent.childNodes[2]).toBe(vm2.$el) 43 expect(parent.childNodes[3].tagName).toBe('P') 44 expect(parent.childNodes[4].tagName).toBe('SPAN') 45 expect(parent.childNodes[5]).toBe(vm2._fragmentEnd) 46 expect(spy.calls.count()).toBe(1) 47 }) 48 }) 49 50 describe('$prependTo', function () { 51 it('normal instance', function () { 52 vm.$prependTo(parent, spy) 53 expect(parent.childNodes.length).toBe(3) 54 expect(parent.firstChild).toBe(vm.$el) 55 expect(spy.calls.count()).toBe(1) 56 vm.$prependTo(empty, spy) 57 expect(empty.childNodes.length).toBe(1) 58 expect(empty.firstChild).toBe(vm.$el) 59 expect(spy.calls.count()).toBe(2) 60 }) 61 62 it('fragment instance', function () { 63 vm2.$prependTo(parent, spy) 64 expect(parent.childNodes.length).toBe(6) 65 expect(parent.childNodes[0]).toBe(vm2._fragmentStart) 66 expect(parent.childNodes[0]).toBe(vm2.$el) 67 expect(parent.childNodes[1].tagName).toBe('P') 68 expect(parent.childNodes[2].tagName).toBe('SPAN') 69 expect(parent.childNodes[3]).toBe(vm2._fragmentEnd) 70 expect(spy.calls.count()).toBe(1) 71 // empty 72 vm2.$prependTo(empty, spy) 73 expect(empty.childNodes.length).toBe(4) 74 expect(empty.childNodes[0]).toBe(vm2._fragmentStart) 75 expect(empty.childNodes[0]).toBe(vm2.$el) 76 expect(empty.childNodes[1].tagName).toBe('P') 77 expect(empty.childNodes[2].tagName).toBe('SPAN') 78 expect(empty.childNodes[3]).toBe(vm2._fragmentEnd) 79 expect(spy.calls.count()).toBe(2) 80 }) 81 }) 82 83 describe('$before', function () { 84 it('normal instance', function () { 85 vm.$before(sibling, spy) 86 expect(parent.childNodes.length).toBe(3) 87 expect(parent.childNodes[1]).toBe(vm.$el) 88 expect(spy.calls.count()).toBe(1) 89 }) 90 91 it('fragment instance', function () { 92 vm2.$before(sibling, spy) 93 expect(parent.childNodes.length).toBe(6) 94 expect(parent.childNodes[1]).toBe(vm2._fragmentStart) 95 expect(parent.childNodes[1]).toBe(vm2.$el) 96 expect(parent.childNodes[2].tagName).toBe('P') 97 expect(parent.childNodes[3].tagName).toBe('SPAN') 98 expect(parent.childNodes[4]).toBe(vm2._fragmentEnd) 99 expect(spy.calls.count()).toBe(1) 100 }) 101 }) 102 103 describe('$after', function () { 104 it('normal instance', function () { 105 vm.$after(target, spy) 106 expect(parent.childNodes.length).toBe(3) 107 expect(parent.childNodes[1]).toBe(vm.$el) 108 expect(spy.calls.count()).toBe(1) 109 }) 110 111 it('normal instance no next sibling', function () { 112 vm.$after(sibling, spy) 113 expect(parent.childNodes.length).toBe(3) 114 expect(parent.lastChild).toBe(vm.$el) 115 expect(spy.calls.count()).toBe(1) 116 }) 117 118 it('fragment instance', function () { 119 vm2.$after(target, spy) 120 expect(parent.childNodes.length).toBe(6) 121 expect(parent.childNodes[1]).toBe(vm2._fragmentStart) 122 expect(parent.childNodes[1]).toBe(vm2.$el) 123 expect(parent.childNodes[2].tagName).toBe('P') 124 expect(parent.childNodes[3].tagName).toBe('SPAN') 125 expect(parent.childNodes[4]).toBe(vm2._fragmentEnd) 126 expect(spy.calls.count()).toBe(1) 127 }) 128 129 it('fragment instance no next sibling', function () { 130 vm2.$after(sibling, spy) 131 expect(parent.childNodes.length).toBe(6) 132 expect(parent.childNodes[2]).toBe(vm2._fragmentStart) 133 expect(parent.childNodes[2]).toBe(vm2.$el) 134 expect(parent.childNodes[3].tagName).toBe('P') 135 expect(parent.childNodes[4].tagName).toBe('SPAN') 136 expect(parent.childNodes[5]).toBe(vm2._fragmentEnd) 137 expect(spy.calls.count()).toBe(1) 138 }) 139 }) 140 141 describe('$remove', function () { 142 it('normal instance', function () { 143 vm.$before(sibling) 144 expect(parent.childNodes.length).toBe(3) 145 expect(parent.childNodes[1]).toBe(vm.$el) 146 vm.$remove(spy) 147 expect(parent.childNodes.length).toBe(2) 148 expect(parent.childNodes[0]).toBe(target) 149 expect(parent.childNodes[1]).toBe(sibling) 150 expect(spy.calls.count()).toBe(1) 151 }) 152 153 it('fragment instance', function () { 154 vm2.$before(sibling) 155 expect(parent.childNodes.length).toBe(6) 156 expect(parent.childNodes[1]).toBe(vm2._fragmentStart) 157 expect(parent.childNodes[1]).toBe(vm2.$el) 158 expect(parent.childNodes[2].tagName).toBe('P') 159 expect(parent.childNodes[3].tagName).toBe('SPAN') 160 expect(parent.childNodes[4]).toBe(vm2._fragmentEnd) 161 vm2.$remove(spy) 162 expect(parent.childNodes.length).toBe(2) 163 expect(parent.childNodes[0]).toBe(target) 164 expect(parent.childNodes[1]).toBe(sibling) 165 expect(spy.calls.count()).toBe(1) 166 }) 167 168 it('detached', function () { 169 vm.$remove(spy) 170 expect(spy.calls.count()).toBe(1) 171 }) 172 }) 173 174 describe('$nextTick', function () { 175 it('should work', function (done) { 176 var context 177 var called = false 178 vm.$nextTick(function () { 179 called = true 180 context = this 181 }) 182 expect(called).toBe(false) 183 _.nextTick(function () { 184 expect(called).toBe(true) 185 expect(context).toBe(vm) 186 done() 187 }) 188 }) 189 }) 190 })