ref_spec.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. vm._directives[0].unbind()
  40. expect(vm.$.test).toBeUndefined()
  41. done()
  42. })
  43. })
  44. it('inside v-if', function () {
  45. var vm = new Vue({
  46. el: el,
  47. data: { test: true },
  48. components: components,
  49. template: '<div v-if="test"><div v-component="test" v-ref="test"></div></div>'
  50. })
  51. expect(vm.$.test).toBeTruthy()
  52. expect(vm.$.test.$options.id).toBe('test')
  53. vm.$.test.$destroy()
  54. expect(vm.$.test).toBeNull()
  55. })
  56. it('nested v-repeat', function () {
  57. var vm = new Vue({
  58. el: el,
  59. template: '<div v-component="c1" v-ref="c1"><div v-repeat="2" v-ref="c2"></div></div>',
  60. components: { c1: {} }
  61. })
  62. expect(vm.$.c1 instanceof Vue).toBe(true)
  63. expect(vm.$.c2).toBeUndefined()
  64. expect(Array.isArray(vm.$.c1.$.c2)).toBe(true)
  65. expect(vm.$.c1.$.c2.length).toBe(2)
  66. })
  67. it('warn on non-root', function () {
  68. var vm = new Vue({
  69. el: el,
  70. template: '<div v-ref="test"></div>'
  71. })
  72. expect(_.warn).toHaveBeenCalled()
  73. })
  74. })
  75. }