extends.spec.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import Vue from 'vue'
  2. describe('Options extends', () => {
  3. it('should work on objects', () => {
  4. const A = {
  5. data () {
  6. return { a: 1 }
  7. }
  8. }
  9. const B = {
  10. extends: A,
  11. data () {
  12. return { b: 2 }
  13. }
  14. }
  15. const vm = new Vue({
  16. extends: B,
  17. data: {
  18. c: 3
  19. }
  20. })
  21. expect(vm.a).toBe(1)
  22. expect(vm.b).toBe(2)
  23. expect(vm.c).toBe(3)
  24. })
  25. it('should work on extended constructors', () => {
  26. const A = Vue.extend({
  27. data () {
  28. return { a: 1 }
  29. }
  30. })
  31. const B = Vue.extend({
  32. extends: A,
  33. data () {
  34. return { b: 2 }
  35. }
  36. })
  37. const vm = new Vue({
  38. extends: B,
  39. data: {
  40. c: 3
  41. }
  42. })
  43. expect(vm.a).toBe(1)
  44. expect(vm.b).toBe(2)
  45. expect(vm.c).toBe(3)
  46. })
  47. it('should work with global mixins + Object.prototype.watch', done => {
  48. let fakeWatch = false
  49. if (!Object.prototype.watch) {
  50. fakeWatch = true
  51. // eslint-disable-next-line no-extend-native
  52. Object.defineProperty(Object.prototype, 'watch', {
  53. writable: true,
  54. configurable: true,
  55. enumerable: false,
  56. value: () => {}
  57. })
  58. }
  59. Vue.mixin({})
  60. const spy = jasmine.createSpy('watch')
  61. const A = Vue.extend({
  62. data: function () {
  63. return { a: 1 }
  64. },
  65. watch: {
  66. a: spy
  67. },
  68. created: function () {
  69. this.a = 2
  70. }
  71. })
  72. new Vue({
  73. extends: A
  74. })
  75. waitForUpdate(() => {
  76. expect(spy).toHaveBeenCalledWith(2, 1)
  77. if (fakeWatch) {
  78. delete Object.prototype.watch
  79. }
  80. }).then(done)
  81. })
  82. })