api.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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.binding)
  198. },
  199. update: function () {}
  200. })
  201. new Vue({
  202. template: '<div v-test-literal="hihi"></div><div v-test-empty="123"></div>'
  203. })
  204. assert.ok(literalCalled)
  205. assert.ok(emptyCalled)
  206. })
  207. it('should return directive object/fn if only one arg is given', function () {
  208. var dir = Vue.directive('test2')
  209. assert.strictEqual(dir, dirTest)
  210. })
  211. })
  212. describe('component()', function () {
  213. var testId = 'api-component-test',
  214. testId2 = testId + '2',
  215. opts = {
  216. className: 'hihi',
  217. data: { hi: 'ok' }
  218. },
  219. Test = Vue.extend(opts)
  220. it('should register a Component constructor', function () {
  221. Vue.component(testId, Test)
  222. assert.strictEqual(assets.components[testId], Test)
  223. })
  224. it('should also work with option objects', function () {
  225. Vue.component(testId2, opts)
  226. assert.ok(assets.components[testId2].prototype instanceof Vue)
  227. })
  228. it('should retrieve the VM if has only one arg', function () {
  229. assert.strictEqual(Vue.component(testId), Test)
  230. })
  231. it('should work with v-component', function () {
  232. mock(testId, '<div v-component="' + testId + '">{{hi}}</div>')
  233. var t = new Vue({ el: '#' + testId }),
  234. child = t.$el.querySelector('div')
  235. assert.strictEqual(child.className, 'hihi')
  236. assert.strictEqual(child.textContent, 'ok')
  237. mock(testId2, '<div v-component="' + testId2 + '">{{hi}}</div>')
  238. var t2 = new Vue({ el: '#' + testId2 }),
  239. child2 = t2.$el.querySelector('div')
  240. assert.strictEqual(child2.className, 'hihi')
  241. assert.strictEqual(child2.textContent, 'ok')
  242. })
  243. })
  244. describe('partial()', function () {
  245. var testId = 'api-partial-test',
  246. partial = '<div class="partial-test"><a>{{hi}}</a></div><span>hahaha</span>'
  247. it('should register the partial as a dom fragment', function () {
  248. Vue.partial(testId, partial)
  249. var converted = assets.partials[testId]
  250. assert.ok(converted instanceof window.DocumentFragment)
  251. assert.strictEqual(converted.querySelector('.partial-test a').innerHTML, '{{hi}}')
  252. assert.strictEqual(converted.querySelector('span').innerHTML, 'hahaha')
  253. })
  254. it('should retrieve the partial if has only one arg', function () {
  255. assert.strictEqual(assets.partials[testId], Vue.partial(testId))
  256. })
  257. it('should work with v-partial as a directive', function () {
  258. var testId = 'api-partial-direcitve'
  259. Vue.partial(testId, partial)
  260. mock(testId, '<div class="directive" v-partial="' + testId + '">hello</div>')
  261. var t = new Vue({
  262. el: '#' + testId,
  263. data: { hi: 'hohoho' }
  264. })
  265. assert.strictEqual(t.$el.querySelector('.directive .partial-test a').textContent, 'hohoho')
  266. assert.strictEqual(t.$el.querySelector('.directive span').innerHTML, 'hahaha')
  267. })
  268. it('should work with v-partial as an inline interpolation', function () {
  269. var testId = 'api-partial-inline'
  270. Vue.partial(testId, partial)
  271. mock(testId, '<div class="inline">{{>' + testId + '}}</div>')
  272. var t = new Vue({
  273. el: '#' + testId,
  274. data: { hi: 'hohoho' }
  275. })
  276. assert.strictEqual(t.$el.querySelector('.inline .partial-test a').textContent, 'hohoho')
  277. assert.strictEqual(t.$el.querySelector('.inline span').innerHTML, 'hahaha')
  278. })
  279. })
  280. describe('effect()', function () {
  281. var testId = 'api-effect-test',
  282. effect = {}
  283. it('should register a effect object', function () {
  284. Vue.effect(testId, effect)
  285. assert.strictEqual(assets.effects[testId], effect)
  286. })
  287. it('should retrieve the effect if has only one arg', function () {
  288. assert.strictEqual(Vue.effect(testId), effect)
  289. })
  290. it('should work with v-effect', function (done) {
  291. var enterCalled = false,
  292. leaveCalled = false
  293. Vue.effect('effect-api-test', {
  294. enter: function (el, done) {
  295. enterCalled = true
  296. done()
  297. },
  298. leave: function (el, done) {
  299. leaveCalled = true
  300. done()
  301. }
  302. })
  303. var t = new Vue({
  304. attributes: {
  305. 'v-show': 'show',
  306. 'v-effect': 'effect-api-test'
  307. },
  308. data: {
  309. show: false
  310. }
  311. })
  312. document.body.appendChild(t.$el)
  313. t.show = true
  314. nextTick(function () {
  315. assert.ok(enterCalled)
  316. assert.strictEqual(t.$el.style.display, '')
  317. t.show = false
  318. nextTick(function () {
  319. assert.ok(leaveCalled)
  320. assert.strictEqual(t.$el.style.display, 'none')
  321. t.$destroy()
  322. done()
  323. })
  324. })
  325. })
  326. })
  327. describe('extend()', function () {
  328. it('should return a subclass of Vue', function () {
  329. var Test = Vue.extend({})
  330. assert.ok(Test.prototype instanceof Vue)
  331. })
  332. it('should allow further extensions', function () {
  333. var Parent = Vue.extend({
  334. data: {
  335. test: 'hi'
  336. }
  337. })
  338. var Child = Parent.extend({
  339. data: {
  340. test2: 'ho',
  341. test3: {
  342. hi: 1
  343. }
  344. }
  345. })
  346. assert.strictEqual(Child.super, Parent)
  347. var child = new Child({
  348. data: {
  349. test3: {
  350. ho: 2
  351. }
  352. }
  353. })
  354. assert.strictEqual(child.test, 'hi')
  355. assert.strictEqual(child.test2, 'ho')
  356. // should overwrite past 1 level deep
  357. assert.strictEqual(child.test3.ho, 2)
  358. assert.notOk(child.test3.hi)
  359. })
  360. it('should allow subclasses to attach private assets', function () {
  361. var testId = 'sub-private'
  362. var Sub = Vue.extend({})
  363. Sub.component(testId, {})
  364. assert.strictEqual(Sub.options.components[testId].super, Vue)
  365. Sub.partial(testId, '123')
  366. assert.ok(Sub.options.partials[testId] instanceof window.DocumentFragment)
  367. var Sub2 = Vue.extend({})
  368. Sub2.component(testId, {})
  369. assert.notStrictEqual(Sub.options.components[testId], Sub2.options.components[testId])
  370. assert.notOk(Vue.options.components[testId])
  371. })
  372. it('should allow subclasses to use plugins', function () {
  373. var Sub = Vue.extend({})
  374. Sub.use(function (Sub) {
  375. Sub.directive('hello', {})
  376. })
  377. assert.ok(Sub.options.directives.hello)
  378. })
  379. describe('Options', function () {
  380. describe('methods', function () {
  381. it('should be mixed to the exteded VM\'s instances', function () {
  382. var methods = {
  383. c: function () {},
  384. d: function () {}
  385. }
  386. var Test = Vue.extend({ methods: methods })
  387. var t = new Test()
  388. assert.strictEqual(t.c, methods.c)
  389. assert.strictEqual(t.d, methods.d)
  390. })
  391. })
  392. describe('data', function () {
  393. it('should be copied to each instance', function () {
  394. var testData = { a: 1 },
  395. Test = Vue.extend({
  396. data: {
  397. test: testData
  398. }
  399. })
  400. var t1 = new Test(),
  401. t2 = new Test()
  402. assert.ok(t1.hasOwnProperty('test'))
  403. assert.strictEqual(t1.test, testData)
  404. assert.ok(t2.hasOwnProperty('test'))
  405. assert.strictEqual(t2.test, testData)
  406. })
  407. })
  408. describe('lazy', function () {
  409. it('should make text input fields only trigger on change', function () {
  410. var Test = Vue.extend({
  411. template: '<input type="text" v-model="test">',
  412. lazy: true
  413. })
  414. var t = new Test({
  415. data: {
  416. test: 'hi'
  417. }
  418. })
  419. var input = t.$el.querySelector('input')
  420. input.value = 'hohoho'
  421. input.dispatchEvent(mockHTMLEvent('input'))
  422. assert.strictEqual(t.test, 'hi')
  423. input.dispatchEvent(mockHTMLEvent('change'))
  424. assert.strictEqual(t.test, 'hohoho')
  425. })
  426. })
  427. describe('replace', function () {
  428. it('should replace an in DOM node', function () {
  429. var testId = 'replace-test'
  430. mock(testId, '<div>ho</div>')
  431. var old = document.getElementById(testId),
  432. parent = old.parentNode
  433. var Test = Vue.extend({
  434. template: '<p>hi</p>',
  435. replace: true
  436. })
  437. var t = new Test({
  438. el: '#' + testId
  439. })
  440. assert.strictEqual(t.$el.tagName, 'P')
  441. assert.strictEqual(t.$el.textContent, 'hi')
  442. assert.strictEqual(t.$el.parentNode, parent)
  443. var now = document.getElementById(testId)
  444. assert.strictEqual(now, t.$el, 'should copy over attributes from replaced node')
  445. })
  446. it('should replace an off DOM Vue\'s $el', function () {
  447. var Test = Vue.extend({
  448. template: '<p>hi</p>',
  449. replace: true
  450. })
  451. var t = new Test()
  452. assert.strictEqual(t.$el.tagName, 'P')
  453. assert.strictEqual(t.$el.textContent, 'hi')
  454. })
  455. it('should not work if template has more than one child node', function () {
  456. var Test = Vue.extend({
  457. template: '<p>hi</p><p>ho</p>',
  458. replace: true
  459. })
  460. var t = new Test()
  461. assert.notStrictEqual(t.$el.tagName, 'P')
  462. assert.strictEqual(t.$el.innerHTML, '<p>hi</p><p>ho</p>')
  463. })
  464. })
  465. describe('DOM element options', function () {
  466. it('should not accept el as an extension option', function () {
  467. var el = document.createElement('div'),
  468. Test = Vue.extend({ el: el }),
  469. t = new Test()
  470. assert.notStrictEqual(t.$el, el)
  471. })
  472. it('should create el with options: tagName, id, className and attributes', function () {
  473. var Test = Vue.extend({
  474. tagName: 'p',
  475. id: 'extend-test',
  476. className: 'extend',
  477. attributes: {
  478. 'test': 'hi',
  479. 'v-text': 'hoho'
  480. },
  481. data: {
  482. hoho: 'what'
  483. }
  484. })
  485. var t = new Test()
  486. assert.strictEqual(t.$el.nodeName, 'P', 'tagName should match')
  487. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  488. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  489. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  490. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  491. })
  492. it('should ignore tagName when el is passed as an instance option', function () {
  493. var el = document.createElement('div'),
  494. Test = Vue.extend({
  495. tagName: 'p',
  496. id: 'extend-test',
  497. className: 'extend',
  498. attributes: {
  499. 'test': 'hi',
  500. 'v-text': 'hoho'
  501. },
  502. data: {
  503. hoho: 'what'
  504. }
  505. }),
  506. t = new Test({
  507. el: el
  508. })
  509. assert.strictEqual(t.$el, el, 'should use instance el')
  510. assert.notStrictEqual(t.$el.nodeName, 'P', 'tagName should NOT match')
  511. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  512. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  513. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  514. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  515. })
  516. })
  517. describe('template', function () {
  518. var raw = '<span>{{hello}}</span><a>haha</a>'
  519. it('should take direct string template and work', function () {
  520. var Test = Vue.extend({
  521. tagName: 'p',
  522. template: raw,
  523. data: {
  524. hello: 'Ahaha'
  525. }
  526. }),
  527. vm = new Test(),
  528. text1 = vm.$el.querySelector('span').textContent,
  529. text2 = vm.$el.querySelector('a').textContent
  530. assert.strictEqual(vm.$el.nodeName, 'P')
  531. assert.strictEqual(text1, 'Ahaha')
  532. assert.strictEqual(text2, 'haha')
  533. })
  534. it('should take a #id and work', function () {
  535. var testId = 'template-test',
  536. tpl = document.createElement('script')
  537. tpl.id = testId
  538. tpl.type = 'text/template'
  539. tpl.innerHTML = raw
  540. document.getElementById('test').appendChild(tpl)
  541. var Test = Vue.extend({
  542. template: '#' + testId,
  543. data: { hello: testId }
  544. })
  545. var t = new Test()
  546. assert.strictEqual(t.$el.querySelector('span').textContent, testId)
  547. })
  548. it('should be overwritable', function () {
  549. var Test = Vue.extend({
  550. template: '<div>this should not happen</div>'
  551. })
  552. var t = new Test({
  553. template: raw,
  554. data: {
  555. hello: 'overwritten!'
  556. }
  557. })
  558. assert.strictEqual(t.$el.querySelector('span').textContent, 'overwritten!')
  559. })
  560. })
  561. describe('paramAttributes', function () {
  562. it('should copy listed attributes into data and parse Numbers', function () {
  563. var Test = Vue.extend({
  564. template: '<div a="1" b="hello"></div>',
  565. replace: true,
  566. paramAttributes: ['a', 'b', 'c']
  567. })
  568. var v = new Test()
  569. assert.strictEqual(v.a, 1)
  570. assert.strictEqual(v.$data.a, 1)
  571. assert.strictEqual(v.b, 'hello')
  572. assert.strictEqual(v.$data.b, 'hello')
  573. assert.strictEqual(v.c, null)
  574. assert.strictEqual(v.$data.c, null)
  575. })
  576. it('should be able in bind data from parents', function (done) {
  577. var v = new Vue({
  578. template: '<div v-component="test" v-ref="child"></div>',
  579. data: {
  580. size: 123
  581. },
  582. components: {
  583. test: {
  584. paramAttributes: ['size'],
  585. template: '<div class="child" size="{{size}}"></div>'
  586. }
  587. }
  588. })
  589. var childAttr = v.$el.querySelector('.child').getAttribute('size')
  590. assert.strictEqual(childAttr, '123')
  591. v.size = 234
  592. nextTick(function () {
  593. var childAttr = v.$el.querySelector('.child').getAttribute('size')
  594. assert.strictEqual(childAttr, '234')
  595. assert.strictEqual(v.$.child.size, 234)
  596. done()
  597. })
  598. })
  599. })
  600. describe('parent', function () {
  601. var parent, child
  602. it('should allow child to access parent bindings', function () {
  603. parent = new Vue({
  604. data: {
  605. test: 'from parent'
  606. }
  607. })
  608. child = new Vue({
  609. parent: parent,
  610. template: '{{test}}'
  611. })
  612. assert.strictEqual(child.$el.textContent, 'from parent')
  613. })
  614. it('should allow event communication between parent and child', function () {
  615. var dispatched = false,
  616. broadcasted = false
  617. parent.$on('dispatch', function () {
  618. dispatched = true
  619. })
  620. child.$on('broadcast', function () {
  621. broadcasted = true
  622. })
  623. parent.$broadcast('broadcast')
  624. child.$dispatch('dispatch')
  625. assert.ok(dispatched)
  626. assert.ok(broadcasted)
  627. })
  628. it('should destroy the child when parent is destroyed', function () {
  629. parent.$destroy()
  630. assert.ok(child.$compiler.destroyed)
  631. })
  632. })
  633. describe('directives', function () {
  634. it('should allow the VM to use private directives', function (done) {
  635. var Test = Vue.extend({
  636. directives: {
  637. test: function (value) {
  638. this.el.innerHTML = value ? 'YES' : 'NO'
  639. }
  640. }
  641. })
  642. var t = new Test({
  643. attributes: {
  644. 'v-test': 'ok'
  645. },
  646. data: {
  647. ok: true
  648. }
  649. })
  650. assert.strictEqual(t.$el.innerHTML, 'YES')
  651. t.ok = false
  652. nextTick(function () {
  653. assert.strictEqual(t.$el.innerHTML, 'NO')
  654. done()
  655. })
  656. })
  657. })
  658. describe('filters', function () {
  659. it('should allow the VM to use private filters', function () {
  660. var Test = Vue.extend({
  661. filters: {
  662. test: function (value) {
  663. return value + '12345'
  664. }
  665. }
  666. })
  667. var t = new Test({
  668. template: '{{hi | test}}',
  669. data: {
  670. hi: 'hohoho'
  671. }
  672. })
  673. assert.strictEqual(t.$el.textContent, 'hohoho12345')
  674. })
  675. })
  676. describe('components', function () {
  677. it('should allow the VM to use private child VMs', function () {
  678. var Child = Vue.extend({
  679. data: {
  680. name: 'child'
  681. }
  682. })
  683. var Parent = Vue.extend({
  684. template: '<p>{{name}}</p><div v-component="child">{{name}}</div>',
  685. data: {
  686. name: 'dad'
  687. },
  688. components: {
  689. child: Child
  690. }
  691. })
  692. var p = new Parent()
  693. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  694. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  695. })
  696. it('should work with plain option object', function () {
  697. var Parent = Vue.extend({
  698. template: '<p>{{name}}</p><div v-component="child">{{name}}</div>',
  699. data: {
  700. name: 'dad'
  701. },
  702. components: {
  703. child: {
  704. data: {
  705. name: 'child'
  706. }
  707. }
  708. }
  709. })
  710. var p = new Parent()
  711. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  712. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  713. })
  714. })
  715. describe('partials', function () {
  716. it('should allow the VM to use private partials', function () {
  717. var Test = Vue.extend({
  718. attributes: {
  719. 'v-partial': 'test'
  720. },
  721. partials: {
  722. test: '<a>{{a}}</a><p>{{b}}</p>'
  723. },
  724. data: {
  725. a: 'hi',
  726. b: 'ho'
  727. }
  728. })
  729. var t = new Test()
  730. assert.strictEqual(t.$el.querySelector('a').textContent, 'hi')
  731. assert.strictEqual(t.$el.querySelector('p').textContent, 'ho')
  732. })
  733. })
  734. describe('effects', function () {
  735. it('should get called during effects', function (done) {
  736. var enterCalled = false,
  737. leaveCalled = false
  738. var t = new Vue({
  739. attributes: {
  740. 'v-show': 'show',
  741. 'v-effect': 'test'
  742. },
  743. effects: {
  744. test: {
  745. enter: function (el, done) {
  746. enterCalled = true
  747. done()
  748. },
  749. leave: function (el, done) {
  750. leaveCalled = true
  751. done()
  752. }
  753. }
  754. },
  755. data: {
  756. show: false
  757. }
  758. })
  759. document.body.appendChild(t.$el)
  760. t.show = true
  761. nextTick(function () {
  762. assert.ok(enterCalled)
  763. assert.strictEqual(t.$el.style.display, '')
  764. t.show = false
  765. nextTick(function () {
  766. assert.ok(leaveCalled)
  767. assert.strictEqual(t.$el.style.display, 'none')
  768. t.$destroy()
  769. done()
  770. })
  771. })
  772. })
  773. })
  774. describe('hooks', function () {
  775. describe('created', function () {
  776. it('should be called before compile and give access to all vm properties', function () {
  777. var called = false,
  778. Test = Vue.extend({ created: function () {
  779. assert.ok(this.$data)
  780. assert.ok(this.$el)
  781. assert.ok(this.$)
  782. assert.ok(this.$compiler)
  783. assert.ok(this.$root)
  784. assert.ok(this.$options.ok)
  785. called = true
  786. }})
  787. new Test({ ok: true })
  788. assert.ok(called)
  789. })
  790. })
  791. describe('ready', function () {
  792. it('should be called after compile', function () {
  793. var called = false,
  794. hook = function () {
  795. assert.ok(this.$options.ok)
  796. assert.notOk(this.$compiler.init)
  797. called = true
  798. },
  799. Test = Vue.extend({ ready: hook })
  800. new Test({ ok: true })
  801. assert.ok(called)
  802. })
  803. })
  804. describe('beforeDestroy', function () {
  805. it('should be called before a vm is destroyed', function () {
  806. var called1 = false,
  807. called2 = false
  808. var Test = Vue.extend({
  809. beforeDestroy: function () {
  810. called1 = true
  811. }
  812. })
  813. var test = new Test()
  814. test.$on('hook:beforeDestroy', function () {
  815. called2 = true
  816. })
  817. test.$destroy()
  818. assert.ok(called1)
  819. assert.ok(called2)
  820. })
  821. })
  822. describe('afterDestroy', function () {
  823. it('should be called after a vm is destroyed', function () {
  824. var called1 = false, called2 = false,
  825. Test = Vue.extend({
  826. afterDestroy: function () {
  827. assert.notOk(this.$el.parentNode)
  828. called1 = true
  829. }
  830. })
  831. var test = new Test()
  832. document.body.appendChild(test.$el)
  833. test.$on('hook:afterDestroy', function () {
  834. called2 = true
  835. })
  836. test.$destroy()
  837. assert.ok(called1)
  838. assert.ok(called2)
  839. })
  840. })
  841. describe('attached', function () {
  842. it('should be called after enter view', function () {
  843. var called1 = false, called2 = false,
  844. test = new Vue({
  845. attached: function () {
  846. assert.strictEqual(this.$el.parentNode, document.getElementById('test'))
  847. called1 = true
  848. }
  849. })
  850. test.$on('hook:attached', function () {
  851. called2 = true
  852. })
  853. test.$appendTo('#test')
  854. assert.ok(called1)
  855. assert.ok(called2)
  856. })
  857. })
  858. describe('detached', function () {
  859. it('should be called after left view', function () {
  860. var called1 = false, called2 = false,
  861. test = new Vue({
  862. detached: function () {
  863. assert.strictEqual(this.$el.parentNode, null)
  864. called1 = true
  865. }
  866. })
  867. test.$on('hook:detached', function () {
  868. called2 = true
  869. })
  870. document.getElementById('test').appendChild(test.$el)
  871. test.$remove()
  872. assert.ok(called1)
  873. assert.ok(called2)
  874. })
  875. })
  876. describe('Hook inheritance', function () {
  877. it('should merge hooks with parent Class', function () {
  878. var called = []
  879. var Parent = Vue.extend({
  880. created: function () {
  881. called.push('parent')
  882. }
  883. })
  884. var Child = Parent.extend({
  885. created: function () {
  886. called.push('child')
  887. }
  888. })
  889. new Child({
  890. created: function () {
  891. called.push('instance')
  892. }
  893. })
  894. assert.deepEqual(called, ['parent', 'child', 'instance'])
  895. })
  896. })
  897. })
  898. })
  899. })
  900. })