Skip to content

Commit efc1086

Browse files
committed
test(uselazyhydration): test if props are updated when component is wrapped in Suspense
1 parent ed3c191 commit efc1086

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/composables/useLazyHydration.spec.js

+53
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
defineComponent,
55
getCurrentInstance,
66
h,
7+
Suspense,
78
onMounted,
89
ref,
910
} from 'vue';
@@ -163,6 +164,58 @@ it('should update props even if hydration is delayed', async () => {
163164
expect(lazyCompInstance.props.foo).toBeTruthy();
164165
});
165166

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+
166219
it('should not break if the parent is a renderless component and has been updated', async () => {
167220
const result = {
168221
client: {},

0 commit comments

Comments
 (0)