@stencil/mock-doc 5.0.0-alpha.16 → 5.0.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# @stencil/mock-doc
|
|
2
|
+
|
|
3
|
+
A minimal mock DOM implementation for server-side rendering and unit testing of Stencil components.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save-dev @stencil/mock-doc
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### `createDocument(html?)` — lightweight document only
|
|
14
|
+
|
|
15
|
+
Use this when you only need a `Document` with no surrounding `window` object:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createDocument, serializeNodeToHtml } from '@stencil/mock-doc';
|
|
19
|
+
|
|
20
|
+
const doc = createDocument('<div class="greeting">Hello</div>');
|
|
21
|
+
const el = doc.querySelector('.greeting');
|
|
22
|
+
el.textContent = 'Hello, world!';
|
|
23
|
+
|
|
24
|
+
console.log(serializeNodeToHtml(el, { outerHtml: true }));
|
|
25
|
+
// <div class="greeting">Hello, world!</div>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### `MockWindow(html?)` — full window environment
|
|
29
|
+
|
|
30
|
+
Use this when your code accesses `window`, `location`, `navigator`, `localStorage`, etc.:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { MockWindow, serializeNodeToHtml } from '@stencil/mock-doc';
|
|
34
|
+
|
|
35
|
+
const win = new MockWindow('<html><body><my-comp></my-comp></body></html>');
|
|
36
|
+
const doc = win.document;
|
|
37
|
+
|
|
38
|
+
const el = doc.querySelector('my-comp');
|
|
39
|
+
el.setAttribute('label', 'Hello');
|
|
40
|
+
|
|
41
|
+
const html = serializeNodeToHtml(doc);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### `parseHtmlToDocument` / `parseHtmlToFragment`
|
|
45
|
+
|
|
46
|
+
Parse an HTML string into a `Document` or a `DocumentFragment`:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { parseHtmlToDocument, parseHtmlToFragment } from '@stencil/mock-doc';
|
|
50
|
+
|
|
51
|
+
const doc = parseHtmlToDocument('<p>Hello</p>');
|
|
52
|
+
const frag = parseHtmlToFragment('<li>one</li><li>two</li>');
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### `serializeNodeToHtml(node, options?)`
|
|
56
|
+
|
|
57
|
+
Serialize any node back to an HTML string. Useful for snapshot tests and SSR output.
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { serializeNodeToHtml } from '@stencil/mock-doc';
|
|
61
|
+
|
|
62
|
+
// Pretty-printed output
|
|
63
|
+
const pretty = serializeNodeToHtml(doc, { prettyHtml: true });
|
|
64
|
+
|
|
65
|
+
// Outer HTML of a single element (includes the element's own tag)
|
|
66
|
+
const outer = serializeNodeToHtml(el, { outerHtml: true });
|
|
67
|
+
|
|
68
|
+
// Serialize shadow roots as Declarative Shadow DOM
|
|
69
|
+
const dsd = serializeNodeToHtml(doc, {
|
|
70
|
+
serializeShadowRoot: 'declarative-shadow-dom',
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Key options:
|
|
75
|
+
|
|
76
|
+
| Option | Default | Description |
|
|
77
|
+
|---|---|---|
|
|
78
|
+
| `prettyHtml` | `false` | Indent and add newlines |
|
|
79
|
+
| `indentSpaces` | `2` (when pretty) | Spaces per indent level |
|
|
80
|
+
| `outerHtml` | `false` | Include the root element's own tag |
|
|
81
|
+
| `removeEmptyAttributes` | `true` | Strip attributes with empty string values |
|
|
82
|
+
| `removeHtmlComments` | `false` | Strip HTML comments |
|
|
83
|
+
| `serializeShadowRoot` | — | `'declarative-shadow-dom'` or `'scoped'` |
|
|
84
|
+
| `fullDocument` | `false` | Always emit a full `<!DOCTYPE html>` document |
|
|
85
|
+
|
|
86
|
+
### `setupGlobal` / `teardownGlobal` — test framework integration
|
|
87
|
+
|
|
88
|
+
Installs a `MockWindow` onto `global` so that `window`, `document`, `customElements`, etc. are available without a browser. Call in `beforeEach`/`afterEach` to get a fresh environment per test:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { setupGlobal, teardownGlobal } from '@stencil/mock-doc';
|
|
92
|
+
|
|
93
|
+
beforeEach(() => setupGlobal(global));
|
|
94
|
+
afterEach(() => teardownGlobal(global));
|
|
95
|
+
|
|
96
|
+
it('reads document.title', () => {
|
|
97
|
+
document.title = 'My Page';
|
|
98
|
+
expect(document.title).toBe('My Page');
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`setupGlobal` returns the `MockWindow` it created, which you can use to reach `window`-level APIs directly if needed.
|
|
103
|
+
|
|
104
|
+
### `patchWindow(win)` — fill gaps in a partial window
|
|
105
|
+
|
|
106
|
+
Useful when running in an environment that has _some_ browser globals but is missing others (e.g. a custom SSR runtime):
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { patchWindow } from '@stencil/mock-doc';
|
|
110
|
+
|
|
111
|
+
patchWindow(globalThis); // fills in any missing window APIs with mock implementations
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Fetch mocks
|
|
115
|
+
|
|
116
|
+
Use `MockRequest`, `MockResponse`, and `MockHeaders` to test code that calls `fetch` without hitting the network:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { MockRequest, MockResponse, MockHeaders } from '@stencil/mock-doc';
|
|
120
|
+
|
|
121
|
+
// Simulate an incoming request
|
|
122
|
+
const req = new MockRequest('/api/data', { method: 'POST' });
|
|
123
|
+
console.log(req.method); // 'POST'
|
|
124
|
+
console.log(req.url); // 'http://localhost/api/data'
|
|
125
|
+
|
|
126
|
+
// Build a response your handler returns
|
|
127
|
+
const res = new MockResponse(JSON.stringify({ ok: true }), {
|
|
128
|
+
status: 200,
|
|
129
|
+
headers: new MockHeaders({ 'content-type': 'application/json' }),
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const body = await res.json(); // { ok: true }
|
|
133
|
+
```
|