-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Form.useData): add data handler to get forms data outside of the…
… context
- Loading branch information
1 parent
1e37cf6
commit 915a4b6
Showing
14 changed files
with
336 additions
and
28 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
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
107 changes: 107 additions & 0 deletions
107
packages/dnb-eufemia/src/extensions/forms/Form/hooks/__tests__/useData.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,107 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import useData from '../useData' | ||
import { useEventEmitter } from '../../../../../shared/helpers/useEventEmitter' | ||
|
||
const emitter = useEventEmitter as jest.Mock | ||
|
||
jest.mock('../../../../../shared/helpers/useEventEmitter', () => { | ||
return { | ||
useEventEmitter: jest.fn(), | ||
} | ||
}) | ||
|
||
describe('Form.useData', () => { | ||
it('should return undefined by default', () => { | ||
const { result } = renderHook(() => useData('id')) | ||
expect(result.current).toEqual(undefined) | ||
}) | ||
|
||
it('should return initialData if data is not present', () => { | ||
const set = jest.fn() | ||
const update = jest.fn() | ||
|
||
emitter.mockReturnValue({ | ||
data: {}, | ||
set, | ||
update, | ||
}) | ||
|
||
const { result } = renderHook(() => useData('id', { key: 'value' })) | ||
|
||
expect(result.current).toEqual({ key: 'value' }) | ||
}) | ||
|
||
it('should return data if data is present', () => { | ||
const set = jest.fn() | ||
const update = jest.fn() | ||
|
||
emitter.mockReturnValue({ | ||
data: { key: 'existingValue' }, | ||
set, | ||
update, | ||
}) | ||
|
||
const { result } = renderHook(() => useData('id', { key: 'value' })) | ||
|
||
expect(result.current).toEqual({ key: 'existingValue' }) | ||
}) | ||
|
||
it('should call "set" with initialData on mount if data is not present', () => { | ||
const set = jest.fn() | ||
const update = jest.fn() | ||
|
||
emitter.mockReturnValue({ | ||
data: {}, | ||
set, | ||
update, | ||
}) | ||
|
||
renderHook(() => useData('id', { key: 'value' })) | ||
|
||
expect(set).toHaveBeenCalledTimes(1) | ||
expect(set).toHaveBeenLastCalledWith({ key: 'value' }) | ||
expect(update).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should call "update" with initialData rerender', () => { | ||
const set = jest.fn() | ||
const update = jest.fn() | ||
|
||
emitter.mockReturnValue({ | ||
data: {}, | ||
set, | ||
update, | ||
}) | ||
|
||
const { rerender } = renderHook((props = { key: 'value' }) => | ||
useData('id', props) | ||
) | ||
|
||
expect(set).toHaveBeenCalledTimes(1) | ||
expect(set).toHaveBeenLastCalledWith({ key: 'value' }) | ||
expect(update).not.toHaveBeenCalled() | ||
|
||
rerender({ key: 'changed' }) | ||
|
||
expect(set).toHaveBeenCalledTimes(2) | ||
expect(set).toHaveBeenLastCalledWith({ key: 'changed' }) | ||
expect(update).toHaveBeenCalledTimes(1) | ||
expect(update).toHaveBeenLastCalledWith({ key: 'changed' }) | ||
}) | ||
|
||
it('should not call "update" or "set" with initialData on mount if data is present', () => { | ||
const set = jest.fn() | ||
const update = jest.fn() | ||
|
||
emitter.mockReturnValue({ | ||
data: { key: 'existingValue' }, | ||
set, | ||
update, | ||
}) | ||
|
||
renderHook(() => useData('id', { key: 'value' })) | ||
|
||
expect(set).not.toHaveBeenCalled() | ||
expect(update).not.toHaveBeenCalled() | ||
}) | ||
}) |
31 changes: 31 additions & 0 deletions
31
packages/dnb-eufemia/src/extensions/forms/Form/hooks/useData.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,31 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { useEventEmitter } from '../../../../shared/component-helper' | ||
|
||
export default function useData<Data>( | ||
id: string, | ||
data: Data = undefined | ||
): Data { | ||
const { | ||
set, | ||
update, | ||
data: emitterData, | ||
} = useEventEmitter<Data>(id) || {} | ||
const dataRef = useRef(data) | ||
|
||
useEffect(() => { | ||
if (data && data !== dataRef.current) { | ||
dataRef.current = data | ||
update?.(data) | ||
} | ||
}, [data, update]) | ||
|
||
if (data) { | ||
const hasExistingData = Object.keys(emitterData || {}).length > 0 | ||
if (!hasExistingData) { | ||
set?.(data) | ||
return data | ||
} | ||
} | ||
|
||
return emitterData | ||
} |
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.