useCssVars.spec.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import {
  2. type ComponentOptions,
  3. type FunctionalComponent,
  4. Suspense,
  5. Teleport,
  6. createStaticVNode,
  7. h,
  8. nextTick,
  9. reactive,
  10. ref,
  11. render,
  12. useCssVars,
  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 v-if & async component & suspense', async () => {
  108. const state = reactive({ color: 'red' })
  109. const root = document.createElement('div')
  110. const show = ref(false)
  111. let resolveAsync: any
  112. let asyncPromise: any
  113. const AsyncComp = {
  114. setup() {
  115. useCssVars(() => state)
  116. asyncPromise = new Promise(r => {
  117. resolveAsync = () => {
  118. r(() => h('p', 'default'))
  119. }
  120. })
  121. return asyncPromise
  122. },
  123. }
  124. const App = {
  125. setup() {
  126. return () =>
  127. h(Suspense, null, {
  128. default: h('div', {}, show.value ? h(AsyncComp) : h('p')),
  129. })
  130. },
  131. }
  132. render(h(App), root)
  133. await nextTick()
  134. // AsyncComp resolve
  135. show.value = true
  136. await nextTick()
  137. resolveAsync()
  138. await asyncPromise.then(() => {})
  139. // Suspense effects flush
  140. await nextTick()
  141. // css vars use with default tree
  142. for (const c of [].slice.call(root.children as any)) {
  143. expect(
  144. ((c as any).children[0] as HTMLElement).style.getPropertyValue(
  145. `--color`,
  146. ),
  147. ).toBe(`red`)
  148. }
  149. state.color = 'green'
  150. await nextTick()
  151. for (const c of [].slice.call(root.children as any)) {
  152. expect(
  153. ((c as any).children[0] as HTMLElement).style.getPropertyValue(
  154. `--color`,
  155. ),
  156. ).toBe('green')
  157. }
  158. })
  159. test('with subTree changed', async () => {
  160. const state = reactive({ color: 'red' })
  161. const value = ref(true)
  162. const root = document.createElement('div')
  163. const App = {
  164. setup() {
  165. useCssVars(() => state)
  166. return () => (value.value ? [h('div')] : [h('div'), h('div')])
  167. },
  168. }
  169. render(h(App), root)
  170. await nextTick()
  171. // css vars use with fallback tree
  172. for (const c of [].slice.call(root.children as any)) {
  173. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  174. }
  175. value.value = false
  176. await nextTick()
  177. for (const c of [].slice.call(root.children as any)) {
  178. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  179. }
  180. })
  181. // #3894
  182. test('with subTree change inside HOC', async () => {
  183. const state = reactive({ color: 'red' })
  184. const value = ref(true)
  185. const root = document.createElement('div')
  186. const Child: FunctionalComponent = (_, { slots }) => slots.default!()
  187. const App = {
  188. setup() {
  189. useCssVars(() => state)
  190. return () =>
  191. h(Child, null, () =>
  192. value.value ? [h('div')] : [h('div'), h('div')],
  193. )
  194. },
  195. }
  196. render(h(App), root)
  197. await nextTick()
  198. // css vars use with fallback tree
  199. for (const c of [].slice.call(root.children as any)) {
  200. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
  201. }
  202. value.value = false
  203. await nextTick()
  204. for (const c of [].slice.call(root.children as any)) {
  205. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  206. }
  207. })
  208. test('with createStaticVNode', async () => {
  209. const state = reactive({ color: 'red' })
  210. const root = document.createElement('div')
  211. const App = {
  212. setup() {
  213. useCssVars(() => state)
  214. return () => [
  215. h('div'),
  216. createStaticVNode('<div>1</div><div><span>2</span></div>', 2),
  217. h('div'),
  218. ]
  219. },
  220. }
  221. render(h(App), root)
  222. await nextTick()
  223. for (const c of [].slice.call(root.children as any)) {
  224. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  225. }
  226. })
  227. test('with teleport', async () => {
  228. document.body.innerHTML = ''
  229. const state = reactive({ color: 'red' })
  230. const root = document.createElement('div')
  231. const target = document.body
  232. const App = {
  233. setup() {
  234. useCssVars(() => state)
  235. return () => [h(Teleport, { to: target }, [h('div')])]
  236. },
  237. }
  238. render(h(App), root)
  239. await nextTick()
  240. for (const c of [].slice.call(target.children as any)) {
  241. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  242. }
  243. })
  244. test('with teleport in child slot', async () => {
  245. document.body.innerHTML = ''
  246. const state = reactive({ color: 'red' })
  247. const root = document.createElement('div')
  248. const target = document.body
  249. const Child: FunctionalComponent = (_, { slots }) => {
  250. return h('div', slots.default && slots.default())
  251. }
  252. const App = {
  253. setup() {
  254. useCssVars(() => state)
  255. return () => h(Child, () => [h(Teleport, { to: target }, [h('div')])])
  256. },
  257. }
  258. render(h(App), root)
  259. await nextTick()
  260. for (const c of [].slice.call(target.children as any)) {
  261. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  262. }
  263. })
  264. test('with teleport(change subTree)', async () => {
  265. document.body.innerHTML = ''
  266. const state = reactive({ color: 'red' })
  267. const root = document.createElement('div')
  268. const target = document.body
  269. const toggle = ref(false)
  270. const App = {
  271. setup() {
  272. useCssVars(() => state)
  273. return () => [
  274. h(Teleport, { to: target }, [
  275. h('div'),
  276. toggle.value ? h('div') : null,
  277. ]),
  278. ]
  279. },
  280. }
  281. render(h(App), root)
  282. await nextTick()
  283. expect(target.children.length).toBe(1)
  284. for (const c of [].slice.call(target.children as any)) {
  285. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  286. }
  287. toggle.value = true
  288. await nextTick()
  289. expect(target.children.length).toBe(2)
  290. for (const c of [].slice.call(target.children as any)) {
  291. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  292. }
  293. })
  294. test('with teleport(disabled)', async () => {
  295. document.body.innerHTML = ''
  296. const state = reactive({ color: 'red' })
  297. const root = document.createElement('div')
  298. const target = document.body
  299. const App = {
  300. setup() {
  301. useCssVars(() => state)
  302. return () => [h(Teleport, { to: target, disabled: true }, [h('div')])]
  303. },
  304. }
  305. expect(() => render(h(App), root)).not.toThrow(TypeError)
  306. await nextTick()
  307. expect(target.children.length).toBe(0)
  308. })
  309. test('with string style', async () => {
  310. document.body.innerHTML = ''
  311. const state = reactive({ color: 'red' })
  312. const root = document.createElement('div')
  313. const disabled = ref(false)
  314. const App = {
  315. setup() {
  316. useCssVars(() => state)
  317. return () => [
  318. h(
  319. 'div',
  320. { style: disabled.value ? 'pointer-events: none' : undefined },
  321. 'foo',
  322. ),
  323. ]
  324. },
  325. }
  326. render(h(App), root)
  327. await nextTick()
  328. for (const c of [].slice.call(root.children as any)) {
  329. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  330. }
  331. disabled.value = true
  332. await nextTick()
  333. for (const c of [].slice.call(root.children as any)) {
  334. expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
  335. }
  336. })
  337. })