Add a few tests on workflow hooks (#8800)

As title
This commit is contained in:
Thomas Trompette 2024-11-28 17:54:41 +01:00 committed by GitHub
parent 38b83f0866
commit 3573d89c3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,35 @@
import { useMutation } from '@apollo/client';
import { renderHook } from '@testing-library/react';
import { ComputeStepOutputSchemaInput } from '~/generated/graphql';
import { useComputeStepOutputSchema } from '../useComputeStepOutputSchema';
jest.mock('@apollo/client', () => ({
...jest.requireActual('@apollo/client'),
useMutation: jest.fn(),
}));
jest.mock('@/object-metadata/hooks/useApolloMetadataClient', () => ({
useApolloMetadataClient: () => ({}),
}));
describe('useComputeStepOutputSchema', () => {
it('should compute schema successfully', async () => {
const mockInput = { stepId: '123' };
const mockResponse = {
data: { computeStepOutputSchema: { schema: { type: 'object' } } },
};
const mockMutate = jest.fn().mockResolvedValue(mockResponse);
(useMutation as jest.Mock).mockReturnValue([mockMutate]);
const { result } = renderHook(() => useComputeStepOutputSchema());
const response = await result.current.computeStepOutputSchema(
mockInput as unknown as ComputeStepOutputSchemaInput,
);
expect(mockMutate).toHaveBeenCalledWith({
variables: { input: mockInput },
});
expect(response).toEqual(mockResponse);
});
});

View File

@ -0,0 +1,36 @@
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
import { WorkflowVersion } from '@/workflow/types/Workflow';
import { renderHook } from '@testing-library/react';
import { useCreateNewWorkflowVersion } from '../useCreateNewWorkflowVersion';
jest.mock('@/object-record/hooks/useCreateOneRecord', () => ({
useCreateOneRecord: jest.fn(),
}));
describe('useCreateNewWorkflowVersion', () => {
it('should create workflow version', async () => {
const mockCreateOneRecord = jest.fn();
(useCreateOneRecord as jest.Mock).mockImplementation(() => ({
createOneRecord: mockCreateOneRecord,
}));
const workflowVersionData = {
workflowId: '123',
name: 'Test Version',
status: 'draft',
trigger: { type: 'manual' },
steps: [],
};
const { result } = renderHook(() => useCreateNewWorkflowVersion());
await result.current.createNewWorkflowVersion(
workflowVersionData as unknown as WorkflowVersion,
);
expect(useCreateOneRecord).toHaveBeenCalledWith({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});
expect(mockCreateOneRecord).toHaveBeenCalledWith(workflowVersionData);
});
});

View File

@ -0,0 +1,81 @@
import { WorkflowWithCurrentVersion } from '@/workflow/types/Workflow';
import { renderHook } from '@testing-library/react';
import { useCreateStep } from '../useCreateStep';
const mockOpenRightDrawer = jest.fn();
const mockUpdateOneWorkflowVersion = jest.fn();
const mockCreateNewWorkflowVersion = jest.fn();
const mockComputeStepOutputSchema = jest.fn().mockResolvedValue({
data: { computeStepOutputSchema: { type: 'object' } },
});
jest.mock('recoil', () => ({
useRecoilValue: () => 'parent-step-id',
useSetRecoilState: () => jest.fn(),
atom: (params: any) => params,
}));
jest.mock('@/workflow/states/workflowCreateStepFromParentStepIdState', () => ({
workflowCreateStepFromParentStepIdState: 'mockAtomState',
}));
jest.mock('@/ui/layout/right-drawer/hooks/useRightDrawer', () => ({
useRightDrawer: () => ({
openRightDrawer: mockOpenRightDrawer,
}),
}));
jest.mock('@/object-record/hooks/useUpdateOneRecord', () => ({
useUpdateOneRecord: () => ({
updateOneRecord: mockUpdateOneWorkflowVersion,
}),
}));
jest.mock('@/workflow/hooks/useCreateNewWorkflowVersion', () => ({
useCreateNewWorkflowVersion: () => ({
createNewWorkflowVersion: mockCreateNewWorkflowVersion,
}),
}));
jest.mock('@/workflow/hooks/useComputeStepOutputSchema', () => ({
useComputeStepOutputSchema: () => ({
computeStepOutputSchema: mockComputeStepOutputSchema,
}),
}));
jest.mock('@/object-metadata/hooks/useFilteredObjectMetadataItems', () => ({
useFilteredObjectMetadataItems: () => ({
activeObjectMetadataItems: [],
}),
}));
jest.mock('@/workflow/utils/insertStep', () => ({
insertStep: jest
.fn()
.mockImplementation(({ steps, stepToAdd }) => [...steps, stepToAdd]),
}));
describe('useCreateStep', () => {
const mockWorkflow = {
id: '123',
currentVersion: {
id: '456',
status: 'DRAFT',
steps: [],
trigger: { type: 'manual' },
},
versions: [],
};
it('should create step in draft version', async () => {
const { result } = renderHook(() =>
useCreateStep({
workflow: mockWorkflow as unknown as WorkflowWithCurrentVersion,
}),
);
await result.current.createStep('CODE');
expect(mockUpdateOneWorkflowVersion).toHaveBeenCalled();
expect(mockOpenRightDrawer).toHaveBeenCalled();
});
});