Skip to content

Commit

Permalink
test: [M3-8546] - Add unit tests for DialogTitle component
Browse files Browse the repository at this point in the history
  • Loading branch information
hasyed-akamai committed Nov 28, 2024
1 parent 0285c7a commit 0297ba5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
54 changes: 54 additions & 0 deletions packages/manager/src/components/DialogTitle/DialogTitle.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import userEvent from '@testing-library/user-event';
import React from 'react';

import { renderWithTheme } from 'src/utilities/testHelpers';

import { DialogTitle } from './DialogTitle';

import type { DialogTitleProps } from './DialogTitle';

const mockId = '1';
const mockSubTitle = 'This a basic dialog';
const mockTitle = 'This is a Dialog';

const defaultProps: DialogTitleProps = {
id: mockId,
subtitle: mockSubTitle,
title: mockTitle,
};

describe('DialogTitle', () => {
it('should render title, subtitle and Id', () => {
const { getByRole, getByText } = renderWithTheme(
<DialogTitle {...defaultProps} />
);
expect(getByText(mockTitle)).toBeVisible();
expect(getByText(mockSubTitle)).toBeVisible();
const titleElement = getByRole('heading');
expect(titleElement).toHaveAttribute('id', mockId);
});

it('should not render title when isFetching is true', () => {
const { queryByText } = renderWithTheme(
<DialogTitle isFetching {...defaultProps} />
);
expect(queryByText(mockTitle)).not.toBeInTheDocument();
});

it('should close the dialog Box when clciked on close Button', async () => {
const onCloseMock = vi.fn();
const { getByRole } = renderWithTheme(
<DialogTitle
onClose={onCloseMock({}, 'escapeKeyDown')}
{...defaultProps}
/>
);
const closeButton = getByRole('button', {
name: 'Close',
});
expect(closeButton).toBeInTheDocument();
await userEvent.click(closeButton);
expect(onCloseMock).toHaveBeenCalledTimes(1);
expect(onCloseMock).toHaveBeenCalledWith({}, 'escapeKeyDown');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from 'react';

import type { SxProps, Theme } from '@mui/material';

interface DialogTitleProps {
export interface DialogTitleProps {
className?: string;
id?: string;
isFetching?: boolean;
Expand Down

0 comments on commit 0297ba5

Please sign in to comment.