Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions test-project/src/components/my-button/my-button.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, type RenderResult } from '@stencil/vitest';
import { h } from '@stencil/core';
import { type MyButton } from './my-button';

describe('my-button - spec tests', () => {
let result: RenderResult;
Expand Down Expand Up @@ -143,4 +144,12 @@ describe('my-button - spec tests', () => {
expect(button?.classList.contains('button--danger')).toBe(true);
});
});

it('can get the class instance properties', async () => {
const { instance } = await render<HTMLMyButtonElement, MyButton>(<my-button variant="primary" />);

if (!instance) return;

expect(instance.internalValue).toBe('variant:primary');
});
});
6 changes: 5 additions & 1 deletion test-project/src/components/my-button/my-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { Component, Prop, Event, EventEmitter, h } from '@stencil/core';
shadow: true,
})
export class MyButton {
get internalValue() {
return 'variant:' + this.variant;
}

/**
* The button variant style
*/
Expand All @@ -27,7 +31,7 @@ export class MyButton {
/**
* Emitted when the button is clicked
*/
@Event() buttonClick: EventEmitter<MouseEvent>;
@Event() buttonClick!: EventEmitter<MouseEvent>;

private handleClick = (event: MouseEvent) => {
if (!this.disabled) {
Expand Down
12 changes: 11 additions & 1 deletion test-project/src/components/my-label/my-label.plugin.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ vi.mock('../../utils/index.js', () => ({
// Import the component source — the plugin transforms it on-the-fly
// and the resulting customElements.define() call registers <my-label>
// the moment this import resolves.
import './my-label.tsx';
import { MyLabel } from './my-label';
import './my-label';
import { capitalize } from '../../utils/index.js';

describe('my-label — module mocking via stencilVitestPlugin', () => {
Expand Down Expand Up @@ -61,4 +62,13 @@ describe('my-label — module mocking via stencilVitestPlugin', () => {
expect(root.shadowRoot!.querySelector('span')?.textContent).toBe('UPPER:CHANGED state: value');
expect(capitalize).toHaveBeenCalledTimes(2);
});

it('can get the class instance properties', async () => {
const { instance } = await render<HTMLMyLabelElement, MyLabel>(<my-label value="world" />);

if (!instance) return;

expect(instance.myValue).toBe('hello world');
expect(instance.state).toEqual({ property: 'value' });
});
});
4 changes: 4 additions & 0 deletions test-project/src/components/my-label/my-label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class MyLabel {
/** Raw text to display — run through `capitalize()` before rendering */
@Prop() value: string = '';

get myValue() {
return 'hello' + ' ' + this.value;
}

@State() state = {
property: 'value',
};
Expand Down
5 changes: 2 additions & 3 deletions test-project/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
"types": ["vitest/globals", "@vitest/browser/matchers", "@stencil/vitest/globals"],
"baseUrl": ".",
"paths": {
"@utils": ["src/utils/index.ts"],
"@utils/*": ["src/utils/*"]
"@utils": ["./src/utils/index.ts"],
"@utils/*": ["./src/utils/*"]
}
},
"include": ["src"],
Expand Down
Loading