Angular UI Components Documentation
This guide unit-tests a component that shows a jqxGrid, in a project made with Angular CLI 22 (ng new) and jqwidgets-ng 27.0.1. The tests check that the grid renders its columns and rows, that a grid method works, and that the component handles a grid event.
A new Angular 22 project runs its tests with Vitest, not Karma: ng new sets up the @angular/build:unit-test builder, Vitest and jsdom, and ng test runs every *.spec.ts file. By default the tests run in Node.js, with jsdom standing in for the browser. jQWidgets components need a real browser, so this guide runs Vitest in browser mode, in Chromium through Playwright.
ng new jqwidgets-testing
cd jqwidgets-testing
npm install jqwidgets-ng
Choose CSS for the styles and no server-side rendering. Then add the jQWidgets stylesheets to the styles of the build target in angular.json:
"styles": [
"node_modules/jqwidgets-ng/jqwidgets/styles/jqx.base.css",
"node_modules/jqwidgets-ng/jqwidgets/styles/jqx.fluent.css",
"src/styles.css"
]
The project is the one from Angular CLI with jQWidgets; nothing in it is specific to testing yet.
The test target in angular.json uses the unit-test builder, which builds the spec files with the same compiler as the app and hands them to Vitest:
"test": {
"builder": "@angular/build:unit-test"
}
tsconfig.spec.json includes the spec files and the vitest/globals types, so describe, it, expect and vi need no import. package.json has vitest and jsdom as development dependencies. If you prefer Karma and Jasmine, ng new --test-runner=karma sets that up instead; this guide uses the default.
With the default setup, a spec file that imports a jqwidgets-ng component fails before any test runs:
Error: Cannot find module '…\node_modules\jqwidgets-ng\jqwidgets\modules\jqxgrid'
imported from …\node_modules\jqwidgets-ng\fesm2022\jqwidgets-ng-jqxgrid.mjs
Node.js loads the package's files itself, and jqwidgets-ng 27.1.0 and earlier import their widget code without a file extension, which bundlers accept and Node.js does not. Even when Vitest is told to process the package (server.deps.inline), the grid draws no column headers and no rows in jsdom, which does not lay out the page: every element has a size of zero. Method calls that depend on the drawn grid, such as sorting, fail as well.
In a real browser both problems go away: the builder bundles the test files for the browser, jqwidgets-ng included, and the page has a layout.
Install Vitest's Playwright provider and Playwright, and download Chromium for it once:
npm install --save-dev @vitest/browser-playwright playwright
npx playwright install chromium
Then tell the test target which browser to use, in angular.json:
"test": {
"builder": "@angular/build:unit-test",
"options": {
"browsers": [
"chromiumHeadless"
]
}
}
A browser name that ends with Headless runs the browser without a window. For one run you can pass the same option on the command line instead: ng test --browsers=chromiumHeadless. To watch the tests run, use chromium.
ProductGrid shows four products in a jqxGrid. It sorts the grid by price through the grid's sortby method, and when a row is selected it shows the product's name and emits it through an output.
src/app/product-grid/product-grid.ts:
import { Component, output, signal, viewChild } from '@angular/core';
import { jqxGridComponent, jqxGridModule } from 'jqwidgets-ng/jqxgrid';
export interface Product {
name: string;
category: string;
price: number;
}
@Component({
selector: 'app-product-grid',
imports: [jqxGridModule],
templateUrl: './product-grid.html'
})
export class ProductGrid {
readonly grid = viewChild.required<jqxGridComponent>('grid');
readonly productSelect = output<Product>();
readonly selected = signal<Product | undefined>(undefined);
readonly products: Product[] = [
{ name: 'Espresso', category: 'Coffee', price: 2.2 },
{ name: 'Cappuccino', category: 'Coffee', price: 3.1 },
{ name: 'Green Tea', category: 'Tea', price: 2.5 },
{ name: 'Croissant', category: 'Bakery', price: 2.4 }
];
readonly source = new jqx.dataAdapter({
localdata: this.products,
datatype: 'array',
datafields: [
{ name: 'name', type: 'string' },
{ name: 'category', type: 'string' },
{ name: 'price', type: 'number' }
]
});
readonly columns = [
{ text: 'Product', datafield: 'name' },
{ text: 'Category', datafield: 'category', width: 120 },
{ text: 'Price', datafield: 'price', width: 80, cellsformat: 'c2', cellsalign: 'right' }
];
sortByPrice(): void {
this.grid().sortby('price', 'asc');
}
onRowSelect(event: any): void {
const product: Product = event.args.row;
this.selected.set(product);
this.productSelect.emit(product);
}
}
src/app/product-grid/product-grid.html:
<jqxGrid #grid
[theme]="'fluent'"
[width]="420"
[autoheight]="true"
[sortable]="true"
[source]="source"
[columns]="columns"
(onRowselect)="onRowSelect($event)">
</jqxGrid>
@if (selected(); as product) {
<p class="selection">Selected: {{ product.name }}</p>
}
src/app/product-grid/product-grid.spec.ts:
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { userEvent } from 'vitest/browser';
import { Product, ProductGrid } from './product-grid';
describe('ProductGrid', () => {
let fixture: ComponentFixture<ProductGrid>;
let element: HTMLElement;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ProductGrid]
}).compileComponents();
fixture = TestBed.createComponent(ProductGrid);
element = fixture.nativeElement;
await fixture.whenStable();
});
function cellTexts(column: number): string[] {
return [...element.querySelectorAll('[role=row]')]
.map((row) => row.querySelectorAll('[role=gridcell]')[column]?.textContent?.trim() ?? '')
.filter((text) => text !== '');
}
it('renders the column headers', () => {
const headers = [...element.querySelectorAll('[role=columnheader]')].map((header) => header.textContent?.trim());
expect(headers).toEqual(['Product', 'Category', 'Price']);
});
it('renders a row for each product', () => {
expect(cellTexts(0)).toEqual(['Espresso', 'Cappuccino', 'Green Tea', 'Croissant']);
expect(cellTexts(2)).toEqual(['$2.20', '$3.10', '$2.50', '$2.40']);
});
it('sorts the rows when sortByPrice() calls the grid method', async () => {
fixture.componentInstance.sortByPrice();
await fixture.whenStable();
expect(cellTexts(0)).toEqual(['Espresso', 'Croissant', 'Green Tea', 'Cappuccino']);
expect(fixture.componentInstance.grid().getsortinformation().sortcolumn).toBe('price');
});
it('handles the rowselect event', async () => {
const emitted: Product[] = [];
fixture.componentInstance.productSelect.subscribe((product) => emitted.push(product));
fixture.componentInstance.grid().selectrow(2);
// The grid raises rowselect after a timeout, so wait for the handler to run.
await vi.waitFor(() => expect(emitted.length).toBe(1));
await fixture.whenStable();
expect(emitted[0].name).toBe('Green Tea');
expect(element.querySelector('.selection')?.textContent).toBe('Selected: Green Tea');
});
it('selects the row the user clicks', async () => {
const cell = [...element.querySelectorAll('[role=gridcell]')].find((c) => c.textContent?.trim() === 'Croissant');
await userEvent.click(cell!);
await vi.waitFor(() => expect(element.querySelector('.selection')?.textContent).toBe('Selected: Croissant'));
expect(fixture.componentInstance.grid().getselectedrowindex()).toBe(3);
});
});
TestBed.createComponent creates ProductGrid and its grid. await fixture.whenStable() waits until Angular has rendered the template and the grid has been created, so every test starts with a drawn grid.role="columnheader" and its cells role="gridcell", so the tests read the rendered text the way a user would see it, formatted prices included.sortByPrice() calls the grid's sortby method through the viewChild query. The test checks the new row order on the page and asks the grid for its sort state with getsortinformation().selectrow(2) selects a row through the grid's API. The grid raises rowselect in a timer, not at once, so the test waits with vi.waitFor until the component's onRowSelect handler has emitted productSelect, and then checks the emitted product and the rendered text.userEvent.click from vitest/browser clicks the cell with a real mouse event in Chromium, so the grid goes through the same code as for a user.ng generate component also creates a spec file for each new component. Once a component's template uses a jqwidgets-ng component, its spec file needs the browser setup above as well.
ng test
ng test builds the spec files, starts Chromium and runs the tests, then keeps watching the files and runs them again when you save. For a single run, as on a build server, add --watch=false. With the verbose reporter, each test is listed:
ng test --watch=false --reporters=verbose
❯ Building...
✔ Building...
Application bundle generation complete. [0.963 seconds] - 2026-10-07T10:07:26.184Z
RUN v5.0.3 …/jqwidgets-testing
✓ |jqwidgets-testing (chromium)| src/app/product-grid/product-grid.spec.ts > ProductGrid > renders the column headers 54ms
✓ |jqwidgets-testing (chromium)| src/app/product-grid/product-grid.spec.ts > ProductGrid > renders a row for each product 18ms
✓ |jqwidgets-testing (chromium)| src/app/product-grid/product-grid.spec.ts > ProductGrid > sorts the rows when sortByPrice() calls the grid method 18ms
✓ |jqwidgets-testing (chromium)| src/app/product-grid/product-grid.spec.ts > ProductGrid > handles the rowselect event 71ms
✓ |jqwidgets-testing (chromium)| src/app/product-grid/product-grid.spec.ts > ProductGrid > selects the row the user clicks 107ms
Test Files 1 passed (1)
Tests 5 passed (5)
Start at 13:07:26
Duration 1.50s (tests 39%, setup 26%, import 22%, worker 14%)
Without a license key, jQWidgets writes an evaluation notice to the browser console, and Vitest prints it as stdout lines between the results; those lines are left out above.
getrows(), getselectedrowindex(), getsortinformation()) and the rendered DOM, not through the widget's internal fields.rowselect, reach your handler after the call that caused them. Wait for the result with vi.waitFor instead of a fixed delay.More: Angular CLI with jQWidgets, the Angular Grid tutorials and Themes in Angular.