_scopeId.spec.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. })
  60. .then(() => {
  61. expect(child.$el.hasAttribute('data-1')).toBe(true)
  62. expect(child.$el.hasAttribute('data-2')).toBe(true)
  63. })
  64. .then(done)
  65. })
  66. it('should work on functional components', () => {
  67. const child = {
  68. functional: true,
  69. _scopeId: 'child',
  70. render(h) {
  71. return h('div', { class: 'child' }, [
  72. h('span', { class: 'child' }, 'child')
  73. ])
  74. }
  75. }
  76. const vm = new Vue({
  77. _scopeId: 'parent',
  78. components: { child },
  79. template: '<div><child></child></div>'
  80. }).$mount()
  81. expect(vm.$el.hasAttribute('parent')).toBe(true)
  82. const childEls = vm.$el.querySelectorAll('.child')
  83. ;[].forEach.call(childEls, el => {
  84. expect(el.hasAttribute('child')).toBe(true)
  85. // functional component with scopeId will not inherit parent scopeId
  86. expect(el.hasAttribute('parent')).toBe(false)
  87. })
  88. })
  89. })