Skip to content

Commit e6a6463

Browse files
committed
docs(qwik-city): loaders mocking
1 parent 287f18d commit e6a6463

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/docs/src/routes/docs/(qwikcity)/api/index.mdx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ test.each(cases)('should render card with %s %s', async ({text, link}) => {
355355
});
356356
```
357357

358-
> a `goto` prop can be passed to customize the `navigate` behavior during tests
358+
> A `goto` prop can be passed to customize the `navigate` behavior during tests
359359
360360
```tsx title="src/components/button.spec.tsx"
361361
import { $ } from '@builder.io/qwik';
@@ -383,6 +383,53 @@ test('should render the button and navigate', async () => {
383383
});
384384
```
385385

386+
> If you are using `routeLoader$`s in a Qwik Component that you want to test, you can provide a `loaders` prop to mock the data returned by the loaders
387+
388+
```ts title="src/loaders/product.loader.ts"
389+
import { routeLoader$ } from '@builder.io/qwik-city';
390+
391+
export const useProductData = routeLoader$(async () => {
392+
const res = await fetch('https://.../product');
393+
const product = (await res.json()) as Product;
394+
return product;
395+
});
396+
```
397+
398+
```tsx title="src/components/product.tsx"
399+
import { component$ } from '@builder.io/qwik';
400+
401+
import { useProductData } from '../loaders/product.loader';
402+
403+
export const Footer = component$(() => {
404+
const signal = useProductData();
405+
return <footer>Product name: {signal.value.product.name}</footer>;
406+
});
407+
```
408+
409+
```tsx title="src/components/product.spec.tsx"
410+
import { createDOM } from '@builder.io/qwik/testing';
411+
import { test, expect } from 'vitest';
412+
413+
import { Footer } from './product';
414+
import { useProductData } from '../loaders/product.loader';
415+
416+
test('should render footer with product name', async () => {
417+
const { screen, render } = await createDOM();
418+
const loadersMock = [
419+
{
420+
loader: useProductData,
421+
data: { product: { name: 'Test Product' } },
422+
},
423+
];
424+
await render(
425+
<QwikCityMockProvider loaders={loadersMock}>
426+
<Footer />
427+
</QwikCityMockProvider>,
428+
);
429+
expect(screen.innerHTML).toMatchSnapshot();
430+
});
431+
```
432+
386433
## `<RouterOutlet>`
387434

388435
The `RouterOutlet` component is responsible for rendering the matched route at a given moment, it internally uses the [`useContent()`](/docs/(qwikcity)/api/index.mdx#usecontent) to render the current page, as well as all of the nested layouts.

packages/qwik-city/src/runtime/src/qwik-city.runtime.api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ export const QWIK_CITY_SCROLLER = "_qCityScroller";
330330

331331
// @public (undocumented)
332332
export interface QwikCityMockProps {
333+
// (undocumented)
334+
loaders?: loaders?: {
335+
loader: LoaderConstructor;
336+
data: any;
337+
}[];
333338
// (undocumented)
334339
goto?: RouteNavigate;
335340
// (undocumented)

0 commit comments

Comments
 (0)