child_spec.js 1.7 KB

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