extends.spec.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. })
  48. describe('Options extends with Object.prototype.watch', () => {
  49. beforeAll(function () {
  50. if (!Object.prototype.watch) {
  51. // eslint-disable-next-line no-extend-native
  52. Object.prototype.watch = {
  53. remove: true
  54. }
  55. }
  56. })
  57. afterAll(function () {
  58. if (Object.prototype.watch && Object.prototype.watch.remove) {
  59. delete Object.prototype.watch
  60. }
  61. })
  62. it('should work with global mixins', done => {
  63. Vue.use({
  64. install: function () {
  65. Vue.mixin({})
  66. }
  67. })
  68. const spy = jasmine.createSpy('watch')
  69. const A = Vue.extend({
  70. data: function () {
  71. return { a: 1 }
  72. },
  73. watch: {
  74. a: spy
  75. },
  76. created: function () {
  77. this.a = 2
  78. }
  79. })
  80. new Vue({
  81. extends: A
  82. })
  83. waitForUpdate(() => {
  84. expect(spy).toHaveBeenCalledWith(2, 1)
  85. }).then(done)
  86. })
  87. })