api.js 27 KB

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