Skip to content

Commit

Permalink
fix: Fix missing scrim background on LoadingOverlay (#2098)
Browse files Browse the repository at this point in the history
Background class wasn't applied when LoadingOverlay has error or is
loading
  • Loading branch information
vbabich authored Jul 9, 2024
1 parent 8e47216 commit c9ed895
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
43 changes: 43 additions & 0 deletions packages/components/src/LoadingOverlay.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import LoadingOverlay from './LoadingOverlay';

describe('LoadingOverlay', () => {
it('renders loading spinner and no scrim on initial loading', () => {
const { container } = render(
<LoadingOverlay isLoading data-testid="test-overlay" />
);
expect(screen.getByTestId('test-overlay-spinner')).toBeInTheDocument();
expect(
container.getElementsByClassName('iris-panel-scrim-background').length
).toBe(0);
});

it('renders error message and no scrim when errorMessage is provided on initial loading', () => {
const errorMessage = 'An error occurred';
const { container } = render(
<LoadingOverlay errorMessage={errorMessage} />
);
const errorElement = screen.getByText(errorMessage);
expect(errorElement).toBeInTheDocument();
expect(
container.getElementsByClassName('iris-panel-scrim-background').length
).toBe(0);
});

it('renders scrim when loaded and errorMessage is provided', () => {
const { container } = render(
<LoadingOverlay isLoaded errorMessage="ERROR_MESSAGE" />
);
expect(
container.getElementsByClassName('iris-panel-scrim-background').length
).toBe(1);
});

it('renders scrim when isLoaded is true and loading again', () => {
const { container } = render(<LoadingOverlay isLoaded isLoading />);
expect(
container.getElementsByClassName('iris-panel-scrim-background').length
).toBe(1);
});
});
12 changes: 9 additions & 3 deletions packages/components/src/LoadingOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type LoadingOverlayProps = {
isLoaded?: boolean;
isLoading?: boolean;
errorMessage?: string | null;
className?: string | null;
scrimClassName?: string;
timeout?: number | { enter?: number; exit?: number; appear?: number };
'data-testid'?: string;
};

Expand All @@ -21,6 +24,9 @@ function LoadingOverlay({
isLoaded = false,
isLoading = true,
errorMessage = null,
className = null,
scrimClassName = 'iris-panel-scrim-background',
timeout = ThemeExport.transitionMs,
'data-testid': dataTestId,
}: LoadingOverlayProps): JSX.Element {
const messageTestId =
Expand All @@ -31,8 +37,8 @@ function LoadingOverlay({
return (
<CSSTransition
in={Boolean(errorMessage) || !isLoaded || isLoading}
timeout={ThemeExport.transitionMs}
classNames={isLoaded ? 'fade' : ''}
timeout={timeout}
classNames={classNames(className, { fade: isLoaded })}
mountOnEnter
unmountOnExit
>
Expand All @@ -41,7 +47,7 @@ function LoadingOverlay({
className={classNames(
'iris-panel-message-overlay',
'fill-parent-absolute',
{ 'iris-panel-scrim-background': isLoaded }
{ [scrimClassName]: isLoaded }
)}
>
<div className="message-content">
Expand Down

0 comments on commit c9ed895

Please sign in to comment.