Skip to content

Commit 441b87e

Browse files
jaysin586huntabyte
andauthored
docs: Vitest guide (#140)
Co-authored-by: Hunter Johnston <64506580+huntabyte@users.noreply.github.com>
1 parent d93bde0 commit 441b87e

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

docs/src/content/testing/vitest.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: Vitest
3+
description: How to test with vitest
4+
section: Testing
5+
---
6+
7+
When testing components that use mode-watcher, you'll need to set up a proper testing environment that mocks the `matchMedia` API. This guide will show you how to properly configure your tests.
8+
9+
## Setup
10+
11+
Create a `vitest.setup.ts` file in your project root (or wherever you keep your test configuration) and add the following code:
12+
13+
```ts
14+
import { vi } from "vitest";
15+
16+
const mockMatchMedia = vi.fn().mockImplementation((query) => ({
17+
matches: false,
18+
media: query,
19+
onchange: null,
20+
addEventListener: vi.fn(),
21+
removeEventListener: vi.fn(),
22+
dispatchEvent: vi.fn(),
23+
// Additional properties to better match the MediaQueryList interface
24+
matchMedia: true,
25+
mediaQueryList: true,
26+
// Method to simulate media query changes
27+
simulateChange: (matches: boolean) => {
28+
mockMatchMedia.mock.results[0].value.matches = matches;
29+
if (mockMatchMedia.mock.results[0].value.onchange) {
30+
mockMatchMedia.mock.results[0].value.onchange();
31+
}
32+
},
33+
}));
34+
35+
Object.defineProperty(window, "matchMedia", {
36+
writable: true,
37+
value: mockMatchMedia,
38+
});
39+
```
40+
41+
Then, update your `vitest.config.ts` to include this setup file:
42+
43+
```ts
44+
import { defineConfig } from "vitest/config";
45+
46+
export default defineConfig({
47+
test: {
48+
setupFiles: ["./vitest.setup.ts"],
49+
// ... other config options
50+
},
51+
});
52+
```
53+
54+
## Usage in Tests
55+
56+
With the setup in place, you can now write tests for components that use mode-watcher. Here's an example:
57+
58+
```ts
59+
import { describe, it, expect } from "vitest";
60+
import { render } from "@testing-library/svelte";
61+
import YourComponent from "./YourComponent.svelte";
62+
63+
describe("YourComponent", () => {
64+
it("should render in light mode by default", () => {
65+
const { container } = render(YourComponent);
66+
// Your assertions here
67+
});
68+
});
69+
```
70+
71+
## Important Notes
72+
73+
1. The mock implementation must be set up before any components are rendered that use mode-watcher.
74+
2. The `simulateChange` method allows you to programmatically trigger media query changes in your tests.
75+
3. Make sure to clean up any event listeners in your `afterEach` or `afterAll` blocks if needed.
76+
77+
## Troubleshooting
78+
79+
If you encounter issues with the `matchMedia` mock not working as expected:
80+
81+
1. Verify that your `vitest.setup.ts` file is properly configured in your `vitest.config.ts`
82+
2. Ensure that the mock is set up before any components are rendered
83+
3. Check that you're using the latest version of mode-watcher, as the implementation details may change between versions

docs/src/lib/navigation.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ const utilities = allDocs
2626
href: `/docs/${doc.slug}`,
2727
}));
2828

29+
const testing = allDocs
30+
.filter((doc) => doc.section === "Testing")
31+
.map((doc) => ({
32+
title: doc.title,
33+
href: `/docs/${doc.slug}`,
34+
}));
35+
2936
export const navigation = defineNavigation({
3037
anchors: [
3138
{
@@ -62,5 +69,9 @@ export const navigation = defineNavigation({
6269
title: "Utilities",
6370
items: utilities,
6471
},
72+
{
73+
title: "Testing",
74+
items: testing,
75+
},
6576
],
6677
});

docs/velite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const baseSchema = s.object({
88
navLabel: s.string().optional(),
99
raw: s.raw(),
1010
toc: s.toc(),
11-
section: s.enum(["Overview", "Components", "States", "Utilities"]),
11+
section: s.enum(["Overview", "Components", "States", "Utilities", "Testing"]),
1212
});
1313

1414
const docSchema = baseSchema.transform((data) => {

0 commit comments

Comments
 (0)