properties.spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import Vue from 'vue'
  2. describe('Instance properties', () => {
  3. it('$data', () => {
  4. const data = { a: 1 }
  5. const vm = new Vue({
  6. data
  7. })
  8. expect(vm.a).toBe(1)
  9. expect(vm.$data).toBe(data)
  10. // vm -> data
  11. vm.a = 2
  12. expect(data.a).toBe(2)
  13. // data -> vm
  14. data.a = 3
  15. expect(vm.a).toBe(3)
  16. })
  17. it('$options', () => {
  18. const A = Vue.extend({
  19. methods: {
  20. a () {}
  21. }
  22. })
  23. const vm = new A({
  24. methods: {
  25. b () {}
  26. }
  27. })
  28. expect(typeof vm.$options.methods.a).toBe('function')
  29. expect(typeof vm.$options.methods.b).toBe('function')
  30. })
  31. it('$root/$children', done => {
  32. const vm = new Vue({
  33. template: '<div><test v-if="ok"></test></div>',
  34. data: { ok: true },
  35. components: {
  36. test: {
  37. template: '<div></div>'
  38. }
  39. }
  40. }).$mount()
  41. expect(vm.$root).toBe(vm)
  42. expect(vm.$children.length).toBe(1)
  43. expect(vm.$children[0].$root).toBe(vm)
  44. vm.ok = false
  45. waitForUpdate(() => {
  46. expect(vm.$children.length).toBe(0)
  47. vm.ok = true
  48. }).then(() => {
  49. expect(vm.$children.length).toBe(1)
  50. expect(vm.$children[0].$root).toBe(vm)
  51. }).then(done)
  52. })
  53. it('$parent', () => {
  54. const calls = []
  55. const makeOption = name => ({
  56. name,
  57. template: `<div><slot></slot></div>`,
  58. created () {
  59. calls.push(`${name}:${this.$parent.$options.name}`)
  60. }
  61. })
  62. new Vue({
  63. template: `
  64. <div>
  65. <outer><middle><inner></inner></middle></outer>
  66. <next></next>
  67. </div>
  68. `,
  69. components: {
  70. outer: makeOption('outer'),
  71. middle: makeOption('middle'),
  72. inner: makeOption('inner'),
  73. next: makeOption('next')
  74. }
  75. }).$mount()
  76. expect(calls).toEqual(['outer:undefined', 'middle:outer', 'inner:middle', 'next:undefined'])
  77. })
  78. it('$props', done => {
  79. const Comp = Vue.extend({
  80. props: ['msg'],
  81. template: '<div>{{ msg }} {{ $props.msg }}</div>'
  82. })
  83. const vm = new Comp({
  84. propsData: {
  85. msg: 'foo'
  86. }
  87. }).$mount()
  88. // check render
  89. expect(vm.$el.textContent).toContain('foo foo')
  90. // warn set
  91. vm.$props = {}
  92. expect('$props is readonly').toHaveBeenWarned()
  93. // check existence
  94. expect(vm.$props.msg).toBe('foo')
  95. // check change
  96. vm.msg = 'bar'
  97. expect(vm.$props.msg).toBe('bar')
  98. waitForUpdate(() => {
  99. expect(vm.$el.textContent).toContain('bar bar')
  100. }).then(() => {
  101. vm.$props.msg = 'baz'
  102. expect(vm.msg).toBe('baz')
  103. }).then(() => {
  104. expect(vm.$el.textContent).toContain('baz baz')
  105. }).then(done)
  106. })
  107. it('warn mutating $props', () => {
  108. const Comp = {
  109. props: ['msg'],
  110. render () {},
  111. mounted () {
  112. expect(this.$props.msg).toBe('foo')
  113. this.$props.msg = 'bar'
  114. }
  115. }
  116. new Vue({
  117. template: `<comp ref="comp" msg="foo" />`,
  118. components: { Comp }
  119. }).$mount()
  120. expect(`Avoid mutating a prop`).toHaveBeenWarned()
  121. })
  122. })