_scopeId.spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import Vue from 'vue'
  2. describe('Options _scopeId', () => {
  3. it('should add scopeId attributes', () => {
  4. const vm = new Vue({
  5. _scopeId: 'foo',
  6. template: '<div><p><span></span></p></div>'
  7. }).$mount()
  8. expect(vm.$el.hasAttribute('foo')).toBe(true)
  9. expect(vm.$el.children[0].hasAttribute('foo')).toBe(true)
  10. expect(vm.$el.children[0].children[0].hasAttribute('foo')).toBe(true)
  11. })
  12. it('should add scopedId attributes from both parent and child on child root', () => {
  13. const vm = new Vue({
  14. _scopeId: 'foo',
  15. template: '<div><child></child></div>',
  16. components: {
  17. child: {
  18. _scopeId: 'bar',
  19. template: '<div></div>'
  20. }
  21. }
  22. }).$mount()
  23. expect(vm.$el.children[0].hasAttribute('foo')).toBe(true)
  24. expect(vm.$el.children[0].hasAttribute('bar')).toBe(true)
  25. })
  26. it('should add scopedId attributes from both parent and child on slot contents', () => {
  27. const vm = new Vue({
  28. _scopeId: 'foo',
  29. template: '<div><child><p>hi</p></child></div>',
  30. components: {
  31. child: {
  32. _scopeId: 'bar',
  33. template: '<div><slot></slot></div>'
  34. }
  35. }
  36. }).$mount()
  37. expect(vm.$el.children[0].children[0].hasAttribute('foo')).toBe(true)
  38. expect(vm.$el.children[0].children[0].hasAttribute('bar')).toBe(true)
  39. })
  40. // #4774
  41. it('should not discard parent scopeId when component root element is replaced', done => {
  42. const vm = new Vue({
  43. _scopeId: 'data-1',
  44. template: `<div><child ref="child" /></div>`,
  45. components: {
  46. child: {
  47. _scopeId: 'data-2',
  48. data: () => ({ show: true }),
  49. template: '<div v-if="show"></div>'
  50. }
  51. }
  52. }).$mount()
  53. const child = vm.$refs.child
  54. expect(child.$el.hasAttribute('data-1')).toBe(true)
  55. expect(child.$el.hasAttribute('data-2')).toBe(true)
  56. child.show = false
  57. waitForUpdate(() => {
  58. child.show = true
  59. }).then(() => {
  60. expect(child.$el.hasAttribute('data-1')).toBe(true)
  61. expect(child.$el.hasAttribute('data-2')).toBe(true)
  62. }).then(done)
  63. })
  64. it('should work on functional components', () => {
  65. const child = {
  66. functional: true,
  67. _scopeId: 'child',
  68. render (h) {
  69. return h('div', { class: 'child' }, [
  70. h('span', { class: 'child' }, 'child')
  71. ])
  72. }
  73. }
  74. const vm = new Vue({
  75. _scopeId: 'parent',
  76. components: { child },
  77. template: '<div><child></child></div>'
  78. }).$mount()
  79. expect(vm.$el.hasAttribute('parent')).toBe(true)
  80. const childEls = vm.$el.querySelectorAll('.child')
  81. ;[].forEach.call(childEls, el => {
  82. expect(el.hasAttribute('child')).toBe(true)
  83. // functional component with scopeId will not inherit parent scopeId
  84. expect(el.hasAttribute('parent')).toBe(false)
  85. })
  86. })
  87. })