properties.spec.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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('$isServer', () => {
  79. const vm = new Vue()
  80. expect(vm.$isServer).toBe(false)
  81. Vue.config._isServer = true
  82. expect(vm.$isServer).toBe(true)
  83. Vue.config._isServer = false
  84. })
  85. })