Skip to content

Example spec for elements which are added/removed from the DOM on button click (dialog modal window) #174

@RVledder

Description

@RVledder

I'm currently working on a project with a material dialog component.

What I find challenging is:

1. Dialog element is not rendered in the container exported from the testing library render method

Typically you'd use:

const { container } = render(Component, ...);

That container contains all the DOM elements you want to test against. But Angular places an empty div with the class 'cdk-overlay-container' at the bottom of the body tag. When triggering that element so that it opens that div gets the content you'd like to show, and the div itself get's a position fixed with a z-index of 1000.

This means that it does not pop up in the container element returned from the render method. How do you assert against it? For now I'm using document.querySelector('.cdk-overlay-container'). Also because it has no role or other attribute to use in a query method of testing library. And is it even correct to use document.querySelector?

2. Asserting on the DOM updates

Test cases:

  • modal should show when a user clicks on the 'trigger' button.
  • modal should return some function when the user clicks on the 'ok' button in the modal
  • modal should hide when a user clicks on the 'ok' button in the modal
  • modal should hide when the user clicks on the 'cancel' button in the modal
  • modal should hide when the user clicks on the backdrop

I'd be interested to see a best in class example for such a component. Perhaps others as well?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions