Skip to content

Commit df68b56

Browse files
committed
Add instructions and a prompt for testing stencil functional components
1 parent 4e72f84 commit df68b56

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
"visio",
306306
"Vite",
307307
"Vitest",
308+
"vnode",
308309
"vuejs",
309310
"WCAG",
310311
"ytlikecount",
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
applyTo: 'packages/atomic/src/components/**/stencil-*.tsx, packages/atomic/src/components/**/stencil-*.spec.ts'
3+
---
4+
5+
## Scope
6+
- Use these instructions when writing unit tests for Stencil `FunctionalComponent` exports in `packages/atomic`.
7+
- Keep assertions framework-agnostic so that future implementation changes require minimal test updates.
8+
9+
## Required References
10+
- Follow [Atomic component instructions](./atomic.instructions.md) for package conventions.
11+
- Follow [Atomic test instructions](./tests-atomic.instructions.md) for structure, naming, mocking patterns, and running tests.
12+
- Review existing examples such as `packages/atomic/src/components/search/result-template-components/quickview-iframe/stencil-quickview-iframe.spec.ts` before creating new tests.
13+
14+
## Production files are read-only.
15+
16+
Do NOT edit the production files (.tsx)
17+
18+
If you can't create unit tests without modifying the production files, stop and ask the user for help.
19+
20+
## Identify Files to Test
21+
- Components reside under `packages/atomic/src/components/<feature>/<name>/`.
22+
- Each folder contains one primary file named `stencil-<component>.tsx` matching the folder name.
23+
- Additional `.tsx` files in the same folder that export a Stencil `FunctionalComponent` must also receive unit tests.
24+
25+
## Test File Setup
26+
- Place new spec files beside the implementation and name them `stencil-<component>.spec.ts`.
27+
- Test files must use `.ts` (no JSX syntax).
28+
- Import utilities from `@stencil/core` using `type`-only imports to avoid bundling runtime code.
29+
- Use `renderStencilVNode` from `@/vitest-utils/testing-helpers/stencil-vnode-renderer` to mount the virtual node into the DOM prior to assertions.
30+
- Avoid importing `Fragment` from `@stencil/core`; prefer arrays or wrapper elements when a fragment-like structure is required.
31+
32+
### Boilerplate Pattern
33+
```ts
34+
import type {VNode} from '@stencil/core';
35+
import {
36+
afterEach,
37+
beforeEach,
38+
describe,
39+
expect,
40+
it,
41+
vi,
42+
} from 'vitest';
43+
import {renderStencilVNode} from '@/vitest-utils/testing-helpers/stencil-vnode-renderer';
44+
import {Component} from './stencil-component';
45+
46+
describe('Component (Stencil)', () => {
47+
let container: HTMLElement;
48+
49+
beforeEach(() => {
50+
container = document.createElement('div');
51+
document.body.appendChild(container);
52+
});
53+
54+
afterEach(() => {
55+
container.remove();
56+
});
57+
58+
const renderComponent = async (props?: {
59+
/* component-specific props */
60+
}) => {
61+
const vnode = Component(
62+
{
63+
/* component-specific props */
64+
},
65+
[],
66+
// biome-ignore lint/suspicious/noExplicitAny: Stencil FunctionalComponent utils parameter is unused in tests.
67+
{} as any
68+
) as VNode;
69+
70+
await renderStencilVNode(vnode, container);
71+
72+
const element = container.firstElementChild;
73+
74+
return {element};
75+
};
76+
77+
it('should ...', async () => {
78+
const {element} = await renderComponent({/* props */});
79+
expect(element).toBeTruthy();
80+
});
81+
});
82+
```
83+
84+
### Internationalization Helpers
85+
- When a component relies on i18n, use `createTestI18n` from `@/vitest-utils/testing-helpers/i18n-utils`.
86+
87+
```ts
88+
import type {i18n} from 'i18next';
89+
import {createTestI18n} from '@/vitest-utils/testing-helpers/i18n-utils';
90+
91+
describe('Component (Stencil)', () => {
92+
let i18n: i18n;
93+
94+
beforeAll(async () => {
95+
i18n = await createTestI18n();
96+
});
97+
});
98+
```
99+
100+
## Test Guidelines
101+
- Cover every exported `FunctionalComponent` in the folder.
102+
- Focus on observable DOM output and events rather than internal implementation details.
103+
- Prefer helper functions within the spec for repeated setup or assertions.
104+
- Do not edit Stencil production `.tsx` files while authoring tests. If changes seem necessary, pause and escalate.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
agent: agent
3+
description: 'Create unit tests for each Stencil FunctionalComponent in the component folder'
4+
---
5+
6+
Follow the [`tests-atomic-stencil-functional-component`](../instructions/tests-atomic-stencil-functional-component.instructions.md) instructions
7+
8+
The unit tests should be as generic as possible such that the future migration to Lit creates the minimal diff.

0 commit comments

Comments
 (0)