api.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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 instances', function () {
  383. var methods = {
  384. c: function () {},
  385. d: function () {}
  386. }
  387. var Test = Vue.extend({ methods: methods })
  388. var t = new Test()
  389. assert.strictEqual(t.c, methods.c)
  390. assert.strictEqual(t.d, methods.d)
  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 and give access to all vm properties', function () {
  778. var called = false,
  779. Test = Vue.extend({ created: function () {
  780. assert.ok(this.$data)
  781. assert.ok(this.$el)
  782. assert.ok(this.$)
  783. assert.ok(this.$compiler)
  784. assert.ok(this.$root)
  785. assert.ok(this.$options.ok)
  786. called = true
  787. }})
  788. new Test({ ok: true })
  789. assert.ok(called)
  790. })
  791. })
  792. describe('ready', function () {
  793. it('should be called after compile', function () {
  794. var called = false,
  795. hook = function () {
  796. assert.ok(this.$options.ok)
  797. assert.notOk(this.$compiler.init)
  798. called = true
  799. },
  800. Test = Vue.extend({ ready: hook })
  801. new Test({ ok: true })
  802. assert.ok(called)
  803. })
  804. })
  805. describe('beforeDestroy', function () {
  806. it('should be called before a vm is destroyed', function () {
  807. var called1 = false,
  808. called2 = false
  809. var Test = Vue.extend({
  810. beforeDestroy: function () {
  811. called1 = true
  812. }
  813. })
  814. var test = new Test()
  815. test.$on('hook:beforeDestroy', function () {
  816. called2 = true
  817. })
  818. test.$destroy()
  819. assert.ok(called1)
  820. assert.ok(called2)
  821. })
  822. })
  823. describe('afterDestroy', function () {
  824. it('should be called after a vm is destroyed', function () {
  825. var called1 = false, called2 = false,
  826. Test = Vue.extend({
  827. afterDestroy: function () {
  828. assert.notOk(this.$el.parentNode)
  829. called1 = true
  830. }
  831. })
  832. var test = new Test()
  833. document.body.appendChild(test.$el)
  834. test.$on('hook:afterDestroy', function () {
  835. called2 = true
  836. })
  837. test.$destroy()
  838. assert.ok(called1)
  839. assert.ok(called2)
  840. })
  841. })
  842. describe('attached', function () {
  843. it('should be called after enter view', function () {
  844. var called1 = false, called2 = false,
  845. test = new Vue({
  846. attached: function () {
  847. assert.strictEqual(this.$el.parentNode, document.getElementById('test'))
  848. called1 = true
  849. }
  850. })
  851. test.$on('hook:attached', function () {
  852. called2 = true
  853. })
  854. test.$appendTo('#test')
  855. assert.ok(called1)
  856. assert.ok(called2)
  857. })
  858. })
  859. describe('detached', function () {
  860. it('should be called after left view', function () {
  861. var called1 = false, called2 = false,
  862. test = new Vue({
  863. detached: function () {
  864. assert.strictEqual(this.$el.parentNode, null)
  865. called1 = true
  866. }
  867. })
  868. test.$on('hook:detached', function () {
  869. called2 = true
  870. })
  871. document.getElementById('test').appendChild(test.$el)
  872. test.$remove()
  873. assert.ok(called1)
  874. assert.ok(called2)
  875. })
  876. })
  877. describe('Hook inheritance', function () {
  878. it('should merge hooks with parent Class', function () {
  879. var called = []
  880. var Parent = Vue.extend({
  881. created: function () {
  882. called.push('parent')
  883. }
  884. })
  885. var Child = Parent.extend({
  886. created: function () {
  887. called.push('child')
  888. }
  889. })
  890. new Child({
  891. created: function () {
  892. called.push('instance')
  893. }
  894. })
  895. assert.deepEqual(called, ['parent', 'child', 'instance'])
  896. })
  897. })
  898. })
  899. })
  900. })
  901. })