-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented events & fixed tests
- Loading branch information
1 parent
32857eb
commit a550b96
Showing
43 changed files
with
954 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
packages/ui/src/components/organisms/Form/DynamicForm/DynamicForm.unit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import { cleanup, render } from '@testing-library/react'; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { Renderer } from '../../Renderer'; | ||
import { ValidatorProvider } from '../Validator'; | ||
import { DynamicFormContext } from './context'; | ||
import { DynamicFormV2 } from './DynamicForm'; | ||
import { useSubmit } from './hooks/external'; | ||
import { useFieldHelpers } from './hooks/internal/useFieldHelpers'; | ||
import { useTouched } from './hooks/internal/useTouched'; | ||
import { useValidationSchema } from './hooks/internal/useValidationSchema'; | ||
import { useValues } from './hooks/internal/useValues'; | ||
import { ICommonFieldParams, IDynamicFormProps, IFormElement, TBaseFormElements } from './types'; | ||
|
||
// Mock dependencies | ||
vi.mock('../../Renderer'); | ||
|
||
vi.mock('../Validator'); | ||
|
||
vi.mock('./hooks/external/useSubmit'); | ||
|
||
vi.mock('./hooks/internal/useFieldHelpers'); | ||
|
||
vi.mock('./hooks/internal/useTouched'); | ||
|
||
vi.mock('./hooks/internal/useValidationSchema'); | ||
|
||
vi.mock('./hooks/internal/useValues'); | ||
|
||
vi.mock('./context', () => ({ | ||
DynamicFormContext: { | ||
Provider: vi.fn(({ children, value }: any) => { | ||
return <div data-testid="context-provider">{children}</div>; | ||
}), | ||
}, | ||
})); | ||
|
||
describe('DynamicFormV2', () => { | ||
beforeEach(() => { | ||
cleanup(); | ||
vi.restoreAllMocks(); | ||
|
||
vi.mocked(Renderer).mockImplementation(({ children }: any) => { | ||
return <div data-testid="renderer">{children}</div>; | ||
}); | ||
vi.mocked(ValidatorProvider).mockImplementation(({ children }: any) => { | ||
return <div data-testid="validator">{children}</div>; | ||
}); | ||
|
||
vi.mocked(useTouched).mockReturnValue({ | ||
touched: {}, | ||
setTouched: vi.fn(), | ||
setFieldTouched: vi.fn(), | ||
touchAllFields: vi.fn(), | ||
} as any); | ||
vi.mocked(useFieldHelpers).mockReturnValue({ | ||
getTouched: vi.fn(), | ||
getValue: vi.fn(), | ||
setTouched: vi.fn(), | ||
setValue: vi.fn(), | ||
} as any); | ||
vi.mocked(useSubmit).mockReturnValue({ submit: vi.fn() } as any); | ||
vi.mocked(useValidationSchema).mockReturnValue([] as any); | ||
vi.mocked(useValues).mockReturnValue({ | ||
values: {}, | ||
setValues: vi.fn(), | ||
setFieldValue: vi.fn(), | ||
} as any); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
const mockProps = { | ||
elements: [], | ||
values: {}, | ||
validationParams: {}, | ||
elementsMap: {}, | ||
onChange: vi.fn(), | ||
onFieldChange: vi.fn(), | ||
onSubmit: vi.fn(), | ||
onEvent: vi.fn(), | ||
} as unknown as IDynamicFormProps<object, TBaseFormElements>; | ||
|
||
it('should render without crashing', () => { | ||
render(<DynamicFormV2 {...mockProps} />); | ||
}); | ||
|
||
it('should pass elements to useValidationSchema', () => { | ||
const elements = [{ id: 'test', element: 'text' }] as unknown as Array< | ||
IFormElement<TBaseFormElements, ICommonFieldParams> | ||
>; | ||
render(<DynamicFormV2 {...mockProps} elements={elements} />); | ||
expect(useValidationSchema).toHaveBeenCalledWith(elements); | ||
}); | ||
|
||
it('should pass correct props to useValues', () => { | ||
render(<DynamicFormV2 {...mockProps} />); | ||
expect(useValues).toHaveBeenCalledWith({ | ||
values: mockProps.values, | ||
onChange: mockProps.onChange, | ||
onFieldChange: mockProps.onFieldChange, | ||
}); | ||
}); | ||
|
||
it('should pass correct props to useTouched', () => { | ||
render(<DynamicFormV2 {...mockProps} />); | ||
expect(useTouched).toHaveBeenCalledWith(mockProps.elements, mockProps.values); | ||
}); | ||
|
||
it('should pass correct props to useFieldHelpers', () => { | ||
render(<DynamicFormV2 {...mockProps} />); | ||
expect(useFieldHelpers).toHaveBeenCalledWith({ | ||
valuesApi: useValues({ | ||
values: mockProps.values, | ||
onChange: mockProps.onChange, | ||
onFieldChange: mockProps.onFieldChange, | ||
}), | ||
touchedApi: useTouched(mockProps.elements, mockProps.values), | ||
}); | ||
}); | ||
|
||
it('should pass correct props to useSubmit', () => { | ||
render(<DynamicFormV2 {...mockProps} />); | ||
expect(useSubmit).toHaveBeenCalledWith({ | ||
values: mockProps.values, | ||
onSubmit: mockProps.onSubmit, | ||
}); | ||
}); | ||
it('should pass context to DynamicFormContext.Provider', () => { | ||
const touchedMock = { | ||
touched: { field1: true }, | ||
setTouched: vi.fn(), | ||
setFieldTouched: vi.fn(), | ||
touchAllFields: vi.fn(), | ||
}; | ||
const valuesMock = { | ||
values: { field1: 'value1' }, | ||
setValues: vi.fn(), | ||
setFieldValue: vi.fn(), | ||
}; | ||
const submitMock = { submit: vi.fn() }; | ||
const fieldHelpersMock = { | ||
getTouched: vi.fn(), | ||
getValue: vi.fn(), | ||
setTouched: vi.fn(), | ||
setValue: vi.fn(), | ||
}; | ||
|
||
vi.mocked(useTouched).mockReturnValue(touchedMock); | ||
vi.mocked(useValues).mockReturnValue(valuesMock); | ||
vi.mocked(useSubmit).mockReturnValue(submitMock); | ||
vi.mocked(useFieldHelpers).mockReturnValue(fieldHelpersMock); | ||
|
||
render(<DynamicFormV2 {...mockProps} />); | ||
|
||
// Get the actual props passed to DynamicFormContext.Provider | ||
const providerProps = vi.mocked(DynamicFormContext.Provider).mock.calls[0]?.[0]; | ||
|
||
expect(providerProps?.value).toEqual({ | ||
touched: touchedMock.touched, | ||
values: valuesMock.values, | ||
submit: submitMock.submit, | ||
fieldHelpers: fieldHelpersMock, | ||
elementsMap: mockProps.elementsMap, | ||
callbacks: { | ||
onEvent: mockProps.onEvent, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/ui/src/components/organisms/Form/DynamicForm/context/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import { IFormEventElement, TElementEvent } from '../hooks/internal/useEvents/types'; | ||
import { IFieldHelpers } from '../hooks/internal/useFieldHelpers/types'; | ||
import { ITouchedState } from '../hooks/internal/useTouched'; | ||
import { TElementsMap } from '../types'; | ||
|
||
export interface IDynamicFormCallbacks { | ||
onEvent?: (eventName: TElementEvent, element: IFormEventElement<any, any>) => void; | ||
} | ||
|
||
export interface IDynamicFormContext<TValues extends object> { | ||
values: TValues; | ||
touched: ITouchedState; | ||
elementsMap: TElementsMap; | ||
fieldHelpers: IFieldHelpers; | ||
submit: () => void; | ||
callbacks: IDynamicFormCallbacks; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.