React Data GridTesting AG Grid

Here we give some hints on testing AG Grid as part of your application.

End to End (e2e) Testing Copy Link

We recommend using e2e tests to validate AG Grid as part of your application.

There are a number of tools available to help with this, such as Playwright, Cypress or Selenium.

e2e tests are recommended so that AG Grid is run inside a real browser. Mocked browser environments (such as jsdom) can be used for simple unit testing cases, but their limitations can lead to confusing test results.

End to End (e2e) Testing Examples Copy Link

Some examples for how to write end-to-end tests are provided in the AG Grid repo.

While these examples depend on Playwright, we do not necessarily recommend one e2e framework over another.

The e2e examples all target the following example:

Test IDs Copy Link

Many testing libraries allow targeting the data-testid attribute to locate elements on the page to use for assertions or actions. AG Grid provides support for attaching this attribute to many of the interactive elements in the grid. This is achieved by calling the setupAgTestIds function. This module should only be used in a test or development environment, not in production.

import { setupAgTestIds } from 'ag-grid-community'

setupAgTestIds();

The test ID attribute that is targeted by this module can be optionally customised using the following module method:

import { setupAgTestIds } from 'ag-grid-community'

setupAgTestIds({ testIdAttribute: 'data-customattr' })

In addition, utility functions are provided to avoid users having to manually construct the IDs:

import { agTestIdFor } from 'ag-grid-community';

// ...in your test case:
const rowId = 'toyota-celica';
const colId = 'ag-Grid-SelectionColumn';
expect(findByTestId(agTestIdFor.checkbox(rowId, colId))).toBeVisible();

Testing with React Testing Library Copy Link

To illustrate an alternative approach, the following snippet demonstrates testing a simple configuration of AG Grid with the React Testing Library.

import { getByText, getByTestId } from '@testing-library/react';
import '@testing-library/jest-dom';

import { 
    createGrid,
    GridOptions, 
    agTestIdFor,
    setupAgTestIds,
} from 'ag-grid-community';

setupAgTestIds();

function createAgGrid() {
    const div = document.createElement('div');

    const gridOptions: GridOptions = {
        columnDefs: [
            { headerName: 'Make', field: 'make' },
            { headerName: 'Model', field: 'model' },
            { 
                field: 'price', 
                valueFormatter: (params) => '$' + params.value.toLocaleString() 
            },
        ],
        rowData: [
            { make: 'Toyota', model: 'Celica', price: 35000 },
            { make: 'Ford', model: 'Mondeo', price: 32000 },
            { make: 'Porsche', model: 'Boxster', price: 72000 },
        ],
        getRowId: ({ data }) => `${data.make.toLowerCase()}-${data.model.toLowerCase()}`,
    };

    const api = createGrid(div, gridOptions);

    return { div, api };
}

test('examples of some things', async () => {
    const { div, api } = createAgGrid();

    // Get a cell value
    expect(getByTestId(div, agTestIdFor.cell('ford-mondeo', 'make'))).toHaveTextContent('Ford');

    // Test the value formatter by searching for the correct price string
    expect(getByText(div, '$72,000')).toBeDefined();

    // Test via the api even though this is not a recommended approach
    expect(api.getDisplayedRowCount()).toBe(3);
});

jsdom Limitations Copy Link

Test tools such as vitest, Dom Testing Library and Jest often rely on jsdom to mock the browser.

jsdom is a pure JavaScript implementation of many web standards with the goal to emulate enough of a subset of a web browser to be useful for testing. However, there are some limitations to be aware of when using jsdom.

If you are using jsdom for testing, you may encounter issues that suggest the grid is not rendering correctly. However, this is likely caused by jsdom not supporting all the features of a real browser.

The main limitations that can affect AG Grid are:

  • No support for CSS layout - impacts column / row virtualisation
  • No support for innerText property Issue #1245 - impacts some grid components

If you wish to use jsdom for your tests you may find the following polyfill useful if you encounter issues with missing content due to the use of innerText:

// Override the innerText setter to use textContent instead within jsdom based tests
Object.defineProperty(Element.prototype, 'innerText', {
    set(value) {
        this.textContent = value;
    },
});

Where you implement this polyfill may vary depending on your testing setup.