|
1 |
| -// TODO port test cases from packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts |
| 1 | +import { |
| 2 | + createComponent, |
| 3 | + createIf, |
| 4 | + defineVaporComponent, |
| 5 | + renderEffect, |
| 6 | + setStyle, |
| 7 | + template, |
| 8 | + vaporUseCssVars, |
| 9 | +} from '@vue/runtime-vapor' |
| 10 | +import { nextTick, onMounted, reactive, ref } from '@vue/runtime-core' |
| 11 | +import { makeRender } from '../_utils' |
| 12 | +import type { VaporComponent } from '../../src/component' |
| 13 | + |
| 14 | +const define = makeRender() |
| 15 | + |
2 | 16 | describe('vaporUseCssVars', () => {
|
| 17 | + async function assertCssVars(getApp: (state: any) => VaporComponent) { |
| 18 | + const state = reactive({ color: 'red' }) |
| 19 | + const App = getApp(state) |
| 20 | + const root = document.createElement('div') |
| 21 | + |
| 22 | + define(App).render({}, root) |
| 23 | + for (const c of [].slice.call(root.children as any)) { |
| 24 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`) |
| 25 | + } |
| 26 | + |
| 27 | + state.color = 'green' |
| 28 | + await nextTick() |
| 29 | + for (const c of [].slice.call(root.children as any)) { |
| 30 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green') |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + test('basic', async () => { |
| 35 | + const t0 = template('<div></div>') |
| 36 | + await assertCssVars(state => ({ |
| 37 | + setup() { |
| 38 | + vaporUseCssVars(() => state) |
| 39 | + const n0 = t0() |
| 40 | + return n0 |
| 41 | + }, |
| 42 | + })) |
| 43 | + }) |
| 44 | + |
| 45 | + test('on multiple root', async () => { |
| 46 | + const t0 = template('<div></div>') |
| 47 | + await assertCssVars(state => ({ |
| 48 | + setup() { |
| 49 | + vaporUseCssVars(() => state) |
| 50 | + const n0 = t0() |
| 51 | + const n1 = t0() |
| 52 | + return [n0, n1] |
| 53 | + }, |
| 54 | + })) |
| 55 | + }) |
| 56 | + |
| 57 | + test('on HOCs', async () => { |
| 58 | + const t0 = template('<div></div>') |
| 59 | + const Child = defineVaporComponent({ |
| 60 | + setup() { |
| 61 | + const n0 = t0() |
| 62 | + return n0 |
| 63 | + }, |
| 64 | + }) |
| 65 | + await assertCssVars(state => ({ |
| 66 | + setup() { |
| 67 | + vaporUseCssVars(() => state) |
| 68 | + return createComponent(Child) |
| 69 | + }, |
| 70 | + })) |
| 71 | + }) |
| 72 | + |
| 73 | + test.todo('on suspense root', async () => {}) |
| 74 | + |
| 75 | + test.todo('with v-if & async component & suspense', async () => {}) |
| 76 | + |
| 77 | + test('with subTree changes', async () => { |
| 78 | + const state = reactive({ color: 'red' }) |
| 79 | + const value = ref(true) |
| 80 | + const root = document.createElement('div') |
| 81 | + const t0 = template('<div></div>') |
| 82 | + |
| 83 | + define({ |
| 84 | + setup() { |
| 85 | + vaporUseCssVars(() => state) |
| 86 | + const n0 = createIf( |
| 87 | + () => value.value, |
| 88 | + () => { |
| 89 | + const n2 = t0() |
| 90 | + return n2 |
| 91 | + }, |
| 92 | + () => { |
| 93 | + const n4 = t0() |
| 94 | + const n5 = t0() |
| 95 | + return [n4, n5] |
| 96 | + }, |
| 97 | + ) |
| 98 | + return n0 |
| 99 | + }, |
| 100 | + }).render({}, root) |
| 101 | + |
| 102 | + // css vars use with fallback tree |
| 103 | + for (const c of [].slice.call(root.children as any)) { |
| 104 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`) |
| 105 | + } |
| 106 | + |
| 107 | + value.value = false |
| 108 | + await nextTick() |
| 109 | + for (const c of [].slice.call(root.children as any)) { |
| 110 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red') |
| 111 | + } |
| 112 | + }) |
| 113 | + |
| 114 | + test('with subTree change inside HOC', async () => { |
| 115 | + const state = reactive({ color: 'red' }) |
| 116 | + const value = ref(true) |
| 117 | + const root = document.createElement('div') |
| 118 | + |
| 119 | + const Child = defineVaporComponent({ |
| 120 | + setup(_, { slots }) { |
| 121 | + return slots.default!() |
| 122 | + }, |
| 123 | + }) |
| 124 | + |
| 125 | + const t0 = template('<div></div>') |
| 126 | + define({ |
| 127 | + setup() { |
| 128 | + vaporUseCssVars(() => state) |
| 129 | + return createComponent(Child, null, { |
| 130 | + default: () => { |
| 131 | + return createIf( |
| 132 | + () => value.value, |
| 133 | + () => { |
| 134 | + const n2 = t0() |
| 135 | + return n2 |
| 136 | + }, |
| 137 | + () => { |
| 138 | + const n4 = t0() |
| 139 | + const n5 = t0() |
| 140 | + return [n4, n5] |
| 141 | + }, |
| 142 | + ) |
| 143 | + }, |
| 144 | + }) |
| 145 | + }, |
| 146 | + }).render({}, root) |
| 147 | + |
| 148 | + await nextTick() |
| 149 | + // css vars use with fallback tree |
| 150 | + for (const c of [].slice.call(root.children as any)) { |
| 151 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`) |
| 152 | + } |
| 153 | + |
| 154 | + value.value = false |
| 155 | + await nextTick() |
| 156 | + for (const c of [].slice.call(root.children as any)) { |
| 157 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red') |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | + test.todo('with teleport', async () => {}) |
| 162 | + |
| 163 | + test.todo('with teleport in child slot', async () => {}) |
| 164 | + |
| 165 | + test('with teleport(change subTree)', async () => {}) |
| 166 | + |
| 167 | + test('with teleport(disabled)', async () => {}) |
| 168 | + |
| 169 | + test('with string style', async () => { |
| 170 | + const state = reactive({ color: 'red' }) |
| 171 | + const root = document.createElement('div') |
| 172 | + const disabled = ref(false) |
| 173 | + const t0 = template('<h1></h1>') |
| 174 | + |
| 175 | + define({ |
| 176 | + setup() { |
| 177 | + vaporUseCssVars(() => state) |
| 178 | + const n0 = t0() as any |
| 179 | + renderEffect(() => |
| 180 | + setStyle(n0, state.color ? 'pointer-events: none' : undefined), |
| 181 | + ) |
| 182 | + return n0 |
| 183 | + }, |
| 184 | + }).render({}, root) |
| 185 | + |
| 186 | + await nextTick() |
| 187 | + for (const c of [].slice.call(root.children as any)) { |
| 188 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red') |
| 189 | + } |
| 190 | + |
| 191 | + disabled.value = true |
| 192 | + await nextTick() |
| 193 | + for (const c of [].slice.call(root.children as any)) { |
| 194 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red') |
| 195 | + } |
| 196 | + }) |
| 197 | + |
| 198 | + test('with delay mount child', async () => { |
| 199 | + const state = reactive({ color: 'red' }) |
| 200 | + const value = ref(false) |
| 201 | + const root = document.createElement('div') |
| 202 | + |
| 203 | + const Child = defineVaporComponent({ |
| 204 | + setup() { |
| 205 | + onMounted(() => { |
| 206 | + const childEl = root.children[0] |
| 207 | + expect(getComputedStyle(childEl!).getPropertyValue(`--color`)).toBe( |
| 208 | + `red`, |
| 209 | + ) |
| 210 | + }) |
| 211 | + return template('<div id="childId"></div>')() |
| 212 | + }, |
| 213 | + }) |
| 214 | + |
| 215 | + define({ |
| 216 | + setup() { |
| 217 | + vaporUseCssVars(() => state) |
| 218 | + return createIf( |
| 219 | + () => value.value, |
| 220 | + () => createComponent(Child), |
| 221 | + () => template('<div></div>')(), |
| 222 | + ) |
| 223 | + }, |
| 224 | + }).render({}, root) |
| 225 | + |
| 226 | + await nextTick() |
| 227 | + // css vars use with fallback tree |
| 228 | + for (const c of [].slice.call(root.children as any)) { |
| 229 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`) |
| 230 | + } |
| 231 | + |
| 232 | + // mount child |
| 233 | + value.value = true |
| 234 | + await nextTick() |
| 235 | + for (const c of [].slice.call(root.children as any)) { |
| 236 | + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`) |
| 237 | + } |
| 238 | + }) |
| 239 | + |
| 240 | + test.todo('with custom element', async () => {}) |
| 241 | + |
| 242 | + test('should set vars before child component onMounted hook', () => { |
| 243 | + const state = reactive({ color: 'red' }) |
| 244 | + const root = document.createElement('div') |
| 245 | + let colorInOnMount |
| 246 | + |
| 247 | + define({ |
| 248 | + setup() { |
| 249 | + vaporUseCssVars(() => state) |
| 250 | + onMounted(() => { |
| 251 | + colorInOnMount = ( |
| 252 | + root.children[0] as HTMLElement |
| 253 | + ).style.getPropertyValue(`--color`) |
| 254 | + }) |
| 255 | + return template('<div></div>')() |
| 256 | + }, |
| 257 | + }).render({}, root) |
3 | 258 |
|
| 259 | + expect(colorInOnMount).toBe(`red`) |
| 260 | + }) |
4 | 261 | })
|
0 commit comments