CSSStyleSheet.replace(), Intersection Observer, Element.prototype.scrollTo and others.@porsche-design-system/components-angular package.setupTest.{js|ts} file.Attention
// setupTest.{js|ts}
import '@porsche-design-system/components-angular/jsdom-polyfill';
// single-component.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import type { TabsBarUpdateEventDetail } from '@porsche-design-system/components-angular';
@Component({
selector: 'single-component',
template: `
<p-tabs-bar [activeTabIndex]="tabIndex" (update)="onUpdate($event)">
<button data-testid="button1" type="button">Tab One</button>
<button data-testid="button2" type="button">Tab Two</button>
<button data-testid="button3" type="button">Tab Three</button>
</p-tabs-bar>
<div data-testid="debug">Active Tab: { tabIndex + 1 }</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SingleComponent {
tabIndex: number = 0;
onUpdate(e: CustomEvent<TabsBarUpdateEventDetail>) {
this.tabIndex = e.detail.activeTabIndex;
}
}
// single-component.test.ts
import { componentsReady } from '@porsche-design-system/components-angular';
import { render } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import '@porsche-design-system/components-angular/jsdom-polyfill';
it('should render Tabs Bar from Porsche Design System and use its events', async () => {
const { getByTestId } = await render(SingleComponent);
await componentsReady();
const debug = getByTestId('debug');
const button1 = getByTestId('button1');
const button2 = getByTestId('button2');
const button3 = getByTestId('button3');
expect(debug.innerHTML).toBe('Active Tab: 1');
await userEvent.click(button2);
expect(debug.innerHTML).toBe('Active Tab: 2');
await userEvent.click(button3);
expect(debug.innerHTML).toBe('Active Tab: 3');
await userEvent.click(button1);
expect(debug.innerHTML).toBe('Active Tab: 1');
});
getByRole only search light DOM, so they never reach those elements and find nothing.testing sub-package provides a Shadow counterpart for every Testing Library query. These search light
DOM and Shadow DOM, including nested Shadow DOM.getByShadowAltText, getAllByShadowAltText, queryByShadowAltText, queryAllByShadowAltText,
findByShadowAltText, findAllByShadowAltTextgetByShadowDisplayValue, getAllByShadowDisplayValue, queryByShadowDisplayValue,
queryAllByShadowDisplayValue, findByShadowDisplayValue, findAllByShadowDisplayValuegetByShadowLabelText, getAllByShadowLabelText, queryByShadowLabelText,
queryAllByShadowLabelText, findByShadowLabelText, findAllByShadowLabelTextgetByShadowPlaceholderText, getAllByShadowPlaceholderText, queryByShadowPlaceholderText,
queryAllByShadowPlaceholderText, findByShadowPlaceholderText, findAllByShadowPlaceholderTextgetByShadowRole, getAllByShadowRole, queryByShadowRole, queryAllByShadowRole, findByShadowRole,
findAllByShadowRolegetByShadowTestId, getAllByShadowTestId, queryByShadowTestId, queryAllByShadowTestId,
findByShadowTestId, findAllByShadowTestIdgetByShadowText, getAllByShadowText, queryByShadowText, queryAllByShadowText, findByShadowText,
findAllByShadowTextgetByShadowTitle, getAllByShadowTitle, queryByShadowTitle, queryAllByShadowTitle,
findByShadowTitle, findAllByShadowTitleimport { screen } from '@porsche-design-system/components-angular/testing';
it('should work for p-button', async () => {
const fixture = TestBed.createComponent(SomeComponent);
fixture.detectChanges();
await componentsReady();
expect(screen.getByShadowRole('button')).toBe(document.querySelector('p-button').shadowRoot.querySelector('button'));
});
screen (every query above, bound to document), within, deepQuerySelector, deepQuerySelectorAll,
getAllElementsAndShadowRoots, debug, logShadowDOM, prettyShadowDOM, logRoles, configure and getConfig.Requires a DOM
testing sub-package where no DOM is available throws an error.getByRoleShadowedgetByShadowRolegetByLabelTextShadowedgetByShadowLabelTextgetByTextShadowedgetByShadowTextp-modal, p-flyout, p-sheet, p-drilldownHTMLDialogElement.prototype.show = jest.fn();
HTMLDialogElement.prototype.showModal = jest.fn(function (this: HTMLDialogElement) {
this.setAttribute('open', '');
});
HTMLDialogElement.prototype.close = jest.fn(function (this: HTMLDialogElement) {
this.removeAttribute('open');
});
p-button, p-button-pure, p-checkbox, p-input-date, p-input-email, p-input-month, p-input-number, p-input-password, p-input-search, p-input-tel, p-input-text, p-input-time, p-input-url, p-input-week, p-multi-select, p-pin-code, p-radio-group, p-segmented-control, p-select, p-textareaHTMLElement.prototype.attachInternals = jest.fn(
() =>
({
setFormValue: jest.fn(),
setValidity: jest.fn(),
}) as ElementInternals
);
p-tabs-bar, p-tabs, p-drilldownElement.prototype.animate = vi.fn(
() =>
({
onfinish: null,
cancel: vi.fn(),
finish: vi.fn(),
}) as unknown as Animation
);
