api.js 31 KB

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