Testing
Tips for unit testing Angular components that use bootstrap-ng.
Run tests in a real browser
bootstrap-ng relies on the browser's own elements and APIs, such as <dialog>, ResizeObserver and layout measurements for positioning menus and tooltips. jsdom has no layout engine and lacks some of these APIs, so test components that use bootstrap-ng in a real browser. With the Angular CLI's Vitest runner, set browsers in the test target in angular.json and install Playwright:
"test": {
"builder": "@angular/build:unit-test",
"options": {
"browsers": ["chromiumHeadless"],
"providersFile": "src/test-providers.ts"
}
}npm install --save-dev @vitest/browser-playwright playwrightbootstrap-ng runs its own unit tests this way, in headless Chromium.
Bootstrap's CSS and transitions
The test build uses the options of your app's build target, so when Bootstrap's CSS is in its styles, tests have it too. Components wait for Bootstrap's CSS transitions before they emit events such as shown and hidden. Without the CSS, nothing has a transition and these events are emitted right away. With it, wait for the event instead of expecting it at once:
@Component({
imports: [BsDialog],
template: `<dialog bsDialog #dialog="bsDialog" (hidden)="hidden = true">…</dialog>`,
})
class Host {
readonly dialog = viewChild.required<BsDialog>('dialog');
hidden = false;
}
it('emits hidden after the dialog closes', async () => {
const fixture = TestBed.createComponent(Host);
await fixture.whenStable();
fixture.componentInstance.dialog().show();
await fixture.whenStable();
fixture.componentInstance.dialog().hide();
// Waits for Bootstrap's fade-out transition.
await vi.waitFor(() => expect(fixture.componentInstance.hidden).toBe(true));
}); Alerts, dialogs, drawers, popovers, toasts and tooltips take an animation input. To turn it off in every test, return their configuration functions from the file that providersFile names in the test target:
// src/test-providers.ts
import { provideBsDialogConfig, provideBsTooltipConfig } from 'bootstrap-ng';
export default [
provideBsDialogConfig({ animation: false }),
provideBsTooltipConfig({ animation: false }),
];Find overlays in the document
Tooltips, popovers, typeahead suggestions, toasts and dialogs opened with BsDialogService are added to document.body, or to the open dialog that holds their trigger, not inside the component. Look for them in document instead of the fixture's element:
button.dispatchEvent(new Event('focusin', { bubbles: true }));
await fixture.whenStable();
const tooltip = document.querySelector('.tooltip');
expect(tooltip?.textContent).toContain('Delete this file');Query by role and name
Components set the ARIA roles, states and labels that Bootstrap documents, so tests can find elements the way users and assistive technology do. Query by role and accessible name, for example with Vitest's browser locators or Testing Library, and check states such as aria-expanded and aria-selected instead of class names.
import { page } from 'vitest/browser';
await page.getByRole('button', { name: 'Filters' }).click();
await expect.element(page.getByRole('button', { name: 'Filters' })).toHaveAttribute(
'aria-expanded',
'true',
);Form controls
Form controls such as datepickers, typeaheads and ratings are Angular form controls. Set and read their values through the form control in the test, as you would with a native input. To test typing, change the input's value and dispatch an input event, or use a user-event library.