From cfe5baba2c17c9e75580ee0b08ac195ea1932b26 Mon Sep 17 00:00:00 2001 From: Aaron Chong Date: Wed, 4 Dec 2024 17:20:46 +0800 Subject: [PATCH] Test mock posts Signed-off-by: Aaron Chong --- .../robots/robot-decommission.test.tsx | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/rmf-dashboard-framework/src/components/robots/robot-decommission.test.tsx b/packages/rmf-dashboard-framework/src/components/robots/robot-decommission.test.tsx index e95e2daab..27f8f073e 100644 --- a/packages/rmf-dashboard-framework/src/components/robots/robot-decommission.test.tsx +++ b/packages/rmf-dashboard-framework/src/components/robots/robot-decommission.test.tsx @@ -1,5 +1,5 @@ import { fireEvent } from '@testing-library/react'; -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import { RmfApiProvider } from '../../hooks'; import { MockRmfApi, render, TestProviders } from '../../utils/test-utils.test'; @@ -9,8 +9,16 @@ import { makeRobot } from './test-utils.test'; describe('Robot decommission button', () => { const rmfApi = new MockRmfApi(); // mock out some api calls so they never resolves - rmfApi.fleetsApi.decommissionRobotFleetsNameDecommissionPost = () => new Promise(() => {}); - rmfApi.fleetsApi.recommissionRobotFleetsNameRecommissionPost = () => new Promise(() => {}); + const mockDecommission = vi.fn().mockImplementation(async () => { + // Simulate some asynchronous operation that has no return value + await new Promise((resolve) => setTimeout(resolve, 100)); + }); + const mockRecommission = vi.fn().mockImplementation(async () => { + // Simulate some asynchronous operation that has no return value + await new Promise((resolve) => setTimeout(resolve, 100)); + }); + rmfApi.fleetsApi.decommissionRobotFleetsNameDecommissionPost = mockDecommission; + rmfApi.fleetsApi.recommissionRobotFleetsNameRecommissionPost = mockRecommission; const Base = (props: React.PropsWithChildren<{}>) => { return ( @@ -32,6 +40,9 @@ describe('Robot decommission button', () => { expect(root.getByText('Decommission')).toBeTruthy(); fireEvent.click(root.getByText('Decommission')); expect(root.getByText('Decommission [test_fleet:test_robot]')).toBeTruthy(); + expect(root.getByText('Confirm')); + fireEvent.click(root.getByText('Confirm')); + expect(mockDecommission).toHaveBeenCalledOnce(); }); it('Renders recommission button', () => { @@ -54,5 +65,8 @@ describe('Robot decommission button', () => { expect(root.getByText('Recommission')).toBeTruthy(); fireEvent.click(root.getByText('Recommission')); expect(root.getByText('Recommission [test_fleet:test_robot]')).toBeTruthy(); + expect(root.getByText('Confirm')); + fireEvent.click(root.getByText('Confirm')); + expect(mockRecommission).toHaveBeenCalledOnce(); }); });