useCssVars.spec.ts 10 KB

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