Testing a delete-recipe confirmation opened with Angular Material's MatDialog, what can MatDialogHarness check and do, and how do you test the dialog component on its own?
answer
- found from the document root
- a selector, never a title filter
- title, content and actions text
- close() is an Escape press
- a test opener for the dialog alone
basics
~20 sMatDialogHarness, loaded from the document root, reads the title, content and actions text plus role and ARIA attributes, finds buttons inside the dialog, and closes it by pressing Escape. MatTestDialogOpener opens the dialog component alone and records its result.
solid answer
~30 sThe dialog renders outside the component under test, so I load `MatDialogHarness` from the document-root loader. The harness reads `getTitleText()`, `getContentText()` and `getActionsText()` from the dialog's title, content and actions sections, plus `getRole()` and the `aria-*` attributes. It is a content container, so `dialog.getHarness(MatButtonHarness.with({text: 'Delete'}))` finds the confirm button inside that dialog only. Its `close()` sends Escape, which means it does nothing when the dialog was opened with `disableClose: true`, a fact I can use to test that the confirmation cannot be dismissed. To test the dialog component alone, `MatTestDialogOpener` opens it for me.
code
ts · 18 linesimport {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatButtonHarness} from '@angular/material/button/testing';
import {MatDialogHarness} from '@angular/material/dialog/testing';
it('asks before deleting a recipe', async () => {
const fixture = TestBed.createComponent(RecipePage);
const loader = TestbedHarnessEnvironment.loader(fixture);
const rootLoader = TestbedHarnessEnvironment.documentRootLoader(fixture);
await (await loader.getHarness(MatButtonHarness.with({text: 'Delete recipe'}))).click();
const dialog = await rootLoader.getHarness(MatDialogHarness);
expect(await dialog.getTitleText()).toBe('Delete "Lentil soup"?');
expect(await dialog.getRole()).toBe('alertdialog');
const confirm = await dialog.getHarness(MatButtonHarness.with({text: 'Delete'}));
expect(await confirm.isDisabled()).toBe(false);
});go deeper
Recall that MatDialogHarness reads the title, content and actions text and lives in the dialog's testing entry point.
Pick one dialog among several with the selector filter and read its sections and ARIA attributes through the harness.
Show how close() as an Escape press interacts with a non-dismissable dialog, and how MatTestDialogOpener isolates the dialog component.
Decide how a team splits dialog coverage between page-level and dialog-level tests so each behaviour is asserted once.
## The scenario In the recipe-sharing app, the recipe page has a **Delete recipe** button. Clicking it opens a confirmation through Angular Material's `MatDialog`, titled `Delete "Lentil soup"?`, with **Cancel** and **Delete** buttons. The team wants two tests: one on the page, proving the dialog appears with the right text, and one on the dialog component itself, proving Delete returns `true` to the opener. ## Where to load it from The dialog renders outside the component under test, so the page-level test loads `MatDialogHarness` from the CDK testing environment's document-root loader rather than the fixture's loader. That loader is the only piece of the CDK's harness API this test needs; everything below comes from Material's own classes. ## What MatDialogHarness reads and does `MatDialogHarness`, from `@angular/material/dialog/testing`, finds `.mat-mdc-dialog-container` elements. Its filter interface adds nothing to the standard `selector` and `ancestor` options, so to pick one dialog among several you pass a `selector`, for example the `id` you gave in the dialog's config. | Method | Returns | |---|---| | `getTitleText()` | Text of the `mat-dialog-title` section, or `''` if there is none | | `getContentText()` | Text of `mat-dialog-content`, or `''` | | `getActionsText()` | Text of `mat-dialog-actions`, or `''` | | `getText()` | All text in the dialog | | `getRole()` | `'dialog'` or `'alertdialog'` | | `getAriaLabel()`, `getAriaLabelledby()`, `getAriaDescribedby()` | The container's ARIA attributes | | `getId()` | The container's id, or `null` | | `close()` | Sends the Escape key to the container | Because it is a **content container harness**, the dialog harness can load harnesses for what is inside it: `getHarness`, `getAllHarnesses` and `getHarnessOrNull` all search within that dialog only. That keeps a **Delete** button on the page from being confused with the **Delete** button in the dialog. ## The Escape detail `close()` does not call the dialog reference; it presses Escape on the container. Two consequences: - When the dialog was opened with `disableClose: true`, Escape is ignored, so `close()` is a **no-op** and the dialog is still there afterwards. The harness's own tests check exactly this. - A test that wants the dialog's **result** should click the button that produces it, through a button harness, rather than calling `close()`, which closes with no result. ## Testing the dialog component on its own The same entry point exports `MatTestDialogOpener`. `MatTestDialogOpener.withComponent(DeleteRecipeDialog, config)` returns a host component type; creating it opens your dialog through the real `MatDialog`, with the config's `data`, and with enter and exit animations set to zero duration. The fixture's instance then exposes: - `dialogRef`, the real dialog reference, - `closedResult`, whatever the dialog closed with. That lets you test the confirmation without building a page around it. ## What the harness does not cover - **Opening the dialog.** The harness only finds a dialog that is already rendered; the test opens it the way a user does, by clicking the page's button, or through `MatTestDialogOpener`. - **The data passed in.** What the dialog received is visible only through what it renders, so assert the title or content text rather than the injected data object. - **Focus placement.** Which element receives focus when the dialog opens is configured on the dialog and checked through the focused element, for example a button harness's `isFocused()`, not through a dialog harness method. ## Steps for the page-level test 1. Create the page fixture, a fixture loader and a document-root loader. 2. Click **Delete recipe** through `MatButtonHarness` from the fixture loader. 3. Load `MatDialogHarness` from the document-root loader. 4. Assert `getTitleText()`, `getRole()` and `getActionsText()`. 5. Load the dialog's **Delete** button from the dialog harness and click it, or assert that `close()` leaves a non-dismissable confirmation open.
- Two dialogs are open at once. How do you get the harness for the delete confirmation specifically?`MatDialogHarness.with()` accepts only the standard `selector` and `ancestor` filters. Open the confirmation with an `id` in its config and load `MatDialogHarness.with({selector: '#delete-recipe-dialog'})` from the document-root loader, since the container carries that id.
- Why might await dialog.close() leave the confirmation on screen?The harness's `close()` presses Escape on the dialog container. If the dialog was opened with `disableClose: true`, Material ignores Escape and backdrop clicks, so the call changes nothing. That is the behaviour to assert for a non-dismissable confirmation; to close it, click Cancel or Delete through a button harness.
- MatDialogHarness.getTitleText() throws when the dialog has no mat-dialog-title.
- MatDialogHarness.close() calls the dialog reference's close() with no result.
- MatDialogHarness has its own filter for the dialog title text.
- Buttons inside a dialog must be found with document.querySelector.
- You must write your own host component to open a dialog under test.