api.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. it('should allow subclasses to use plugins', function () {
  321. var Sub = Vue.extend({})
  322. Sub.use(function (Sub) {
  323. Sub.directive('hello', {})
  324. })
  325. assert.ok(Sub.options.directives.hello)
  326. })
  327. describe('Options', function () {
  328. describe('methods', function () {
  329. it('should be mixed to the exteded VM\'s prototype', function () {
  330. var mixins = {
  331. c: function () {},
  332. d: function () {}
  333. }
  334. var Test = Vue.extend({ methods: mixins })
  335. for (var key in mixins) {
  336. assert.strictEqual(Test.prototype[key], mixins[key])
  337. }
  338. })
  339. })
  340. describe('data', function () {
  341. it('should be copied to each instance', function () {
  342. var testData = { a: 1 },
  343. Test = Vue.extend({
  344. data: {
  345. test: testData
  346. }
  347. })
  348. var t1 = new Test(),
  349. t2 = new Test()
  350. assert.ok(t1.hasOwnProperty('test'))
  351. assert.strictEqual(t1.test, testData)
  352. assert.ok(t2.hasOwnProperty('test'))
  353. assert.strictEqual(t2.test, testData)
  354. })
  355. })
  356. describe('lazy', function () {
  357. it('should make text input fields only trigger on change', function () {
  358. var Test = Vue.extend({
  359. template: '<input type="text" v-model="test">',
  360. lazy: true
  361. })
  362. var t = new Test({
  363. data: {
  364. test: 'hi'
  365. }
  366. })
  367. var input = t.$el.querySelector('input')
  368. input.value = 'hohoho'
  369. input.dispatchEvent(mockHTMLEvent('input'))
  370. assert.strictEqual(t.test, 'hi')
  371. input.dispatchEvent(mockHTMLEvent('change'))
  372. assert.strictEqual(t.test, 'hohoho')
  373. })
  374. })
  375. describe('replace', function () {
  376. it('should replace an in DOM node', function () {
  377. var testId = 'replace-test'
  378. mock(testId, '<div>ho</div>')
  379. var old = document.getElementById(testId),
  380. parent = old.parentNode
  381. var Test = Vue.extend({
  382. template: '<p>hi</p>',
  383. replace: true
  384. })
  385. var t = new Test({
  386. el: '#' + testId
  387. })
  388. assert.strictEqual(t.$el.tagName, 'P')
  389. assert.strictEqual(t.$el.textContent, 'hi')
  390. assert.strictEqual(t.$el.parentNode, parent)
  391. var now = document.getElementById(testId)
  392. assert.strictEqual(now, null)
  393. })
  394. it('should replace an off DOM Vue\'s $el', function () {
  395. var Test = Vue.extend({
  396. template: '<p>hi</p>',
  397. replace: true
  398. })
  399. var t = new Test()
  400. assert.strictEqual(t.$el.tagName, 'P')
  401. assert.strictEqual(t.$el.textContent, 'hi')
  402. })
  403. it('should not work if template has more than one child node', function () {
  404. var Test = Vue.extend({
  405. template: '<p>hi</p><p>ho</p>',
  406. replace: true
  407. })
  408. var t = new Test()
  409. assert.notStrictEqual(t.$el.tagName, 'P')
  410. assert.strictEqual(t.$el.innerHTML, '<p>hi</p><p>ho</p>')
  411. })
  412. })
  413. describe('DOM element options', function () {
  414. it('should not accept el as an extension option', function () {
  415. var el = document.createElement('div'),
  416. Test = Vue.extend({ el: el }),
  417. t = new Test()
  418. assert.notStrictEqual(t.$el, el)
  419. })
  420. it('should create el with options: tagName, id, className and attributes', function () {
  421. var Test = Vue.extend({
  422. tagName: 'p',
  423. id: 'extend-test',
  424. className: 'extend',
  425. attributes: {
  426. 'test': 'hi',
  427. 'v-text': 'hoho'
  428. },
  429. data: {
  430. hoho: 'what'
  431. }
  432. })
  433. var t = new Test()
  434. assert.strictEqual(t.$el.nodeName, 'P', 'tagName should match')
  435. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  436. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  437. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  438. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  439. })
  440. it('should ignore tagName when el is passed as an instance option', function () {
  441. var el = document.createElement('div'),
  442. Test = Vue.extend({
  443. tagName: 'p',
  444. id: 'extend-test',
  445. className: 'extend',
  446. attributes: {
  447. 'test': 'hi',
  448. 'v-text': 'hoho'
  449. },
  450. data: {
  451. hoho: 'what'
  452. }
  453. }),
  454. t = new Test({
  455. el: el
  456. })
  457. assert.strictEqual(t.$el, el, 'should use instance el')
  458. assert.notStrictEqual(t.$el.nodeName, 'P', 'tagName should NOT match')
  459. assert.strictEqual(t.$el.id, 'extend-test', 'id should match')
  460. assert.strictEqual(t.$el.className, 'extend', 'className should match')
  461. assert.strictEqual(t.$el.getAttribute('test'), 'hi', 'normal attr should work')
  462. assert.strictEqual(t.$el.textContent, 'what', 'directives passed in as attr should work')
  463. })
  464. })
  465. describe('template', function () {
  466. var raw = '<span>{{hello}}</span><a>haha</a>'
  467. it('should take direct string template and work', function () {
  468. var Test = Vue.extend({
  469. tagName: 'p',
  470. template: raw,
  471. data: {
  472. hello: 'Ahaha'
  473. }
  474. }),
  475. vm = new Test(),
  476. text1 = vm.$el.querySelector('span').textContent,
  477. text2 = vm.$el.querySelector('a').textContent
  478. assert.strictEqual(vm.$el.nodeName, 'P')
  479. assert.strictEqual(text1, 'Ahaha')
  480. assert.strictEqual(text2, 'haha')
  481. })
  482. it('should take a #id and work', function () {
  483. var testId = 'template-test',
  484. tpl = document.createElement('script')
  485. tpl.id = testId
  486. tpl.type = 'text/template'
  487. tpl.innerHTML = raw
  488. document.getElementById('test').appendChild(tpl)
  489. var Test = Vue.extend({
  490. template: '#' + testId,
  491. data: { hello: testId }
  492. })
  493. var t = new Test()
  494. assert.strictEqual(t.$el.querySelector('span').textContent, testId)
  495. })
  496. it('should be overwritable', function () {
  497. var Test = Vue.extend({
  498. template: '<div>this should not happen</div>'
  499. })
  500. var t = new Test({
  501. template: raw,
  502. data: {
  503. hello: 'overwritten!'
  504. }
  505. })
  506. assert.strictEqual(t.$el.querySelector('span').textContent, 'overwritten!')
  507. })
  508. })
  509. describe('directives', function () {
  510. it('should allow the VM to use private directives', function (done) {
  511. var Test = Vue.extend({
  512. directives: {
  513. test: function (value) {
  514. this.el.innerHTML = value ? 'YES' : 'NO'
  515. }
  516. }
  517. })
  518. var t = new Test({
  519. attributes: {
  520. 'v-test': 'ok'
  521. },
  522. data: {
  523. ok: true
  524. }
  525. })
  526. assert.strictEqual(t.$el.innerHTML, 'YES')
  527. t.ok = false
  528. nextTick(function () {
  529. assert.strictEqual(t.$el.innerHTML, 'NO')
  530. done()
  531. })
  532. })
  533. })
  534. describe('filters', function () {
  535. it('should allow the VM to use private filters', function () {
  536. var Test = Vue.extend({
  537. filters: {
  538. test: function (value) {
  539. return value + '12345'
  540. }
  541. }
  542. })
  543. var t = new Test({
  544. template: '{{hi | test}}',
  545. data: {
  546. hi: 'hohoho'
  547. }
  548. })
  549. assert.strictEqual(t.$el.textContent, 'hohoho12345')
  550. })
  551. })
  552. describe('components', function () {
  553. it('should allow the VM to use private child VMs', function () {
  554. var Child = Vue.extend({
  555. data: {
  556. name: 'child'
  557. }
  558. })
  559. var Parent = Vue.extend({
  560. template: '<p>{{name}}</p><div v-component="child">{{name}}</div>',
  561. data: {
  562. name: 'dad'
  563. },
  564. components: {
  565. child: Child
  566. }
  567. })
  568. var p = new Parent()
  569. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  570. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  571. })
  572. it('should work with plain option object', function () {
  573. var Parent = Vue.extend({
  574. template: '<p>{{name}}</p><div v-component="child">{{name}}</div>',
  575. data: {
  576. name: 'dad'
  577. },
  578. components: {
  579. child: {
  580. data: {
  581. name: 'child'
  582. }
  583. }
  584. }
  585. })
  586. var p = new Parent()
  587. assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
  588. assert.strictEqual(p.$el.querySelector('div').textContent, 'child')
  589. })
  590. })
  591. describe('partials', function () {
  592. it('should allow the VM to use private partials', function () {
  593. var Test = Vue.extend({
  594. attributes: {
  595. 'v-partial': 'test'
  596. },
  597. partials: {
  598. test: '<a>{{a}}</a><p>{{b}}</p>'
  599. },
  600. data: {
  601. a: 'hi',
  602. b: 'ho'
  603. }
  604. })
  605. var t = new Test()
  606. assert.strictEqual(t.$el.querySelector('a').textContent, 'hi')
  607. assert.strictEqual(t.$el.querySelector('p').textContent, 'ho')
  608. })
  609. })
  610. describe('transitions', function () {
  611. it('should get called during transitions', function (done) {
  612. var enterCalled = false,
  613. leaveCalled = false
  614. var t = new Vue({
  615. attributes: {
  616. 'v-show': 'show',
  617. 'v-transition': 'test'
  618. },
  619. transitions: {
  620. test: {
  621. enter: function (el, done) {
  622. enterCalled = true
  623. done()
  624. },
  625. leave: function (el, done) {
  626. leaveCalled = true
  627. done()
  628. }
  629. }
  630. },
  631. data: {
  632. show: false
  633. }
  634. })
  635. document.body.appendChild(t.$el)
  636. t.show = true
  637. nextTick(function () {
  638. assert.ok(enterCalled)
  639. assert.strictEqual(t.$el.style.display, '')
  640. t.show = false
  641. nextTick(function () {
  642. assert.ok(leaveCalled)
  643. assert.strictEqual(t.$el.style.display, 'none')
  644. t.$destroy()
  645. done()
  646. })
  647. })
  648. })
  649. })
  650. describe('hooks', function () {
  651. describe('created', function () {
  652. it('should be called before compile', function () {
  653. var called = false,
  654. Test = Vue.extend({ created: function (options) {
  655. assert.ok(options.ok)
  656. called = true
  657. }})
  658. new Test({ ok: true })
  659. assert.ok(called)
  660. })
  661. })
  662. describe('ready', function () {
  663. it('should be called after compile with options', function () {
  664. var called = false,
  665. hook = function (options) {
  666. assert.ok(options.ok)
  667. assert.notOk(this.$compiler.init)
  668. called = true
  669. },
  670. Test = Vue.extend({ ready: hook })
  671. new Test({ ok: true })
  672. assert.ok(called)
  673. })
  674. })
  675. describe('beforeDestroy', function () {
  676. it('should be called before a vm is destroyed', function () {
  677. var called1 = false,
  678. called2 = false
  679. var Test = Vue.extend({
  680. beforeDestroy: function () {
  681. called1 = true
  682. }
  683. })
  684. var test = new Test()
  685. test.$on('hook:beforeDestroy', function () {
  686. called2 = true
  687. })
  688. test.$destroy()
  689. assert.ok(called1)
  690. assert.ok(called2)
  691. })
  692. })
  693. describe('afterDestroy', function () {
  694. it('should be called after a vm is destroyed', function () {
  695. var called1 = false, called2 = false,
  696. Test = Vue.extend({
  697. afterDestroy: function () {
  698. assert.notOk(this.$el.parentNode)
  699. called1 = true
  700. }
  701. })
  702. var test = new Test()
  703. document.body.appendChild(test.$el)
  704. test.$on('hook:afterDestroy', function () {
  705. called2 = true
  706. })
  707. test.$destroy()
  708. assert.ok(called1)
  709. assert.ok(called2)
  710. })
  711. })
  712. describe('attached', function () {
  713. it('should be called after enter view', function () {
  714. var called1 = false, called2 = false,
  715. test = new Vue({
  716. attached: function () {
  717. assert.strictEqual(this.$el.parentNode, document.getElementById('test'))
  718. called1 = true
  719. }
  720. })
  721. test.$on('hook:attached', function () {
  722. called2 = true
  723. })
  724. test.$appendTo('#test')
  725. assert.ok(called1)
  726. assert.ok(called2)
  727. })
  728. })
  729. describe('detached', function () {
  730. it('should be called after left view', function () {
  731. var called1 = false, called2 = false,
  732. test = new Vue({
  733. detached: function () {
  734. assert.strictEqual(this.$el.parentNode, null)
  735. called1 = true
  736. }
  737. })
  738. test.$on('hook:detached', function () {
  739. called2 = true
  740. })
  741. document.getElementById('test').appendChild(test.$el)
  742. test.$remove()
  743. assert.ok(called1)
  744. assert.ok(called2)
  745. })
  746. })
  747. describe('Hook inheritance', function () {
  748. it('should merge hooks with parent Class', function () {
  749. var called = []
  750. var Parent = Vue.extend({
  751. created: function () {
  752. called.push('parent')
  753. }
  754. })
  755. var Child = Parent.extend({
  756. created: function () {
  757. called.push('child')
  758. }
  759. })
  760. new Child({
  761. created: function () {
  762. called.push('instance')
  763. }
  764. })
  765. assert.deepEqual(called, ['parent', 'child', 'instance'])
  766. })
  767. })
  768. })
  769. })
  770. })
  771. })