-
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 phone field & tests & updated storybook
- Loading branch information
1 parent
fbc135a
commit f4a885e
Showing
6 changed files
with
165 additions
and
1 deletion.
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
31 changes: 31 additions & 0 deletions
31
packages/ui/src/components/organisms/Form/DynamicForm/fields/PhoneField/PhoneField.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 { PhoneNumberInput } from '@/components/atoms'; | ||
import { createTestId } from '@/components/organisms/Renderer'; | ||
import { useField } from '../../hooks/external'; | ||
import { FieldErrors } from '../../layouts/FieldErrors'; | ||
import { FieldLayout } from '../../layouts/FieldLayout'; | ||
import { TDynamicFormElement } from '../../types'; | ||
import { useStack } from '../FieldList/providers/StackProvider'; | ||
|
||
export interface IPhoneFieldParams { | ||
defaultCountry?: string; | ||
} | ||
|
||
export const PhoneField: TDynamicFormElement<string, IPhoneFieldParams> = ({ element }) => { | ||
const { defaultCountry = 'us' } = element.params || {}; | ||
const { stack } = useStack(); | ||
const { value, onChange, onBlur, onFocus } = useField<string | undefined>(element, stack); | ||
|
||
return ( | ||
<FieldLayout element={element}> | ||
<PhoneNumberInput | ||
country={defaultCountry} | ||
testId={createTestId(element, stack)} | ||
value={value} | ||
onChange={onChange} | ||
onBlur={onBlur} | ||
onFocus={onFocus} | ||
/> | ||
<FieldErrors element={element} /> | ||
</FieldLayout> | ||
); | ||
}; |
121 changes: 121 additions & 0 deletions
121
...s/ui/src/components/organisms/Form/DynamicForm/fields/PhoneField/PhoneField.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,121 @@ | ||
import { PhoneNumberInput } from '@/components/atoms'; | ||
import { createTestId } from '@/components/organisms/Renderer'; | ||
import '@testing-library/jest-dom'; | ||
import { render } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { useField } from '../../hooks/external'; | ||
import { FieldErrors } from '../../layouts/FieldErrors'; | ||
import { FieldLayout } from '../../layouts/FieldLayout'; | ||
import { IFormElement } from '../../types'; | ||
import { useStack } from '../FieldList/providers/StackProvider'; | ||
import { IPhoneFieldParams, PhoneField } from './PhoneField'; | ||
|
||
vi.mock('@/components/atoms', () => ({ | ||
PhoneNumberInput: vi.fn(), | ||
})); | ||
|
||
vi.mock('../../hooks/external', () => ({ | ||
useField: vi.fn(), | ||
})); | ||
|
||
vi.mock('../../layouts/FieldErrors', () => ({ | ||
FieldErrors: vi.fn(), | ||
})); | ||
|
||
vi.mock('../../layouts/FieldLayout', () => ({ | ||
FieldLayout: vi.fn(({ children }) => <div>{children}</div>), | ||
})); | ||
|
||
vi.mock('../FieldList/providers/StackProvider', () => ({ | ||
useStack: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/components/organisms/Renderer', () => ({ | ||
createTestId: vi.fn(), | ||
})); | ||
|
||
describe('PhoneField', () => { | ||
const mockElement = { | ||
id: 'test-phone', | ||
params: {}, | ||
valueDestination: 'test.path', | ||
element: 'phonefield', | ||
} as IFormElement<string, IPhoneFieldParams>; | ||
|
||
const mockFieldValues = { | ||
value: '+1234567890', | ||
onChange: vi.fn(), | ||
onBlur: vi.fn(), | ||
onFocus: vi.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
vi.mocked(useStack).mockReturnValue({ stack: [] }); | ||
vi.mocked(useField).mockReturnValue(mockFieldValues as any); | ||
vi.mocked(createTestId).mockReturnValue('test-id'); | ||
}); | ||
|
||
it('should render PhoneNumberInput with default country "us"', () => { | ||
render(<PhoneField element={mockElement} />); | ||
|
||
expect(PhoneNumberInput).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
country: 'us', | ||
testId: 'test-id', | ||
value: '+1234567890', | ||
onChange: mockFieldValues.onChange, | ||
onBlur: mockFieldValues.onBlur, | ||
onFocus: mockFieldValues.onFocus, | ||
}), | ||
expect.anything(), | ||
); | ||
}); | ||
|
||
it('should render PhoneNumberInput with custom country from params', () => { | ||
const elementWithCustomCountry = { | ||
...mockElement, | ||
params: { defaultCountry: 'il' }, | ||
}; | ||
|
||
render(<PhoneField element={elementWithCustomCountry} />); | ||
|
||
expect(PhoneNumberInput).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
country: 'il', | ||
}), | ||
expect.anything(), | ||
); | ||
}); | ||
|
||
it('should render FieldLayout with element prop', () => { | ||
render(<PhoneField element={mockElement} />); | ||
|
||
expect(FieldLayout).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
element: mockElement, | ||
}), | ||
expect.anything(), | ||
); | ||
}); | ||
|
||
it('should render FieldErrors with element prop', () => { | ||
render(<PhoneField element={mockElement} />); | ||
|
||
expect(FieldErrors).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
element: mockElement, | ||
}), | ||
expect.anything(), | ||
); | ||
}); | ||
|
||
it('should pass stack to createTestId', () => { | ||
const mockStack = [0, 1]; | ||
vi.mocked(useStack).mockReturnValue({ stack: mockStack }); | ||
|
||
render(<PhoneField element={mockElement} />); | ||
|
||
expect(createTestId).toHaveBeenCalledWith(mockElement, mockStack); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/ui/src/components/organisms/Form/DynamicForm/fields/PhoneField/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 './PhoneField'; |
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