Skip to content

Commit

Permalink
chore(Canvas): Add missing unit test for Canvas.tsx
Browse files Browse the repository at this point in the history
Add missing test for delete flow functionality through the canvas.

relates: #974
  • Loading branch information
lordrip committed Apr 23, 2024
1 parent 08f0763 commit 0043640
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions packages/ui/src/components/Visualization/Canvas/Canvas.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { render, waitFor, fireEvent, screen} from '@testing-library/react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import { CamelRouteResource, KameletResource } from '../../../models/camel';
import { CamelRouteVisualEntity } from '../../../models/visualization/flows';
import { EntitiesContext } from '../../../providers';
import { CatalogModalContext } from '../../../providers/catalog-modal.provider';
import { VisibleFLowsContextResult } from '../../../providers/visible-flows.provider';
import { CamelRouteResource, KameletResource } from '../../../models/camel';
import { VisibleFLowsContextResult, VisibleFlowsContext } from '../../../providers/visible-flows.provider';
import { TestProvidersWrapper } from '../../../stubs';
import { camelRouteJson } from '../../../stubs/camel-route';
import { kameletJson } from '../../../stubs/kamelet-route';
import { TestProvidersWrapper } from '../../../stubs';
import { act } from 'react-dom/test-utils';
import { Canvas } from './Canvas';

describe('Canvas', () => {
Expand Down Expand Up @@ -43,32 +44,44 @@ describe('Canvas', () => {
});

it('should be able to delete the routes', async () => {
const camelRouteResource = new CamelRouteResource(camelRouteJson);
const routeEntity = camelRouteResource.getVisualEntities();
const removeSpy = jest.spyOn(camelRouteResource, 'removeEntity');
const camelResource = new CamelRouteResource(camelRouteJson);
const routeEntity = camelResource.getVisualEntities();
const removeSpy = jest.spyOn(camelResource, 'removeEntity');
const setCurrentSchemaTypeSpy = jest.fn();
const updateEntitiesFromCamelResourceSpy = jest.fn();
const updateSourceCodeFromEntitiesSpy = jest.fn();

render(
<TestProvidersWrapper
visibleFlows={
{ visibleFlows: { ['route-8888']: true } } as unknown as VisibleFLowsContextResult
}
>
<Canvas entities={routeEntity} />
</TestProvidersWrapper>,
<EntitiesContext.Provider
value={{
camelResource,
entities: camelResource.getEntities(),
visualEntities: camelResource.getVisualEntities(),
currentSchemaType: camelResource.getType(),
setCurrentSchemaType: setCurrentSchemaTypeSpy,
updateEntitiesFromCamelResource: updateEntitiesFromCamelResourceSpy,
updateSourceCodeFromEntities: updateSourceCodeFromEntitiesSpy,
}}
>
<VisibleFlowsContext.Provider
value={{ visibleFlows: { ['route-8888']: true } } as unknown as VisibleFLowsContextResult}
>
<Canvas entities={routeEntity} />
</VisibleFlowsContext.Provider>
</EntitiesContext.Provider>,
);

// Right click anywhere on the container label
const route = screen.getByText('route-8888');
// const route = document.querySelectorAll('.pf-topology__group');
await act(async() => {
await act(async () => {
fireEvent.contextMenu(route);
});

// click the Delete ContextMenuItem
const deleteRoute = screen.getByRole('menuitem', {name: 'Delete'});
// Click the Delete ContextMenuItem
const deleteRoute = screen.getByRole('menuitem', { name: 'Delete' });
expect(deleteRoute).toBeInTheDocument();

await act(() => {
await act(async () => {
fireEvent.click(deleteRoute);
});

Expand All @@ -84,26 +97,24 @@ describe('Canvas', () => {

render(
<TestProvidersWrapper
visibleFlows={
{ visibleFlows: { ['user-source']: true } } as unknown as VisibleFLowsContextResult
}
>
<Canvas entities={kameletEntity} />
</TestProvidersWrapper>,
visibleFlows={{ visibleFlows: { ['user-source']: true } } as unknown as VisibleFLowsContextResult}
>
<Canvas entities={kameletEntity} />
</TestProvidersWrapper>,
);

// Right click anywhere on the container label
const kamelet = screen.getByText('user-source');
// const route = document.querySelectorAll('.pf-topology__group');
await act(async() => {
await act(async () => {
fireEvent.contextMenu(kamelet);
});

// click the Delete ContextMenuItem
const deleteKamelet = screen.getByRole('menuitem', {name: 'Delete'});
const deleteKamelet = screen.getByRole('menuitem', { name: 'Delete' });
expect(deleteKamelet).toBeInTheDocument();

await act(() => {
await act(async () => {
fireEvent.click(deleteKamelet);
});

Expand Down

0 comments on commit 0043640

Please sign in to comment.