scope_spec.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Test property proxy, scope inheritance,
  3. * data event propagation and data sync
  4. */
  5. var Vue = require('../../../../src/vue')
  6. var Observer = require('../../../../src/observer')
  7. Observer.pathDelimiter = '.'
  8. describe('Scope', function () {
  9. // TODO
  10. describe('computed', function () {
  11. var vm = new Vue({
  12. data: {
  13. a: 'a',
  14. b: 'b'
  15. },
  16. computed: {
  17. c: function () {
  18. expect(this).toBe(vm)
  19. return this.a + this.b
  20. },
  21. d: {
  22. get: function () {
  23. expect(this).toBe(vm)
  24. return this.a + this.b
  25. },
  26. set: function (newVal) {
  27. expect(this).toBe(vm)
  28. var vals = newVal.split(' ')
  29. this.a = vals[0]
  30. this.b = vals[1]
  31. }
  32. }
  33. }
  34. })
  35. it('get', function () {
  36. expect(vm.c).toBe('ab')
  37. expect(vm.d).toBe('ab')
  38. })
  39. it('set', function () {
  40. vm.c = 123 // should do nothing
  41. vm.d = 'c d'
  42. expect(vm.a).toBe('c')
  43. expect(vm.b).toBe('d')
  44. expect(vm.c).toBe('cd')
  45. expect(vm.d).toBe('cd')
  46. })
  47. it('inherit', function () {
  48. var child = vm.$addChild()
  49. expect(child.c).toBe('cd')
  50. child.d = 'e f'
  51. expect(vm.a).toBe('e')
  52. expect(vm.b).toBe('f')
  53. expect(vm.c).toBe('ef')
  54. expect(vm.d).toBe('ef')
  55. expect(child.a).toBe('e')
  56. expect(child.b).toBe('f')
  57. expect(child.c).toBe('ef')
  58. expect(child.d).toBe('ef')
  59. })
  60. })
  61. describe('methods', function () {
  62. it('should work and have correct context', function () {
  63. var vm = new Vue({
  64. data: {
  65. a: 1
  66. },
  67. methods: {
  68. test: function () {
  69. expect(this instanceof Vue).toBe(true)
  70. return this.a
  71. }
  72. }
  73. })
  74. expect(vm.test()).toBe(1)
  75. var child = vm.$addChild()
  76. expect(child.test()).toBe(1)
  77. })
  78. })
  79. })