Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any plans to add support for the HTMLDialogElement? #3294

Open
derekdon opened this issue Nov 23, 2021 · 10 comments
Open

Any plans to add support for the HTMLDialogElement? #3294

derekdon opened this issue Nov 23, 2021 · 10 comments
Labels

Comments

@derekdon
Copy link

Basic info:

  • Node.js version: v16.13.0
  • jsdom version: 18.1.0

Minimal reproduction case

/**
 * show and showModal don't exist (jsdom + HTMLDialogElement)
 */
// Suite('can have state toggled', async ({element}) => {
//   assert.is(element.open, false);
//   element.show();
//   assert.is(element.open, true);
//   element.close();
//   assert.is(element.open, false);
// });

https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement

@zarahzachz
Copy link

zarahzachz commented May 5, 2022

Can anyone speak to the status of this? <dialog> and its show methods are now supported by all modern browsers, but implementation is breaking my unit tests.

Receiving TypeError: dialogNode.show is not a function

Break seems to be related to this change to HTMLDialogElement

@domenic
Copy link
Member

domenic commented May 5, 2022

The status is that we're waiting on you to submit a pull request implementing this feature :)

@Asim-Tahir
Copy link

Asim-Tahir commented Jul 27, 2022

I think #3403 fixes this.

Workaround for jest

beforeAll(() => {
  HTMLDialogElement.prototype.show = jest.fn();
  HTMLDialogElement.prototype.showModal = jest.fn();
  HTMLDialogElement.prototype.close = jest.fn();
});

Workaround for vitest

beforeAll(() => {
  HTMLDialogElement.prototype.show = vi.fn();
  HTMLDialogElement.prototype.showModal = vi.fn();
  HTMLDialogElement.prototype.close = vi.fn();
});

@ardunster
Copy link

It would be great if this could get some more visibility / priority. Using the <dialog> tag increases accessibility functionality (screen readers, keyboard control, automatic focusing on open, etc) over options like hiding/displaying a div to show a dialog and is supported in all the major browsers at this point. We used the workaround listed above to set the functions as prototypes but then also had to rewrite tests against implementation instead of behavior which feels terrible.

@jtrein
Copy link

jtrein commented Oct 5, 2022

Adding to the workaround posted, if you are using Jest, this may help to do some assertions while we wait for #3403 to be merged.

Mocks

HTMLDialogElement.prototype.show = jest.fn(function mock(
  this: HTMLDialogElement
) {
  this.open = true;
});

HTMLDialogElement.prototype.showModal = jest.fn(function mock(
  this: HTMLDialogElement
) {
  this.open = true;
});

HTMLDialogElement.prototype.close = jest.fn(function mock(
  this: HTMLDialogElement
) {
  this.open = false;
});

Assertions:

// Assert `<dialog>` is shown - probably not necessary in a test, but for proof the value is set
await waitFor(() => {
  expect((screen.getByLabelText(/some aria-label on the dialog/i) as HTMLDialogElement).open).toBe(
    true
  );
});

// Assert some `<dialog>` content is shown. Should work as expected in `@testing-library`, for example.
await waitFor(() => {
  expect(screen.getByText(/Some Title/i)).toBeVisible();
});

@redaaax
Copy link

redaaax commented May 16, 2023

HTMLDialogElement.prototype.show = jest.fn(function mock(
  this: HTMLDialogElement
) {
  this.open = true;
});

HTMLDialogElement.prototype.showModal = jest.fn(function mock(
  this: HTMLDialogElement
) {
  this.open = true;
});

HTMLDialogElement.prototype.close = jest.fn(function mock(
  this: HTMLDialogElement
) {
  this.open = false;
});

I get a ReferenceError: HTMLDialogElement is not defined

@crutchcorn
Copy link

@domenic I'd love to help contribute support for this better, but don't know what all this would entail. I'm an experienced engineer, but would love some guidance on what next steps would be so that I can open a PR to fix :)

@Mange
Copy link

Mange commented Aug 16, 2023

When implementing this, I think we should also implement the HTMLDialogElement.returnValue and <form method="dialog"> system.

@stefanonepanedap
Copy link

I am not sure if the current implementation supports it, but the "cancel" event would be a nice addition...

chasenlehara added a commit to bitovi/trainings that referenced this issue Oct 12, 2023
Unfortunately [jsdom doesn’t support HTMLDialogElement](jsdom/jsdom#3294), so removes the `<dialog>` element from one of the demo components.

This also fixes a JSX/SVG error.
chasenlehara added a commit to bitovi/trainings that referenced this issue Oct 12, 2023
Unfortunately [jsdom doesn’t support HTMLDialogElement](jsdom/jsdom#3294), so this removes the `<dialog>` element from one of the demo components.

This also fixes a JSX/SVG error in the `Images` demo.
chasenlehara added a commit to bitovi/trainings that referenced this issue Oct 12, 2023
Unfortunately [jsdom doesn’t support HTMLDialogElement](jsdom/jsdom#3294), so this removes the `<dialog>` element from one of the demo components.

This also fixes a JSX/SVG error in the `Images` demo.
@nezed
Copy link

nezed commented Dec 29, 2023

I was lucky to find that happy-dom already have a very nice basic implementation it.

It still lacks some things like returnValue and keyboard accessibility, maybe this would inspire someone:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests