child_spec.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var Vue = require('../../../../src/vue')
  2. var _ = Vue.util
  3. describe('Child API', function () {
  4. var vm
  5. beforeEach(function () {
  6. vm = new Vue({
  7. data: {
  8. a: 1,
  9. b: 1
  10. },
  11. directives: {
  12. test: function () {}
  13. }
  14. })
  15. })
  16. it('default', function () {
  17. var child = vm.$addChild()
  18. expect(child instanceof Vue).toBe(true)
  19. expect(child.a).toBeUndefined()
  20. expect(child.$parent).toBe(vm)
  21. expect(child.$root).toBe(vm)
  22. expect(vm.$children.indexOf(child)).toBe(0)
  23. expect(_.resolveAsset(child.$options, 'directives', 'test')).toBeTruthy()
  24. })
  25. it('inherit scope', function () {
  26. var child = vm.$addChild({
  27. inherit: true,
  28. data: {
  29. b: 2
  30. }
  31. })
  32. expect(child.a).toBe(1)
  33. expect(child.b).toBe(2)
  34. expect(child.constructor.prototype).toBe(vm)
  35. })
  36. it('with constructor', function () {
  37. var Ctor = Vue.extend({
  38. inherit: true,
  39. data: function () {
  40. return {
  41. c: 3
  42. }
  43. }
  44. })
  45. var child = vm.$addChild({
  46. data: {
  47. b: 2
  48. }
  49. }, Ctor)
  50. expect(child.a).toBe(1)
  51. expect(child.b).toBe(2)
  52. expect(child.c).toBe(3)
  53. expect(child.constructor.options).toBe(Ctor.options)
  54. })
  55. it('cache constructor', function () {
  56. var Ctor = Vue.extend({
  57. inherit: true
  58. })
  59. var child1 = vm.$addChild(null, Ctor)
  60. var child2 = vm.$addChild(null, Ctor)
  61. expect(child1.constructor).toBe(child2.constructor)
  62. })
  63. })