api.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. describe('API', function () {
  2. var utils = require('vue/src/utils'),
  3. assets = require('vue/src/config').globalAssets,
  4. nextTick = utils.nextTick
  5. describe('config()', function () {
  6. var config = require('vue/src/config')
  7. it('should get', function () {
  8. assert.strictEqual(Vue.config('debug'), false)
  9. })
  10. it('should set', function () {
  11. Vue.config('test', 1)
  12. assert.strictEqual(config.test, 1)
  13. })
  14. describe('changing prefix', function () {
  15. before(function () {
  16. Vue.config({ prefix: 'test' })
  17. })
  18. after(function () {
  19. Vue.config({ prefix: 'v' })
  20. })
  21. it('should work', function () {
  22. var v = new Vue({
  23. template: '<span test-text="test"></span>',
  24. data: { test: 'helllllo' }
  25. })
  26. assert.strictEqual(v.$el.innerHTML, '<span>' + v.test + '</span>')
  27. })
  28. })
  29. describe('changing interpolation delimiters', function () {
  30. before(function () {
  31. Vue.config({ delimiters: ['[', ']'] })
  32. })
  33. after(function () {
  34. Vue.config({ delimiters: ['{', '}'] })
  35. })
  36. it('should work', function () {
  37. var v = new Vue({
  38. template: '<span>[[text]]</span><div>[[[html]]]</div>',
  39. data: {
  40. text: 'hello!!!',
  41. html: '<span><a>some raw html</a></span>'
  42. }
  43. })
  44. assert.strictEqual(v.$el.querySelector('span').innerHTML, v.text)
  45. assert.strictEqual(v.$el.querySelector('div').innerHTML, v.html + '<!--v-html-->')
  46. })
  47. })
  48. describe('skipping interpolation', function () {
  49. before(function () {
  50. Vue.config({ interpolate: false })
  51. })
  52. after(function () {
  53. Vue.config({ interpolate: true })
  54. })
  55. it('should work', function () {
  56. var raw = '<span class="{{text}}">{{text}}</span>'
  57. var v = new Vue({
  58. template: raw,
  59. data: {
  60. text: 'hello!!!'
  61. }
  62. })
  63. assert.strictEqual(v.$el.innerHTML, raw)
  64. })
  65. })
  66. })
  67. describe('require()', function () {
  68. it('should expose internal modules', function () {
  69. var c = Vue.require('config'),
  70. cc = require('vue/src/config')
  71. assert.strictEqual(c, cc)
  72. })
  73. })
  74. describe('use()', function () {
  75. it('should install a plugin via its install function', function () {
  76. var called = false
  77. Vue.use({
  78. install: function (vue) {
  79. called = true
  80. assert.strictEqual(vue, Vue)
  81. }
  82. })
  83. assert.ok(called)
  84. })
  85. it('should install a plugin if it’s a function itself', function () {
  86. var called = false
  87. Vue.use(function (vue) {
  88. called = true
  89. assert.strictEqual(vue, Vue)
  90. })
  91. assert.ok(called)
  92. })
  93. it('should pass any additional parameter', function () {
  94. var param1 = 'a',
  95. param2 = { b: 'c' }
  96. Vue.use(function (vue, p1, p2) {
  97. assert.strictEqual(p1, param1)
  98. assert.strictEqual(p2, param2)
  99. }, param1, param2)
  100. Vue.use({
  101. install: function (vue, p1, p2) {
  102. assert.strictEqual(p1, param1)
  103. assert.strictEqual(p2, param2)
  104. }
  105. }, param1, param2)
  106. })
  107. it('should properly set the value of this', function () {
  108. var plugin = {
  109. install: function () {
  110. assert.strictEqual(this, plugin)
  111. }
  112. }
  113. Vue.use(plugin)
  114. Vue.use(function () {
  115. assert.strictEqual(this, global)
  116. })
  117. })
  118. })
  119. describe('filter()', function () {
  120. var reverse = function (input) {
  121. return input.split('').reverse().join('')
  122. }
  123. it('should create custom filter', function () {
  124. var testId = 'filter-1',
  125. msg = '12345'
  126. Vue.filter('reverse', reverse)
  127. mock(testId, '{{ test | reverse }}')
  128. new Vue({
  129. el: '#' + testId,
  130. data: { test: msg }
  131. })
  132. assert.strictEqual(document.querySelector('#' + testId).innerHTML, '54321')
  133. })
  134. it('should return filter function if only one arg is given', function () {
  135. var f = Vue.filter('reverse')
  136. assert.strictEqual(f, reverse)
  137. })
  138. })
  139. describe('directive()', function () {
  140. var dirTest
  141. it('should create custom directive with update() function only', function () {
  142. var testId = 'directive-1',
  143. msg = 'wowow'
  144. Vue.directive('test', function (value) {
  145. this.el.setAttribute(testId, value + '123')
  146. })
  147. mock(testId, '<span v-test="test"></span>')
  148. new Vue({
  149. el: '#' + testId,
  150. data: { test: msg }
  151. })
  152. var el = document.querySelector('#' + testId + ' span')
  153. assert.strictEqual(el.getAttribute(testId), msg + '123')
  154. })
  155. it('should create custom directive with object', function () {
  156. var testId = 'directive-2',
  157. msg = 'wowaaaa?'
  158. dirTest = {
  159. bind: function (value) {
  160. this.el.setAttribute(testId + 'bind', value + 'bind')
  161. },
  162. update: function (value) {
  163. this.el.setAttribute(testId + 'update', value + 'update')
  164. },
  165. unbind: function () {
  166. this.el.removeAttribute(testId + 'bind')
  167. }
  168. }
  169. Vue.directive('test2', dirTest)
  170. mock(testId, '<span v-test2="test"></span>')
  171. var vm = new Vue({
  172. el: '#' + testId,
  173. data: { test: msg }
  174. }),
  175. el = document.querySelector('#' + testId + ' span')
  176. assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind() with value')
  177. assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update() with value')
  178. vm.$destroy() // assuming this works
  179. assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
  180. })
  181. it('should not create binding for literal or empty directives', function () {
  182. var literalCalled = false,
  183. emptyCalled = false
  184. Vue.directive('test-literal', {
  185. isLiteral: true,
  186. bind: function () {
  187. literalCalled = true
  188. assert.strictEqual(this.expression, 'hihi')
  189. assert.notOk(this.binding)
  190. },
  191. update: function () {}
  192. })
  193. Vue.directive('test-empty', {
  194. isEmpty: true,
  195. bind: function () {
  196. emptyCalled = true
  197. assert.notOk(this.expression)
  198. assert.notOk(this.binding)
  199. },
  200. update: function () {}
  201. })
  202. new Vue({
  203. template: '<div v-test-literal="hihi"></div><div v-test-empty="123"></div>'
  204. })
  205. assert.ok(literalCalled)
  206. assert.ok(emptyCalled)
  207. })
  208. it('should return directive object/fn if only one arg is given', function () {
  209. var dir = Vue.directive('test2')
  210. assert.strictEqual(dir, dirTest)
  211. })
  212. })
  213. describe('component()', function () {
  214. var testId = 'api-component-test',
  215. testId2 = testId + '2',
  216. opts = {
  217. className: 'hihi',
  218. data: { hi: 'ok' }
  219. },
  220. Test = Vue.extend(opts)
  221. it('should register a Component constructor', function () {
  222. Vue.component(testId, Test)
  223. assert.strictEqual(assets.components[testId], Test)
  224. })
  225. it('should also work with option objects', function () {
  226. Vue.component(testId2, opts)
  227. assert.ok(assets.components[testId2].prototype instanceof Vue)
  228. })
  229. it('should retrieve the VM if has only one arg', function () {
  230. assert.strictEqual(Vue.component(testId), Test)
  231. })
  232. it('should work with v-component', function () {
  233. mock(testId, '<div v-component="' + testId + '">{{hi}}</div>')
  234. var t = new Vue({ el: '#' + testId }),
  235. child = t.$el.querySelector('div')
  236. assert.strictEqual(child.className, 'hihi')
  237. assert.strictEqual(child.textContent, 'ok')
  238. mock(testId2, '<div v-component="' + testId2 + '">{{hi}}</div>')
  239. var t2 = new Vue({ el: '#' + testId2 }),
  240. child2 = t2.$el.querySelector('div')
  241. assert.strictEqual(child2.className, 'hihi')
  242. assert.strictEqual(child2.textContent, 'ok')
  243. })
  244. })
  245. describe('partial()', function () {
  246. var testId = 'api-partial-test',
  247. partial = '<div class="partial-test"><a>{{hi}}</a></div><span>hahaha</span>'
  248. it('should register the partial as a dom fragment', function () {
  249. Vue.partial(testId, partial)
  250. var converted = assets.partials[testId]
  251. assert.ok(converted instanceof window.DocumentFragment)
  252. assert.strictEqual(converted.querySelector('.partial-test a').innerHTML, '{{hi}}')
  253. assert.strictEqual(converted.querySelector('span').innerHTML, 'hahaha')
  254. })
  255. it('should retrieve the partial if has only one arg', function () {
  256. assert.strictEqual(assets.partials[testId], Vue.partial(testId))
  257. })
  258. it('should work with v-partial as a directive', function () {
  259. var testId = 'api-partial-direcitve'
  260. Vue.partial(testId, partial)
  261. mock(testId, '<div class="directive" v-partial="' + testId + '">hello</div>')
  262. var t = new Vue({
  263. el: '#' + testId,
  264. data: { hi: 'hohoho' }
  265. })
  266. assert.strictEqual(t.$el.querySelector('.directive .partial-test a').textContent, 'hohoho')
  267. assert.strictEqual(t.$el.querySelector('.directive span').innerHTML, 'hahaha')
  268. })
  269. it('should work with v-partial as an inline interpolation', function () {
  270. var testId = 'api-partial-inline'
  271. Vue.partial(testId, partial)
  272. mock(testId, '<div class="inline">{{>' + testId + '}}</div>')
  273. var t = new Vue({
  274. el: '#' + testId,
  275. data: { hi: 'hohoho' }
  276. })
  277. assert.strictEqual(t.$el.querySelector('.inline .partial-test a').textContent, 'hohoho')
  278. assert.strictEqual(t.$el.querySelector('.inline span').innerHTML, 'hahaha')
  279. })
  280. })
  281. describe('effect()', function () {
  282. var testId = 'api-effect-test',
  283. effect = {}
  284. it('should register a effect object', function () {
  285. Vue.effect(testId, effect)
  286. assert.strictEqual(assets.effects[testId], effect)
  287. })
  288. it('should retrieve the effect if has only one arg', function () {
  289. assert.strictEqual(Vue.effect(testId), effect)
  290. })
  291. it('should work with v-effect', function (done) {
  292. var enterCalled = false,
  293. leaveCalled = false
  294. Vue.effect('effect-api-test', {
  295. enter: function (el, done) {
  296. enterCalled = true
  297. done()
  298. },
  299. leave: function (el, done) {
  300. leaveCalled = true
  301. done()
  302. }
  303. })
  304. var t = new Vue({
  305. attributes: {
  306. 'v-show': 'show',
  307. 'v-effect': 'effect-api-test'
  308. },
  309. data: {
  310. show: false
  311. }
  312. })
  313. document.body.appendChild(t.$el)
  314. t.show = true
  315. nextTick(function () {
  316. assert.ok(enterCalled)
  317. assert.strictEqual(t.$el.style.display, '')
  318. t.show = false
  319. nextTick(function () {
  320. assert.ok(leaveCalled)
  321. assert.strictEqual(t.$el.style.display, 'none')
  322. t.$destroy()
  323. done()
  324. })
  325. })
  326. })
  327. })
  328. describe('extend()', function () {
  329. it('should return a subclass of Vue', function () {
  330. var Test = Vue.extend({})
  331. assert.ok(Test.prototype instanceof Vue)
  332. })
  333. it('should allow further extensions', function () {
  334. var Parent = Vue.extend({
  335. data: {
  336. test: 'hi'
  337. }
  338. })
  339. var Child = Parent.extend({
  340. data: {
  341. test2: 'ho',
  342. test3: {
  343. hi: 1
  344. }
  345. }
  346. })
  347. assert.strictEqual(Child.super, Parent)
  348. var child = new Child({
  349. data: {
  350. test3: {
  351. ho: 2
  352. }
  353. }
  354. })
  355. assert.strictEqual(child.test, 'hi')
  356. assert.strictEqual(child.test2, 'ho')
  357. // should overwrite past 1 level deep
  358. assert.strictEqual(child.test3.ho, 2)
  359. assert.notOk(child.test3.hi)
  360. })
  361. it('should allow subclasses to attach private assets', function () {
  362. var testId = 'sub-private'
  363. var Sub = Vue.extend({})
  364. Sub.component(testId, {})
  365. assert.strictEqual(Sub.options.components[testId].super, Vue)
  366. Sub.partial(testId, '123')
  367. assert.ok(Sub.options.partials[testId] instanceof window.DocumentFragment)
  368. var Sub2 = Vue.extend({})
  369. Sub2.component(testId, {})
  370. assert.notStrictEqual(Sub.options.components[testId], Sub2.options.components[testId])
  371. assert.notOk(Vue.options.components[testId])
  372. })
  373. it('should allow subclasses to use plugins', function () {
  374. var Sub = Vue.extend({})
  375. Sub.use(function (Sub) {
  376. Sub.directive('hello', {})
  377. })
  378. assert.ok(Sub.options.directives.hello)
  379. })
  380. describe('Options', function () {
  381. describe('methods', function () {
  382. it('should be mixed to the exteded VM\'s prototype', function () {
  383. var mixins = {
  384. c: function () {},
  385. d: function () {}
  386. }
  387. var Test = Vue.extend({ methods: mixins })
  388. for (var key in mixins) {
  389. assert.strictEqual(Test.prototype[key], mixins[key])
  390. }
  391. })
  392. })
  393. describe('data', function () {
  394. it('should be copied to each instance', function () {
  395. var testData = { a: 1 },
  396. Test = Vue.extend({
  397. data: {
  398. test: testData
  399. }
  400. })
  401. var t1 = new Test(),
  402. t2 = new Test()
  403. assert.ok(t1.hasOwnProperty('test'))
  404. assert.strictEqual(t1.test, testData)
  405. assert.ok(t2.hasOwnProperty('test'))
  406. assert.strictEqual(t2.test, testData)
  407. })
  408. })
  409. describe('lazy', function () {
  410. it('should make text input fields only trigger on change', function () {
  411. var Test = Vue.extend({
  412. template: '<input type="text" v-model="test">',
  413. lazy: true
  414. })
  415. var t = new Test({
  416. data: {
  417. test: 'hi'
  418. }
  419. })
  420. var input = t.$el.querySelector('input')
  421. input.value = 'hohoho'
  422. input.dispatchEvent(mockHTMLEvent('input'))
  423. assert.strictEqual(t.test, 'hi')
  424. input.dispatchEvent(mockHTMLEvent('change'))
  425. assert.strictEqual(t.test, 'hohoho')
  426. })
  427. })
  428. describe('replace', function () {
  429. it('should replace an in DOM node', function () {
  430. var testId = 'replace-test'
  431. mock(testId, '<div>ho</div>')
  432. var old = document.getElementById(testId),
  433. parent = old.parentNode
  434. var Test = Vue.extend({
  435. template: '<p>hi</p>',
  436. replace: true
  437. })
  438. var t = new Test({
  439. el: '#' + testId
  440. })
  441. assert.strictEqual(t.$el.tagName, 'P')
  442. assert.strictEqual(t.$el.textContent, 'hi')
  443. assert.strictEqual(t.$el.parentNode, parent)
  444. var now = document.getElementById(testId)
  445. assert.strictEqual(now, t.$el, 'should copy over attributes from replaced node')
  446. })
  447. it('should replace an off DOM Vue\'s $el', function () {
  448. var Test = Vue.extend({
  449. template: '<p>hi</p>',
  450. replace: true
  451. })
  452. var t = new Test()
  453. assert.strictEqual(t.$el.tagName, 'P')
  454. assert.strictEqual(t.$el.textContent, 'hi')
  455. })
  456. it('should not work if template has more than one child node', function () {
  457. var Test = Vue.extend({
  458. template: '<p>hi</p><p>ho</p>',
  459. replace: true
  460. })
  461. var t = new Test()
  462. assert.notStrictEqual(t.$el.tagName, 'P')
  463. assert.strictEqual(t.$el.innerHTML, '<p>hi</p><p>ho</p>')
  464. })
  465. })
  466. describe('DOM element options', function () {
  467. it('should not accept el as an extension option', function () {
  468. var el = document.createElement('div'),
  469. Test = Vue.extend({ el: el }),
  470. t = new Test()
  471. assert.notStrictEqual(t.$el, el)
  472. })
  473. it('should create el with options: tagName, id, className and attributes', function () {
  474. var Test = Vue.extend({
  475. tagName: 'p',
  476. id: 'extend-test',
  477. className: 'extend',
  478. attributes: {
  479. 'test': 'hi',
  480. 'v-text': 'hoho'
  481. },
  482. data: {
  483. hoho: 'what'
  484. }
  485. })
  486. var t = new Test()
  487. assert.strictEqual(t.$el.nodeName, 'P', 'tagName should match')
  488. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  489. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  490. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  491. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  492. })
  493. it('should ignore tagName when el is passed as an instance option', function () {
  494. var el = document.createElement('div'),
  495. Test = Vue.extend({
  496. tagName: 'p',
  497. id: 'extend-test',
  498. className: 'extend',
  499. attributes: {
  500. 'test': 'hi',
  501. 'v-text': 'hoho'
  502. },
  503. data: {
  504. hoho: 'what'
  505. }
  506. }),
  507. t = new Test({
  508. el: el
  509. })
  510. assert.strictEqual(t.$el, el, 'should use instance el')
  511. assert.notStrictEqual(t.$el.nodeName, 'P', 'tagName should NOT match')
  512. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  513. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  514. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  515. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  516. })
  517. })
  518. describe('template', function () {
  519. var raw = '<span>{{hello}}</span><a>haha</a>'
  520. it('should take direct string template and work', function () {
  521. var Test = Vue.extend({
  522. tagName: 'p',
  523. template: raw,
  524. data: {
  525. hello: 'Ahaha'
  526. }
  527. }),
  528. vm = new Test(),
  529. text1 = vm.$el.querySelector('span').textContent,
  530. text2 = vm.$el.querySelector('a').textContent
  531. assert.strictEqual(vm.$el.nodeName, 'P')
  532. assert.strictEqual(text1, 'Ahaha')
  533. assert.strictEqual(text2, 'haha')
  534. })
  535. it('should take a #id and work', function () {
  536. var testId = 'template-test',
  537. tpl = document.createElement('script')
  538. tpl.id = testId
  539. tpl.type = 'text/template'
  540. tpl.innerHTML = raw
  541. document.getElementById('test').appendChild(tpl)
  542. var Test = Vue.extend({
  543. template: '#' + testId,
  544. data: { hello: testId }
  545. })
  546. var t = new Test()
  547. assert.strictEqual(t.$el.querySelector('span').textContent, testId)
  548. })
  549. it('should be overwritable', function () {
  550. var Test = Vue.extend({
  551. template: '<div>this should not happen</div>'
  552. })
  553. var t = new Test({
  554. template: raw,
  555. data: {
  556. hello: 'overwritten!'
  557. }
  558. })
  559. assert.strictEqual(t.$el.querySelector('span').textContent, 'overwritten!')
  560. })
  561. })
  562. describe('paramAttributes', function () {
  563. it('should copy listed attributes into data and parse Numbers', function () {
  564. var Test = Vue.extend({
  565. template: '<div a="1" b="hello"></div>',
  566. replace: true,
  567. paramAttributes: ['a', 'b', 'c']
  568. })
  569. var v = new Test()
  570. assert.strictEqual(v.a, 1)
  571. assert.strictEqual(v.$data.a, 1)
  572. assert.strictEqual(v.b, 'hello')
  573. assert.strictEqual(v.$data.b, 'hello')
  574. assert.strictEqual(v.c, null)
  575. assert.strictEqual(v.$data.c, null)
  576. })
  577. it('should be able in bind data from parents', function (done) {
  578. var v = new Vue({
  579. template: '<div v-component="test" v-ref="child"></div>',
  580. data: {
  581. size: 123
  582. },
  583. components: {
  584. test: {
  585. paramAttributes: ['size'],
  586. template: '<div class="child" size="{{size}}"></div>'
  587. }
  588. }
  589. })
  590. var childAttr = v.$el.querySelector('.child').getAttribute('size')
  591. assert.strictEqual(childAttr, '123')
  592. v.size = 234
  593. nextTick(function () {
  594. var childAttr = v.$el.querySelector('.child').getAttribute('size')
  595. assert.strictEqual(childAttr, '234')
  596. assert.strictEqual(v.$.child.size, 234)
  597. done()
  598. })
  599. })
  600. })
  601. describe('parent', function () {
  602. var parent, child
  603. it('should allow child to access parent bindings', function () {
  604. parent = new Vue({
  605. data: {
  606. test: 'from parent'
  607. }
  608. })
  609. child = new Vue({
  610. parent: parent,
  611. template: '{{test}}'
  612. })
  613. assert.strictEqual(child.$el.textContent, 'from parent')
  614. })
  615. it('should allow event communication between parent and child', function () {
  616. var dispatched = false,
  617. broadcasted = false
  618. parent.$on('dispatch', function () {
  619. dispatched = true
  620. })
  621. child.$on('broadcast', function () {
  622. broadcasted = true
  623. })
  624. parent.$broadcast('broadcast')
  625. child.$dispatch('dispatch')
  626. assert.ok(dispatched)
  627. assert.ok(broadcasted)
  628. })
  629. it('should destroy the child when parent is destroyed', function () {
  630. parent.$destroy()
  631. assert.ok(child.$compiler.destroyed)
  632. })
  633. })
  634. describe('directives', function () {
  635. it('should allow the VM to use private directives', function (done) {
  636. var Test = Vue.extend({
  637. directives: {
  638. test: function (value) {
  639. this.el.innerHTML = value ? 'YES' : 'NO'
  640. }
  641. }
  642. })
  643. var t = new Test({
  644. attributes: {
  645. 'v-test': 'ok'
  646. },
  647. data: {
  648. ok: true
  649. }
  650. })
  651. assert.strictEqual(t.$el.innerHTML, 'YES')
  652. t.ok = false
  653. nextTick(function () {
  654. assert.strictEqual(t.$el.innerHTML, 'NO')
  655. done()
  656. })
  657. })
  658. })
  659. describe('filters', function () {
  660. it('should allow the VM to use private filters', function () {
  661. var Test = Vue.extend({
  662. filters: {
  663. test: function (value) {
  664. return value + '12345'
  665. }
  666. }
  667. })
  668. var t = new Test({
  669. template: '{{hi | test}}',
  670. data: {
  671. hi: 'hohoho'
  672. }
  673. })
  674. assert.strictEqual(t.$el.textContent, 'hohoho12345')
  675. })
  676. })
  677. describe('components', function () {
  678. it('should allow the VM to use private child VMs', function () {
  679. var Child = Vue.extend({
  680. data: {
  681. name: 'child'
  682. }
  683. })
  684. var Parent = Vue.extend({
  685. template: '<p>{{name}}</p><div v-component="child">{{name}}</div>',
  686. data: {
  687. name: 'dad'
  688. },
  689. components: {
  690. child: Child
  691. }
  692. })
  693. var p = new Parent()
  694. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  695. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  696. })
  697. it('should work with plain option object', function () {
  698. var Parent = Vue.extend({
  699. template: '<p>{{name}}</p><div v-component="child">{{name}}</div>',
  700. data: {
  701. name: 'dad'
  702. },
  703. components: {
  704. child: {
  705. data: {
  706. name: 'child'
  707. }
  708. }
  709. }
  710. })
  711. var p = new Parent()
  712. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  713. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  714. })
  715. })
  716. describe('partials', function () {
  717. it('should allow the VM to use private partials', function () {
  718. var Test = Vue.extend({
  719. attributes: {
  720. 'v-partial': 'test'
  721. },
  722. partials: {
  723. test: '<a>{{a}}</a><p>{{b}}</p>'
  724. },
  725. data: {
  726. a: 'hi',
  727. b: 'ho'
  728. }
  729. })
  730. var t = new Test()
  731. assert.strictEqual(t.$el.querySelector('a').textContent, 'hi')
  732. assert.strictEqual(t.$el.querySelector('p').textContent, 'ho')
  733. })
  734. })
  735. describe('effects', function () {
  736. it('should get called during effects', function (done) {
  737. var enterCalled = false,
  738. leaveCalled = false
  739. var t = new Vue({
  740. attributes: {
  741. 'v-show': 'show',
  742. 'v-effect': 'test'
  743. },
  744. effects: {
  745. test: {
  746. enter: function (el, done) {
  747. enterCalled = true
  748. done()
  749. },
  750. leave: function (el, done) {
  751. leaveCalled = true
  752. done()
  753. }
  754. }
  755. },
  756. data: {
  757. show: false
  758. }
  759. })
  760. document.body.appendChild(t.$el)
  761. t.show = true
  762. nextTick(function () {
  763. assert.ok(enterCalled)
  764. assert.strictEqual(t.$el.style.display, '')
  765. t.show = false
  766. nextTick(function () {
  767. assert.ok(leaveCalled)
  768. assert.strictEqual(t.$el.style.display, 'none')
  769. t.$destroy()
  770. done()
  771. })
  772. })
  773. })
  774. })
  775. describe('hooks', function () {
  776. describe('created', function () {
  777. it('should be called before compile', function () {
  778. var called = false,
  779. Test = Vue.extend({ created: function () {
  780. assert.ok(this.$options.ok)
  781. called = true
  782. }})
  783. new Test({ ok: true })
  784. assert.ok(called)
  785. })
  786. })
  787. describe('ready', function () {
  788. it('should be called after compile', function () {
  789. var called = false,
  790. hook = function () {
  791. assert.ok(this.$options.ok)
  792. assert.notOk(this.$compiler.init)
  793. called = true
  794. },
  795. Test = Vue.extend({ ready: hook })
  796. new Test({ ok: true })
  797. assert.ok(called)
  798. })
  799. })
  800. describe('beforeDestroy', function () {
  801. it('should be called before a vm is destroyed', function () {
  802. var called1 = false,
  803. called2 = false
  804. var Test = Vue.extend({
  805. beforeDestroy: function () {
  806. called1 = true
  807. }
  808. })
  809. var test = new Test()
  810. test.$on('hook:beforeDestroy', function () {
  811. called2 = true
  812. })
  813. test.$destroy()
  814. assert.ok(called1)
  815. assert.ok(called2)
  816. })
  817. })
  818. describe('afterDestroy', function () {
  819. it('should be called after a vm is destroyed', function () {
  820. var called1 = false, called2 = false,
  821. Test = Vue.extend({
  822. afterDestroy: function () {
  823. assert.notOk(this.$el.parentNode)
  824. called1 = true
  825. }
  826. })
  827. var test = new Test()
  828. document.body.appendChild(test.$el)
  829. test.$on('hook:afterDestroy', function () {
  830. called2 = true
  831. })
  832. test.$destroy()
  833. assert.ok(called1)
  834. assert.ok(called2)
  835. })
  836. })
  837. describe('attached', function () {
  838. it('should be called after enter view', function () {
  839. var called1 = false, called2 = false,
  840. test = new Vue({
  841. attached: function () {
  842. assert.strictEqual(this.$el.parentNode, document.getElementById('test'))
  843. called1 = true
  844. }
  845. })
  846. test.$on('hook:attached', function () {
  847. called2 = true
  848. })
  849. test.$appendTo('#test')
  850. assert.ok(called1)
  851. assert.ok(called2)
  852. })
  853. })
  854. describe('detached', function () {
  855. it('should be called after left view', function () {
  856. var called1 = false, called2 = false,
  857. test = new Vue({
  858. detached: function () {
  859. assert.strictEqual(this.$el.parentNode, null)
  860. called1 = true
  861. }
  862. })
  863. test.$on('hook:detached', function () {
  864. called2 = true
  865. })
  866. document.getElementById('test').appendChild(test.$el)
  867. test.$remove()
  868. assert.ok(called1)
  869. assert.ok(called2)
  870. })
  871. })
  872. describe('Hook inheritance', function () {
  873. it('should merge hooks with parent Class', function () {
  874. var called = []
  875. var Parent = Vue.extend({
  876. created: function () {
  877. called.push('parent')
  878. }
  879. })
  880. var Child = Parent.extend({
  881. created: function () {
  882. called.push('child')
  883. }
  884. })
  885. new Child({
  886. created: function () {
  887. called.push('instance')
  888. }
  889. })
  890. assert.deepEqual(called, ['parent', 'child', 'instance'])
  891. })
  892. })
  893. })
  894. })
  895. })
  896. })