-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
198 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import FormState from '../FormState'; | ||
|
||
import useArrayFieldLength from './useArrayFieldLength'; | ||
import useFormSelect from './useFormSelect'; | ||
|
||
jest.mock('./useFormSelect'); | ||
|
||
describe('useArrayFieldLength', () => { | ||
it('calls useFormSelect with the correct selector function', () => { | ||
const mockUseFormSelect = useFormSelect as jest.MockedFunction<typeof useFormSelect>; | ||
const mockName = 'test'; | ||
const mockState = { values: { [mockName]: ['item1', 'item2', 'item3'] } } as FormState; | ||
|
||
useArrayFieldLength(mockName); | ||
|
||
expect(mockUseFormSelect).toHaveBeenCalled(); | ||
const selector = mockUseFormSelect.mock.calls[0][0]; | ||
expect(selector(mockState)).toBe(3); | ||
}); | ||
|
||
it('returns 0 if the field is not an array', () => { | ||
const mockUseFormSelect = useFormSelect as jest.MockedFunction<typeof useFormSelect>; | ||
const mockName = 'test'; | ||
const mockState = { values: { [mockName]: 'not an array' } } as FormState; | ||
|
||
useArrayFieldLength(mockName); | ||
|
||
expect(mockUseFormSelect).toHaveBeenCalled(); | ||
const selector = mockUseFormSelect.mock.calls[0][0]; | ||
expect(selector(mockState)).toBe(0); | ||
}); | ||
}); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { useContext } from 'react'; | ||
|
||
import type { Data, FormState } from '../context'; | ||
import { FormStateContext } from '../context'; | ||
import useFormSelect from '../hooks/useFormSelect'; | ||
|
||
export default function useFormState<D extends Data>(): FormState<D> { | ||
return useContext(FormStateContext) as FormState<D>; | ||
export default function useFormState<D extends Data = Data>() { | ||
return useFormSelect<D>((s) => s as FormState<D>); | ||
} |
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,29 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
|
||
import useFormDispatch from './useFormDispatch'; | ||
import useFormSelect from './useFormSelect'; | ||
import useFormSubmit from './useFormSubmit'; | ||
|
||
jest.mock('./useFormDispatch'); | ||
jest.mock('./useFormSelect'); | ||
|
||
describe('useFormSubmit', () => { | ||
it('returns a tuple with submitting status, validating status, and dispatch submit function', () => { | ||
const mockDispatch = jest.fn(); | ||
(useFormDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
(useFormSelect as jest.Mock).mockImplementation((selector) => | ||
selector({ isSubmitting: true, isValidating: false }), | ||
); | ||
|
||
const { result } = renderHook(() => useFormSubmit()); | ||
|
||
expect(result.current[0]).toBe(true); | ||
expect(result.current[1]).toBe(false); | ||
|
||
act(() => { | ||
result.current[2](); | ||
}); | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ type: 'startSubmit' }); | ||
}); | ||
}); |
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,25 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import useFormDispatch from './useFormDispatch'; | ||
import useSetFieldDisabled from './useSetFieldDisabled'; | ||
|
||
jest.mock('./useFormDispatch'); | ||
|
||
describe('useSetFieldDisabled', () => { | ||
it('returns a function that dispatches a setDisabled action', () => { | ||
const mockDispatch = jest.fn(); | ||
(useFormDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
|
||
const { result } = renderHook(() => useSetFieldDisabled('test')); | ||
|
||
act(() => { | ||
result.current(true); | ||
}); | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
name: 'test', | ||
type: 'setDisabled', | ||
value: true, | ||
}); | ||
}); | ||
}); |
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,42 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import useFormDispatch from './useFormDispatch'; | ||
import useSetFieldError from './useSetFieldError'; | ||
|
||
jest.mock('./useFormDispatch'); | ||
|
||
describe('useSetFieldError', () => { | ||
it('returns a function that dispatches a setError action', async () => { | ||
const mockDispatch = jest.fn(); | ||
(useFormDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
|
||
const { result } = renderHook(() => useSetFieldError('test')); | ||
|
||
await act(async () => { | ||
await result.current('error message'); | ||
}); | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
error: 'error message', | ||
name: 'test', | ||
type: 'setError', | ||
}); | ||
}); | ||
|
||
it('handles promise errors', async () => { | ||
const mockDispatch = jest.fn(); | ||
(useFormDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
|
||
const { result } = renderHook(() => useSetFieldError('test')); | ||
|
||
await act(async () => { | ||
await result.current(Promise.resolve('promise error message')); | ||
}); | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
error: 'promise error message', | ||
name: 'test', | ||
type: 'setError', | ||
}); | ||
}); | ||
}); |
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,21 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import useFormDispatch from './useFormDispatch'; | ||
import useSetValues from './useSetValues'; | ||
|
||
jest.mock('./useFormDispatch'); | ||
|
||
describe('useSetValues', () => { | ||
it('returns a function that dispatches a setValues action', () => { | ||
const mockDispatch = jest.fn(); | ||
(useFormDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
|
||
const { result } = renderHook(() => useSetValues()); | ||
|
||
act(() => { | ||
result.current({ field: 'value' }); | ||
}); | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ type: 'setValues', values: { field: 'value' } }); | ||
}); | ||
}); |