useCssVars.spec.ts 3.6 KB

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