|
4 | 4 | defineComponent,
|
5 | 5 | getCurrentInstance,
|
6 | 6 | h,
|
| 7 | + Suspense, |
7 | 8 | onMounted,
|
8 | 9 | ref,
|
9 | 10 | } from 'vue';
|
@@ -163,6 +164,58 @@ it('should update props even if hydration is delayed', async () => {
|
163 | 164 | expect(lazyCompInstance.props.foo).toBeTruthy();
|
164 | 165 | });
|
165 | 166 |
|
| 167 | +it('should update props even if hydration is delayed (with Suspense)', async () => { |
| 168 | + // <Suspense> is an experimental feature and its API will likely change. |
| 169 | + vi.spyOn(console, 'info').mockImplementation(() => {}); |
| 170 | + |
| 171 | + const result = { |
| 172 | + client: {}, |
| 173 | + server: {}, |
| 174 | + }; |
| 175 | + |
| 176 | + const spyClick = vi.fn(); |
| 177 | + |
| 178 | + const bar = ref(false); |
| 179 | + |
| 180 | + let lazyCompInstance; |
| 181 | + |
| 182 | + const { container } = await withSSRSetup((isClient) => { |
| 183 | + const LazyComp = { |
| 184 | + props: ['foo'], |
| 185 | + setup(props) { |
| 186 | + result[isClient ? 'client' : 'server'] = useLazyHydration(); |
| 187 | + |
| 188 | + lazyCompInstance = getCurrentInstance(); |
| 189 | + |
| 190 | + return () => h('button', { onClick: spyClick }, props.foo); |
| 191 | + }, |
| 192 | + }; |
| 193 | + |
| 194 | + return () => h(Suspense, h(LazyComp, { foo: bar.value })); |
| 195 | + }); |
| 196 | + |
| 197 | + expect(result.client.willPerformHydration).toBe(true); |
| 198 | + expect(result.server.willPerformHydration).toBe(false); |
| 199 | + |
| 200 | + // should have not been hydrated |
| 201 | + triggerEvent('click', container.querySelector('button')); |
| 202 | + expect(spyClick).not.toHaveBeenCalled(); |
| 203 | + expect(container.querySelector('button').innerText).toBe('false'); |
| 204 | + expect(lazyCompInstance.props.foo).toBeFalsy(); |
| 205 | + |
| 206 | + // update props and wait for it to complete |
| 207 | + bar.value = true; |
| 208 | + await flushPromises(); |
| 209 | + |
| 210 | + // should have only updated props |
| 211 | + triggerEvent('click', container.querySelector('button')); |
| 212 | + expect(spyClick).not.toHaveBeenCalled(); |
| 213 | + expect(container.querySelector('button').innerText).toBe('false'); |
| 214 | + expect(lazyCompInstance.props.foo).toBeTruthy(); |
| 215 | + |
| 216 | + vi.restoreAllMocks(); |
| 217 | +}); |
| 218 | + |
166 | 219 | it('should not break if the parent is a renderless component and has been updated', async () => {
|
167 | 220 | const result = {
|
168 | 221 | client: {},
|
|
0 commit comments