Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions packages/react/src/components/popover/popover.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChevronDownIcon, ChevronUpIcon } from '@navikt/aksel-icons';

Check failure on line 1 in packages/react/src/components/popover/popover.test.tsx

View workflow job for this annotation

GitHub Actions / Checks

format

File content differs from formatting output

Check failure on line 1 in packages/react/src/components/popover/popover.test.tsx

View workflow job for this annotation

GitHub Actions / Unit Test Report

packages/react/src/components/popover/popover.test.tsx / Popover > when a dialog is open above the popover > should not close the popover with Escape when a modal dialog is open

[2mexpect([22m[31melement[39m[2m).toBeVisible()[22m Received element is not visible: [31m<div class="ds-popover" data-placement="top" data-variant="default" id=":r11:" popover="manual" />[39m
Raw output
Error: expect(element).toBeVisible()

Received element is not visible:
  <div
  class="ds-popover"
  data-placement="top"
  data-variant="default"
  id=":r11:"
  popover="manual"
/>
  src/components/popover/popover.test.tsx:298:45

Check failure on line 1 in packages/react/src/components/popover/popover.test.tsx

View workflow job for this annotation

GitHub Actions / Unit Test Report

packages/react/src/components/popover/popover.test.tsx / Popover > when a dialog is open above the popover > should not call onClose when Escape is pressed with a modal dialog open

expected "vi.fn()" to not be called at all, but actually been called 1 times[90m Received: [1m 1st vi.fn() call: [22m Array [] [39m[90m Number of calls: [1m1[22m [39m
Raw output
AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times

Received:

  1st vi.fn() call:

    Array []


Number of calls: 1

  src/components/popover/popover.test.tsx:376:27

Check failure on line 1 in packages/react/src/components/popover/popover.test.tsx

View workflow job for this annotation

GitHub Actions / Unit Test Report

packages/react/src/components/popover/popover.test.tsx / Popover > when a dialog is open above the popover > should not close the popover with Escape when a modal dialog is open

[2mexpect([22m[31melement[39m[2m).toBeVisible()[22m Received element is not visible: [31m<div class="ds-popover" data-placement="top" data-variant="default" id="_r_g_" popover="manual" />[39m
Raw output
Error: expect(element).toBeVisible()

Received element is not visible:
  <div
  class="ds-popover"
  data-placement="top"
  data-variant="default"
  id="_r_g_"
  popover="manual"
/>
  src/components/popover/popover.test.tsx:298:45

Check failure on line 1 in packages/react/src/components/popover/popover.test.tsx

View workflow job for this annotation

GitHub Actions / Unit Test Report

packages/react/src/components/popover/popover.test.tsx / Popover > when a dialog is open above the popover > should not call onClose when Escape is pressed with a modal dialog open

expected "vi.fn()" to not be called at all, but actually been called 1 times[90m Received: [1m 1st vi.fn() call: [22m Array [] [39m[90m Number of calls: [1m1[22m [39m
Raw output
AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times

Received:

  1st vi.fn() call:

    Array []


Number of calls: 1

  src/components/popover/popover.test.tsx:376:27
import { act, render, screen } from '@testing-library/react';
import { useState } from 'react';

Expand Down Expand Up @@ -229,4 +229,102 @@
expect(onOpen).toHaveBeenCalledTimes(0);
});
});

describe('when a dialog is open above the popover', () => {
it('should not close the popover with Escape when a modal dialog is open', async () => {
render(
<>
<Popover.TriggerContext>
<Popover.Trigger>trigger</Popover.Trigger>
<Popover>{contentText}</Popover>
</Popover.TriggerContext>
<dialog data-testid='test-dialog'>Dialog content</dialog>
</>,
);

const popoverTrigger = screen.getByRole('button');
await act(async () => popoverTrigger.click());
expect(screen.getByText(contentText)).toBeVisible();

/* Open the dialog modally so it covers the popover in the top layer */
const dialog = screen.getByTestId('test-dialog') as HTMLDialogElement;
await act(async () => dialog.showModal());
expect(dialog.open).toBe(true);

/* Pressing Escape should close the dialog but NOT the popover */
const esc = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true });
await act(async () => document.dispatchEvent(esc));

/* Popover should still be visible since it was not the top-layer element */
expect(screen.getByText(contentText)).toBeVisible();
});

it('should close the popover with Escape when no dialog is open', async () => {
render(
<>
<Popover.TriggerContext>
<Popover.Trigger>trigger</Popover.Trigger>
<Popover>{contentText}</Popover>
</Popover.TriggerContext>
<dialog data-testid='test-dialog'>Dialog content</dialog>
</>,
);

const popoverTrigger = screen.getByRole('button');
await act(async () => popoverTrigger.click());
expect(screen.getByText(contentText)).toBeVisible();

/* Dialog is present in DOM but NOT open */
const dialog = screen.getByTestId('test-dialog') as HTMLDialogElement;
expect(dialog.open).toBe(false);

/* Pressing Escape should close the popover since it is the top-layer element */
const esc = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true });
await act(async () => document.dispatchEvent(esc));

expect(screen.getByText(contentText)).not.toBeVisible();
});

it('should not call onClose when Escape is pressed with a modal dialog open', async () => {
const onClose = vi.fn();
render(
<>
<Popover.TriggerContext>
<Popover.Trigger>trigger</Popover.Trigger>
<Popover onClose={onClose}>{contentText}</Popover>
</Popover.TriggerContext>
<dialog data-testid='test-dialog'>Dialog content</dialog>
</>,
);

const popoverTrigger = screen.getByRole('button');
await act(async () => popoverTrigger.click());

const dialog = screen.getByTestId('test-dialog') as HTMLDialogElement;
await act(async () => dialog.showModal());

const esc = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true });
await act(async () => document.dispatchEvent(esc));

expect(onClose).not.toHaveBeenCalled();
});

it('should call onClose when Escape is pressed with the popover on top and no open dialog', async () => {
const onClose = vi.fn();
render(
<Popover.TriggerContext>
<Popover.Trigger>trigger</Popover.Trigger>
<Popover onClose={onClose}>{contentText}</Popover>
</Popover.TriggerContext>,
);

const popoverTrigger = screen.getByRole('button');
await act(async () => popoverTrigger.click());

const esc = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true });
await act(async () => document.dispatchEvent(esc));

expect(onClose).toHaveBeenCalledTimes(1);
});
});
});
Loading