Skip to content

Commit

Permalink
[#239] Added unit tests for BrokerDetailsBreadcrumb component
Browse files Browse the repository at this point in the history
  • Loading branch information
Msarawan committed Jul 29, 2024
1 parent 9120757 commit 991adae
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import { MemoryRouter } from 'react-router-dom-v5-compat';
import { screen, fireEvent, render, waitForI18n } from '../../../../test-utils';
import {
screen,
fireEvent,
render,
waitForI18n,
waitFor,
} from '../../../../test-utils';
import { BrokerDetailsBreadcrumb } from './BrokerDetailsBreadcrumb';
import { useNavigate } from 'react-router-dom-v5-compat';
import { k8sDelete } from '@openshift-console/dynamic-plugin-sdk';
import { AMQBrokerModel } from '../../../../k8s/models';

jest.mock('react-router-dom-v5-compat', () => ({
...jest.requireActual('react-router-dom-v5-compat'),
useNavigate: jest.fn(),
}));

jest.mock('@openshift-console/dynamic-plugin-sdk', () => ({
k8sDelete: jest.fn(),
}));

describe('BrokerDetailsBreadcrumb', () => {
const name = 'test-1';
const namespace = 'test';
const podName = 'test-1-ss-0';
const navigate = jest.fn();

beforeEach(() => {
(useNavigate as jest.Mock).mockReturnValue(navigate);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should render the component correctly', async () => {
render(
Expand Down Expand Up @@ -35,10 +62,41 @@ describe('BrokerDetailsBreadcrumb', () => {
expect(comp.getByText(`${podName}`)).toBeInTheDocument();
});

it('should navigate back to BrokerList page when click on Brokers BreadcrumbItem', async () => {
const comp = render(
<MemoryRouter>
<BrokerDetailsBreadcrumb
name={name}
namespace={namespace}
podName={podName}
/>
</MemoryRouter>,
);
await waitForI18n(comp);

fireEvent.click(screen.getByText('brokers'));
expect(navigate).toHaveBeenCalledWith(`/k8s/ns/${namespace}/brokers`);
});

it('should navigate back to BrokerPodsList page when click on BrokerPods BreadcrumbItem', async () => {
const comp = render(
<MemoryRouter>
<BrokerDetailsBreadcrumb
name={name}
namespace={namespace}
podName={podName}
/>
</MemoryRouter>,
);
await waitForI18n(comp);

fireEvent.click(screen.getByText(`broker ${name}`));
expect(navigate).toHaveBeenCalledWith(
`/k8s/ns/${namespace}/brokers/${name}`,
);
});

it('should redirect back to BrokerList page when namespace is all-namespaces', async () => {
const name = 'test-1';
const namespace = 'all-namespaces';
const podName = 'test-1-ss-0';
const comp = render(
<MemoryRouter>
<BrokerDetailsBreadcrumb
Expand Down Expand Up @@ -71,4 +129,77 @@ describe('BrokerDetailsBreadcrumb', () => {
fireEvent.click(toggle);
expect(toggle).toHaveAttribute('aria-expanded', 'true');
});

it('should navigate back to PodsList page when click on Apply button in both form and yaml editor', async () => {
const comp = render(
<MemoryRouter>
<BrokerDetailsBreadcrumb
name={name}
namespace={namespace}
podName={podName}
/>
</MemoryRouter>,
);
await waitForI18n(comp);

fireEvent.click(screen.getByTestId('broker-toggle-kebab'));
fireEvent.click(screen.getByText('edit_broker'));

expect(navigate).toHaveBeenCalledWith(
`/k8s/ns/${namespace}/edit-broker/${name}?returnUrl=${encodeURIComponent(
window.location.pathname,
)}`,
);
});

it('should open and close the delete broker modal', async () => {
const comp = render(
<MemoryRouter>
<BrokerDetailsBreadcrumb
name={name}
namespace={namespace}
podName={podName}
/>
</MemoryRouter>,
);
await waitForI18n(comp);

fireEvent.click(screen.getByTestId('broker-toggle-kebab'));
fireEvent.click(screen.getByText('delete_broker'));
expect(screen.getByText('delete_modal_instance_title')).toBeInTheDocument();

fireEvent.click(screen.getByText('cancel'));
await waitFor(() =>
expect(
screen.queryByText('delete_modal_instance_title'),
).not.toBeInTheDocument(),
);
});

it('should delete broker and navigate to the brokers list', async () => {
(k8sDelete as jest.Mock).mockResolvedValue({});
const comp = render(
<MemoryRouter>
<BrokerDetailsBreadcrumb
name={name}
namespace={namespace}
podName={podName}
/>
</MemoryRouter>,
);
await waitForI18n(comp);

fireEvent.click(screen.getByTestId('broker-toggle-kebab'));
fireEvent.click(screen.getByText('delete_broker'));
await waitFor(() => screen.getByText('delete'));
fireEvent.click(screen.getByText('delete'));

await waitFor(() => {
expect(k8sDelete).toHaveBeenCalledWith({
model: AMQBrokerModel,
resource: { metadata: { name, namespace: namespace } },
});
expect(navigate).toHaveBeenCalledWith(`/k8s/ns/${namespace}/brokers`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const BrokerDetailsBreadcrumb: FC<BrokerDetailsBreadcrumbProps> = ({
setIsOpen(isOpen);
};



const onSelect = () => {
setIsOpen(false);
};
Expand Down

0 comments on commit 991adae

Please sign in to comment.