Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions packages/wds/src/components/snackbar/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ describe('when given snackbar component', () => {
});

it('should not close snackbar if mouse is over the toast container after open', () => {
window.matchMedia = vi.fn().mockReturnValue({ matches: true });

expect(screen.getByTestId('snackbar')).toBeInTheDocument();

act(() => vi.advanceTimersByTime(DEFAULT_DURATION / 2));
Expand All @@ -95,6 +97,20 @@ describe('when given snackbar component', () => {
expect(screen.queryByTestId('snackbar')).not.toBeInTheDocument();
});

it('should close snackbar even if mouse is over the toast container on non-cursor device', () => {
window.matchMedia = vi.fn().mockReturnValue({ matches: false });

expect(screen.getByTestId('snackbar')).toBeInTheDocument();

act(() => vi.advanceTimersByTime(DEFAULT_DURATION / 2));

fireEvent.mouseEnter(screen.getByTestId('snackbar'));

act(() => vi.advanceTimersByTime(DEFAULT_DURATION / 2));

expect(screen.queryByTestId('snackbar')).not.toBeInTheDocument();
});

it('should pass accessibility test', async () => {
expect(await axe(screen.getByTestId('snackbar'))).toHaveNoViolations();
expect(
Expand Down
4 changes: 2 additions & 2 deletions packages/wds/src/components/toast/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CSSProperties } from 'react';

export const isNotPointerDevice = () =>
window.matchMedia('(pointer: coarse)').matches;
export const isCursorDevice = () =>
window.matchMedia('(pointer: fine)').matches;

export const makeTransitionStyle = ({
open,
Expand Down
3 changes: 3 additions & 0 deletions packages/wds/src/components/toast/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useCallback, useEffect, useRef, useState } from 'react';

import { isCursorDevice } from './helpers';

import type { CSSProperties } from 'react';
import type { RegionToastItem } from '../../stores/region-store';

Expand Down Expand Up @@ -78,6 +80,7 @@ export const useToastAnimation = ({

const handleMouseEnter = () => {
if (
isCursorDevice() &&
timeoutRef.current &&
startTimeRef.current &&
remainingTimeRef.current
Expand Down
16 changes: 16 additions & 0 deletions packages/wds/src/components/toast/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ describe('when given toast component', () => {
});

it('should not close toast if mouse is over the toast container after open', () => {
window.matchMedia = vi.fn().mockReturnValue({ matches: true });

expect(screen.getByTestId('toast-container')).toBeInTheDocument();

act(() => vi.advanceTimersByTime(DEFAULT_DURATION / 2));
Expand All @@ -68,6 +70,20 @@ describe('when given toast component', () => {

expect(screen.queryByTestId('toast-container')).not.toBeInTheDocument();
});

it('should close toast even if mouse is over the toast container on non-cursor device', () => {
window.matchMedia = vi.fn().mockReturnValue({ matches: false });

expect(screen.getByTestId('toast-container')).toBeInTheDocument();

act(() => vi.advanceTimersByTime(DEFAULT_DURATION / 2));

fireEvent.mouseEnter(screen.getByTestId('toast-container'));

act(() => vi.advanceTimersByTime(DEFAULT_DURATION / 2));

expect(screen.queryByTestId('toast-container')).not.toBeInTheDocument();
});
});

describe('when given toast component with each variant', () => {
Expand Down
Loading