api.js 32 KB

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