-
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: added clear value on input hide & updates tests
- Loading branch information
1 parent
8996991
commit a8e111a
Showing
8 changed files
with
157 additions
and
15 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
1 change: 1 addition & 0 deletions
1
...rganisms/Form/DynamicForm/hooks/external/useElement/hooks/useClearValueOnUnmount/index.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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useClearValueOnUnmount'; |
17 changes: 17 additions & 0 deletions
17
...amicForm/hooks/external/useElement/hooks/useClearValueOnUnmount/useClearValueOnUnmount.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useStack } from '@/components/organisms/Form/DynamicForm/fields/FieldList/providers/StackProvider'; | ||
import { IFormElement } from '@/components/organisms/Form/DynamicForm/types'; | ||
import { useRef } from 'react'; | ||
import { useUnmount } from '../../../../internal/useUnmount'; | ||
import { useField } from '../../../useField'; | ||
|
||
export const useClearValueOnUnmount = (element: IFormElement<any, any>, hidden: boolean) => { | ||
const { stack } = useStack(); | ||
const { onChange } = useField(element, stack); | ||
const prevHidden = useRef(hidden); | ||
|
||
useUnmount(() => { | ||
if (!prevHidden.current && hidden) { | ||
onChange(undefined, true); | ||
} | ||
}); | ||
}; |
66 changes: 66 additions & 0 deletions
66
...ooks/external/useElement/hooks/useClearValueOnUnmount/useClearValueOnUnmount.unit.test.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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useStack } from '@/components/organisms/Form/DynamicForm/fields/FieldList/providers/StackProvider'; | ||
import { IFormElement } from '@/components/organisms/Form/DynamicForm/types'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { useUnmount } from '../../../../internal/useUnmount'; | ||
import { useField } from '../../../useField'; | ||
import { useClearValueOnUnmount } from './useClearValueOnUnmount'; | ||
|
||
vi.mock('@/components/organisms/Form/DynamicForm/fields/FieldList/providers/StackProvider'); | ||
vi.mock('../../../useField'); | ||
vi.mock('../../../../internal/useUnmount'); | ||
|
||
describe('useClearValueOnUnmount', () => { | ||
const mockElement = { | ||
id: 'test-element', | ||
} as IFormElement<any, any>; | ||
|
||
const mockOnChange = vi.fn(); | ||
const mockUnmountCallback = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
|
||
vi.mocked(useStack).mockReturnValue({ stack: [] }); | ||
vi.mocked(useField).mockReturnValue({ onChange: mockOnChange } as any); | ||
vi.mocked(useUnmount).mockImplementation(callback => { | ||
mockUnmountCallback.mockImplementation(callback); | ||
}); | ||
}); | ||
|
||
it('should not clear value when hidden state has not changed', () => { | ||
renderHook(() => useClearValueOnUnmount(mockElement, false)); | ||
|
||
mockUnmountCallback(); | ||
|
||
expect(mockOnChange).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not clear value when element was already hidden', () => { | ||
renderHook(() => useClearValueOnUnmount(mockElement, true)); | ||
|
||
mockUnmountCallback(); | ||
|
||
expect(mockOnChange).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should clear value when element becomes hidden', () => { | ||
const { rerender } = renderHook(({ hidden }) => useClearValueOnUnmount(mockElement, hidden), { | ||
initialProps: { hidden: false }, | ||
}); | ||
|
||
rerender({ hidden: true }); | ||
mockUnmountCallback(); | ||
|
||
expect(mockOnChange).toHaveBeenCalledWith(undefined, true); | ||
}); | ||
|
||
it('should use stack from useStack hook', () => { | ||
const mockStack = [1, 2, 3]; | ||
vi.mocked(useStack).mockReturnValue({ stack: mockStack }); | ||
|
||
renderHook(() => useClearValueOnUnmount(mockElement, false)); | ||
|
||
expect(useField).toHaveBeenCalledWith(mockElement, mockStack); | ||
}); | ||
}); |
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