useCssVars.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {
  2. render,
  3. useCssVars,
  4. h,
  5. reactive,
  6. nextTick,
  7. ComponentOptions,
  8. Suspense
  9. } from '@vue/runtime-dom'
  10. describe('useCssVars', () => {
  11. async function assertCssVars(
  12. getApp: (state: any) => ComponentOptions,
  13. scopeId?: string
  14. ) {
  15. const state = reactive({ color: 'red' })
  16. const App = getApp(state)
  17. const root = document.createElement('div')
  18. const prefix = scopeId ? `${scopeId.replace(/^data-v-/, '')}-` : ``
  19. render(h(App), root)
  20. for (const c of [].slice.call(root.children as any)) {
  21. expect(
  22. (c as HTMLElement).style.getPropertyValue(`--${prefix}color`)
  23. ).toBe(`red`)
  24. }
  25. state.color = 'green'
  26. await nextTick()
  27. for (const c of [].slice.call(root.children as any)) {
  28. expect(
  29. (c as HTMLElement).style.getPropertyValue(`--${prefix}color`)
  30. ).toBe('green')
  31. }
  32. }
  33. test('basic', async () => {
  34. await assertCssVars(state => ({
  35. setup() {
  36. // test receiving render context
  37. useCssVars((ctx: any) => ({
  38. color: ctx.color
  39. }))
  40. return state
  41. },
  42. render() {
  43. return h('div')
  44. }
  45. }))
  46. })
  47. test('on fragment root', async () => {
  48. await assertCssVars(state => ({
  49. setup() {
  50. useCssVars(() => state)
  51. return () => [h('div'), h('div')]
  52. }
  53. }))
  54. })
  55. test('on HOCs', async () => {
  56. const Child = () => [h('div'), h('div')]
  57. await assertCssVars(state => ({
  58. setup() {
  59. useCssVars(() => state)
  60. return () => h(Child)
  61. }
  62. }))
  63. })
  64. test('on suspense root', async () => {
  65. const state = reactive({ color: 'red' })
  66. const root = document.createElement('div')
  67. const AsyncComp = {
  68. async setup() {
  69. return () => h('p', 'default')
  70. }
  71. }
  72. const App = {
  73. setup() {
  74. useCssVars(() => state)
  75. return () =>
  76. h(Suspense, null, {
  77. default: h(AsyncComp),
  78. fallback: h('div', 'fallback')
  79. })
  80. }
  81. }
  82. render(h(App), root)
  83. // css vars use with fallback tree
  84. for (const c of [].slice.call(root.children as any)) {
  85. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  86. }
  87. // AsyncComp resolve
  88. await nextTick()
  89. // Suspense effects flush
  90. await nextTick()
  91. // css vars use with default tree
  92. for (const c of [].slice.call(root.children as any)) {
  93. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  94. }
  95. state.color = 'green'
  96. await nextTick()
  97. for (const c of [].slice.call(root.children as any)) {
  98. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
  99. }
  100. })
  101. test('with <style scoped>', async () => {
  102. const id = 'data-v-12345'
  103. await assertCssVars(
  104. state => ({
  105. __scopeId: id,
  106. setup() {
  107. useCssVars(() => state, true)
  108. return () => h('div')
  109. }
  110. }),
  111. id
  112. )
  113. })
  114. })