write jest tests import cloneDeep from 'lodash.clonedeep'; import isEqual from…
- User
write jest tests import cloneDeep from 'lodash.clonedeep'; import isEqual from 'lodash.isequal';
import type { ConditionGroupId } from 'ufs-common';
- Assistant
write jest tests import cloneDeep from 'lodash.clonedeep'; import isEqual from 'lodash.isequal';
import type { ConditionGroupId } from 'ufs-common';
import type { ConditionDto } from '$conditions-constructor-v2/entities/condition';
import { getBorrowerAtypicalFormulation, getUpdatedOtherTypicalConditionData, getUpdatedPropagations, } from '../helpers'; import { availableAttributesSelector } from '../slices'; import { updateXmlTreeString } from './updateXmlTreeString';
export interface UpdateXmlTreeAndReturnNewConditionStateProps { formState: ConditionDto; didFormStateChange: boolean; isFirstRender: boolean; selectedPropagationIndex: number; passAll: boolean; conditionGroupId: ConditionGroupId; alwaysAvailableAttributes: boolean; }
export const [useUpdateXmlTreeAndReturnNewConditionState, updateXmlTreeAndReturnNewConditionState] = createAppAsyncThunk<ConditionDto, UpdateXmlTreeAndReturnNewConditionStateProps>( 'updateXmlTreeAndReturnNewConditionState', async ( { alwaysAvailableAttributes, conditionGroupId, didFormStateChange, formState, isFirstRender, passAll, selectedPropagationIndex, }, { getState, dispatch }, ) => { const availableAttributes = availableAttributesSelector(getState()); let currentUpdatedState = formState; let prevUpdatedState = null; const isSavedCovenant = Boolean(formState.id !== '0'); const userTextDocumentation = formState.userTextDocumentation || null; const borrowerPropagationIndex = formState.propagations.findIndex( (it) => it.propagationKind.id === 'BORROWER', ); const { borrowerAtypicalFormulation, userTextDecisionAbsentBorrower } = getBorrowerAtypicalFormulation({ borrowerPropagation: formState.propagations[borrowerPropagationIndex], userTextDecisionAbsentBorrower: formState.userTextDecisionAbsentBorrower, }); while (!isEqual(prevUpdatedState, currentUpdatedState)) { const { actualXmlTree, actualXmlTreeString } = await dispatch( updateXmlTreeString({ formState, isFirstRender, didFormStateChange, selectedPropagationIndex, }), ).unwrap(); prevUpdatedState = cloneDeep(currentUpdatedState); currentUpdatedState = { ...currentUpdatedState, otherTypicalConditionData: getUpdatedOtherTypicalConditionData({ conditionGroupId, otherTypicalConditionData: formState.otherTypicalConditionData, propagations: formState.propagations, }), userTextDocumentation, userTextDecisionAbsentBorrower, propagations: getUpdatedPropagations({ formStateWithFormulation: formState, currentFormState: currentUpdatedState, availableAttributes, xmlTree: actualXmlTree ?? actualXmlTreeString, selectedPropagationIndex, borrowerAtypicalFormulation, borrowerPropagationIndex, isSavedCovenant, passAll, alwaysAvailable: alwaysAvailableAttributes, isFirstRender, }), }; }
return currentUpdatedState;
},
);
To create Jest tests for the updateXmlTreeAndReturnNewConditionState thunk, we will need to mock the necessary dependencies, particularly the dispatch function and any selectors or helper functions that are called within the thunk. The goal here is to ensure that the thunk behaves correctly based on various inputs and state conditions.
Below is an example of how you might set up such tests:
import { updateXmlTreeAndReturnNewConditionState } from './updateXmlTreeAndReturnNewConditionState';
import { updateXmlTreeString } from './updateXmlTreeString';
import { availableAttributesSelector } from '../slices';
import { getBorrowerAtypicalFormulation, getUpdatedOtherTypicalConditionData, getUpdatedPropagations } from '../helpers';
jest.mock('./updateXmlTreeString');
jest.mock('../slices');
jest.mock('../helpers');
describe('updateXmlTreeAndReturnNewConditionState', () => {
const mockDispatch = jest.fn();
const mockGetState = jest.fn();
const initialFormState = {
id: '1',
userTextDocumentation: '',
userTextDecisionAbsentBorrower: '',
propagations: [
// Add mock propagation objects as needed
],
otherTypicalConditionData: {},
};
const mockConditionGroupId = 'test-condition-group-id';
beforeEach(() => {
jest.clearAllMocks();
availableAttributesSelector.mockReturnValue(['attribute1', 'attribute2']); // Mock the selector
updateXmlTreeString.mockResolvedValue({
unwrap: jest.fn().mockResolvedValue({
actualXmlTree: '<mockXmlTree></mockXmlTree>',
actualXmlTreeString: '<mockXmlTreeString></mockXmlTreeString>',
}),
});
getBorrowerAtypicalFormulation.mockReturnValue({
borrowerAtypicalFormulation: {},
userTextDecisionAbsentBorrower: '',
});
});
it('should return updated state when form state changes', async () => {
const props = {
alwaysAvailableAttributes: false,
conditionGroupId: mockConditionGroupId,
didFormStateChange: true,
formState: initialFormState,
isFirstRender: false,
passAll: false,
selectedPropagationIndex: 0,
};
const result = await updateXmlTreeAndReturnNewConditionState(props, { getState: mockGetState, dispatch: mockDispatch });
expect(result).toEqual(expect.objectContaining({
// Check the expected shape of currentUpdatedState
otherTypicalConditionData: expect.any(Object),
userTextDocumentation: expect.any(String),
userTextDecisionAbsentBorrower: expect.any(String),
propagations: expect.any(Array),
}));
});
// Add more tests for other scenarios that you want to handle:
it('should handle first render correctly', async () => {
// Set up specific cases for isFirstRender
const props = {
alwaysAvailableAttributes: true,
conditionGroupId: mockConditionGroupId,
didFormStateChange: false,
formState: initialFormState,
isFirstRender: true,
passAll: true,
selectedPropagationIndex: 1,
};
const result = await updateXmlTreeAndReturnNewConditionState(props, { getState: mockGetState, dispatch: mockDispatch });
// Verify the specific outcome based on this scenario
expect(result).toHaveProperty('propagations');
// Other assertions based on expected behaviors during first render
});
it('should not update state if no change detected', async () => {
// Set up cases to verify when state does not change
const props = {
alwaysAvailableAttributes: true,
conditionGroupId: mockConditionGroupId,
didFormStateChange: false,
formState: initialFormState,
isFirstRender: false,
passAll: false,
selectedPropagationIndex: 0,
};
const result = await updateXmlTreeAndReturnNewConditionState(props, { getState: mockGetState, dispatch: mockDispatch });
// possibility that currentUpdatedState equals formState, so check this
expect(result).toEqual(initialFormState); // Assuming that no change means the formState remains the same
});
});
updateXmlTreeAndReturnNewConditionState thunk using Jest.updateXmlTreeString, availableAttributesSelector, and helper functions to control their outputs and behavior during the test.Not yet included in a published release.