api.js 39 KB

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