useCssVars.spec.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import {
  2. ref,
  3. render,
  4. useCssVars,
  5. createStaticVNode,
  6. h,
  7. reactive,
  8. nextTick,
  9. ComponentOptions,
  10. Suspense,
  11. Teleport,
  12. FunctionalComponent
  13. } from '@vue/runtime-dom'
  14. describe('useCssVars', () => {
  15. async function assertCssVars(getApp: (state: any) => ComponentOptions) {
  16. const state = reactive({ color: 'red' })
  17. const App = getApp(state)
  18. const root = document.createElement('div')
  19. render(h(App), root)
  20. await nextTick()
  21. for (const c of [].slice.call(root.children as any)) {
  22. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  23. }
  24. state.color = 'green'
  25. await nextTick()
  26. for (const c of [].slice.call(root.children as any)) {
  27. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
  28. }
  29. }
  30. test('basic', async () => {
  31. await assertCssVars(state => ({
  32. setup() {
  33. // test receiving render context
  34. useCssVars((ctx: any) => ({
  35. color: ctx.color
  36. }))
  37. return state
  38. },
  39. render() {
  40. return h('div')
  41. }
  42. }))
  43. })
  44. test('on fragment root', async () => {
  45. await assertCssVars(state => ({
  46. setup() {
  47. useCssVars(() => state)
  48. return () => [h('div'), h('div')]
  49. }
  50. }))
  51. })
  52. test('on HOCs', async () => {
  53. const Child = () => [h('div'), h('div')]
  54. await assertCssVars(state => ({
  55. setup() {
  56. useCssVars(() => state)
  57. return () => h(Child)
  58. }
  59. }))
  60. })
  61. test('on suspense root', async () => {
  62. const state = reactive({ color: 'red' })
  63. const root = document.createElement('div')
  64. let resolveAsync: any
  65. let asyncPromise: any
  66. const AsyncComp = {
  67. setup() {
  68. asyncPromise = new Promise(r => {
  69. resolveAsync = () => {
  70. r(() => h('p', 'default'))
  71. }
  72. })
  73. return asyncPromise
  74. }
  75. }
  76. const App = {
  77. setup() {
  78. useCssVars(() => state)
  79. return () =>
  80. h(Suspense, null, {
  81. default: h(AsyncComp),
  82. fallback: h('div', 'fallback')
  83. })
  84. }
  85. }
  86. render(h(App), root)
  87. await nextTick()
  88. // css vars use with fallback tree
  89. for (const c of [].slice.call(root.children as any)) {
  90. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  91. }
  92. // AsyncComp resolve
  93. resolveAsync()
  94. await asyncPromise.then(() => {})
  95. // Suspense effects flush
  96. await nextTick()
  97. // css vars use with default tree
  98. for (const c of [].slice.call(root.children as any)) {
  99. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  100. }
  101. state.color = 'green'
  102. await nextTick()
  103. for (const c of [].slice.call(root.children as any)) {
  104. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
  105. }
  106. })
  107. test('with subTree changed', async () => {
  108. const state = reactive({ color: 'red' })
  109. const value = ref(true)
  110. const root = document.createElement('div')
  111. const App = {
  112. setup() {
  113. useCssVars(() => state)
  114. return () => (value.value ? [h('div')] : [h('div'), h('div')])
  115. }
  116. }
  117. render(h(App), root)
  118. await nextTick()
  119. // css vars use with fallback tree
  120. for (const c of [].slice.call(root.children as any)) {
  121. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  122. }
  123. value.value = false
  124. await nextTick()
  125. for (const c of [].slice.call(root.children as any)) {
  126. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  127. }
  128. })
  129. // #3894
  130. test('with subTree change inside HOC', async () => {
  131. const state = reactive({ color: 'red' })
  132. const value = ref(true)
  133. const root = document.createElement('div')
  134. const Child: FunctionalComponent = (_, { slots }) => slots.default!()
  135. const App = {
  136. setup() {
  137. useCssVars(() => state)
  138. return () =>
  139. h(Child, null, () =>
  140. value.value ? [h('div')] : [h('div'), h('div')]
  141. )
  142. }
  143. }
  144. render(h(App), root)
  145. await nextTick()
  146. // css vars use with fallback tree
  147. for (const c of [].slice.call(root.children as any)) {
  148. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  149. }
  150. value.value = false
  151. await nextTick()
  152. for (const c of [].slice.call(root.children as any)) {
  153. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  154. }
  155. })
  156. test('with createStaticVNode', async () => {
  157. const state = reactive({ color: 'red' })
  158. const root = document.createElement('div')
  159. const App = {
  160. setup() {
  161. useCssVars(() => state)
  162. return () => [
  163. h('div'),
  164. createStaticVNode('<div>1</div><div><span>2</span></div>', 2),
  165. h('div')
  166. ]
  167. }
  168. }
  169. render(h(App), root)
  170. await nextTick()
  171. for (const c of [].slice.call(root.children as any)) {
  172. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  173. }
  174. })
  175. test('with teleport', async () => {
  176. document.body.innerHTML = ''
  177. const state = reactive({ color: 'red' })
  178. const root = document.createElement('div')
  179. const target = document.body
  180. const App = {
  181. setup() {
  182. useCssVars(() => state)
  183. return () => [h(Teleport, { to: target }, [h('div')])]
  184. }
  185. }
  186. render(h(App), root)
  187. await nextTick()
  188. for (const c of [].slice.call(target.children as any)) {
  189. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  190. }
  191. })
  192. test('with teleport in child slot', async () => {
  193. document.body.innerHTML = ''
  194. const state = reactive({ color: 'red' })
  195. const root = document.createElement('div')
  196. const target = document.body
  197. const Child: FunctionalComponent = (_, { slots }) => {
  198. return h('div', slots.default && slots.default())
  199. }
  200. const App = {
  201. setup() {
  202. useCssVars(() => state)
  203. return () => h(Child, () => [h(Teleport, { to: target }, [h('div')])])
  204. }
  205. }
  206. render(h(App), root)
  207. await nextTick()
  208. for (const c of [].slice.call(target.children as any)) {
  209. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  210. }
  211. })
  212. test('with teleport(change subTree)', async () => {
  213. document.body.innerHTML = ''
  214. const state = reactive({ color: 'red' })
  215. const root = document.createElement('div')
  216. const target = document.body
  217. const toggle = ref(false)
  218. const App = {
  219. setup() {
  220. useCssVars(() => state)
  221. return () => [
  222. h(Teleport, { to: target }, [
  223. h('div'),
  224. toggle.value ? h('div') : null
  225. ])
  226. ]
  227. }
  228. }
  229. render(h(App), root)
  230. await nextTick()
  231. expect(target.children.length).toBe(1)
  232. for (const c of [].slice.call(target.children as any)) {
  233. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  234. }
  235. toggle.value = true
  236. await nextTick()
  237. expect(target.children.length).toBe(2)
  238. for (const c of [].slice.call(target.children as any)) {
  239. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  240. }
  241. })
  242. test('with teleport(disabled)', async () => {
  243. document.body.innerHTML = ''
  244. const state = reactive({ color: 'red' })
  245. const root = document.createElement('div')
  246. const target = document.body
  247. const App = {
  248. setup() {
  249. useCssVars(() => state)
  250. return () => [h(Teleport, { to: target, disabled: true }, [h('div')])]
  251. }
  252. }
  253. expect(() => render(h(App), root)).not.toThrow(TypeError)
  254. await nextTick()
  255. expect(target.children.length).toBe(0)
  256. })
  257. test('with string style', async () => {
  258. document.body.innerHTML = ''
  259. const state = reactive({ color: 'red' })
  260. const root = document.createElement('div')
  261. const disabled = ref(false)
  262. const App = {
  263. setup() {
  264. useCssVars(() => state)
  265. return () => [
  266. h(
  267. 'div',
  268. { style: disabled.value ? 'pointer-events: none' : undefined },
  269. 'foo'
  270. )
  271. ]
  272. }
  273. }
  274. render(h(App), root)
  275. await nextTick()
  276. for (const c of [].slice.call(root.children as any)) {
  277. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  278. }
  279. disabled.value = true
  280. await nextTick()
  281. for (const c of [].slice.call(root.children as any)) {
  282. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  283. }
  284. })
  285. })