@gluonjs/test-utils


@gluonjs/test-utils / packages/test-utils/src / ComponentFixture

Interface: ComponentFixture<Props>

Extends

Type Parameters

Props

Props extends object

Properties

active

readonly active: boolean

Inherited from

TestFixture.active


app

readonly app: GluonApp

Inherited from

TestFixture.app


container

readonly container: Element

Inherited from

TestFixture.container


id

readonly id: number

Inherited from

TestFixture.id


name

readonly name: string

Inherited from

TestFixture.name


props

readonly props: Props

Methods

cleanup()

cleanup(): void

Returns

void

Inherited from

TestFixture.cleanup


get()

get<ElementType>(selector): ElementType

Type Parameters

ElementType

ElementType extends Element = Element

Parameters

selector

string

Returns

ElementType

Inherited from

TestFixture.get


own()

own(cleanup, label?): void

Parameters

cleanup

() => void | PromiseLike<void>

label?

string

Returns

void

Inherited from

TestFixture.own


query()

query<ElementType>(selector): ElementType | null

Type Parameters

ElementType

ElementType extends Element = Element

Parameters

selector

string

Returns

ElementType | null

Inherited from

TestFixture.query


setProps()

setProps(patch): Promise<void>

Parameters

patch

Partial<Props>

Returns

Promise<void>


text()

text(): string

Returns

string

Inherited from

TestFixture.text

Example

Read reactive component props and patch them while retaining all query, text, ownership, and cleanup operations from the base fixture:

ts
import { createInjectionKey, html } from '@gluonjs/core';
import {
  activeFixtureNames,
  assertNoFixtureLeaks,
  cleanupFixtures,
  flushUpdates,
  installAutoCleanup,
  mountComponent,
  mountElement,
  settle,
  testProvider,
  type AutoCleanupOptions,
  type BaseMountOptions,
  type ComponentFixture,
  type ElementFixture,
  type ElementMountOptions,
  type SettleOptions,
  type TestFixture,
  type TestProvider,
} from '@gluonjs/test-utils';

const currencyKey = createInjectionKey<string>('currency');
const currency: TestProvider<string> = testProvider(currencyKey, 'EUR');
const base = { name: 'product-price', providers: [currency] } satisfies BaseMountOptions;
const component: ComponentFixture<{ price: number }> = mountComponent(
  ({ price }) => html`<output>${price.toFixed(2)}</output>`,
  { ...base, props: { price: 129 } },
);
await component.setProps({ price: 119 });

interface ProductPickerElement extends HTMLElement { value: string; }
const elementOptions = {
  name: 'product-picker',
  attributes: { 'aria-label': 'Product' },
  properties: { value: 'lamp' },
  slots: { default: 'Choose a product' },
} satisfies ElementMountOptions<ProductPickerElement>;
const element: ElementFixture<ProductPickerElement> = mountElement<ProductPickerElement>('product-picker', elementOptions);
const fixture: TestFixture = component;
fixture.own(() => console.log('release request'), 'request');
await flushUpdates(() => element.element.dispatchEvent(new CustomEvent('change')));
const settleOptions = { cycles: 2, timers: false } satisfies SettleOptions;
await settle(settleOptions);
console.log(fixture.text(), element.emitted('change'), activeFixtureNames());

const autoCleanup = { assertBeforeCleanup: true } satisfies AutoCleanupOptions;
installAutoCleanup((cleanup) => { void cleanup; }, autoCleanup);
element.cleanup();
component.cleanup();
await cleanupFixtures();
assertNoFixtureLeaks();