ref_spec.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. if (_.inBrowser) {
  4. describe('v-ref', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. spyOn(_, 'warn')
  9. })
  10. var components = {
  11. test: {
  12. id: 'test'
  13. }
  14. }
  15. it('normal', function () {
  16. var vm = new Vue({
  17. el: el,
  18. components: components,
  19. template: '<div v-component="test" v-ref="test"></div>'
  20. })
  21. expect(vm.$.test).toBeTruthy()
  22. expect(vm.$.test.$options.id).toBe('test')
  23. vm.$.test.$destroy()
  24. expect(vm.$.test).toBeNull()
  25. })
  26. it('with v-repeat', function (done) {
  27. var vm = new Vue({
  28. el: el,
  29. data: { items: [1,2,3,4,5] },
  30. template: '<div v-repeat="items" v-ref="test"></div>'
  31. })
  32. expect(vm.$.test).toBeTruthy()
  33. expect(Array.isArray(vm.$.test)).toBe(true)
  34. expect(vm.$.test[0].$value).toBe(1)
  35. expect(vm.$.test[4].$value).toBe(5)
  36. vm.items = []
  37. _.nextTick(function () {
  38. expect(vm.$.test.length).toBe(0)
  39. done()
  40. })
  41. })
  42. it('inside v-if', function () {
  43. var vm = new Vue({
  44. el: el,
  45. data: { test: true },
  46. components: components,
  47. template: '<div v-if="test"><div v-component="test" v-ref="test"></div></div>'
  48. })
  49. expect(vm.$.test).toBeTruthy()
  50. expect(vm.$.test.$options.id).toBe('test')
  51. vm.$.test.$destroy()
  52. expect(vm.$.test).toBeNull()
  53. })
  54. it('warn on non-root', function () {
  55. var vm = new Vue({
  56. el: el,
  57. template: '<div v-ref="test"></div>'
  58. })
  59. expect(_.warn).toHaveBeenCalled()
  60. })
  61. })
  62. }