api.js 36 KB

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