-
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
christian97dd
committed
Nov 27, 2024
1 parent
cc3a75c
commit 8d712c1
Showing
6 changed files
with
124 additions
and
0 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,44 @@ | ||
import React from 'react'; | ||
import {render, fireEvent} from '@testing-library/react-native'; | ||
import BaseInput from './index'; | ||
|
||
describe('BaseInput Component', () => { | ||
it('renders correctly with placeholder and value', () => { | ||
const {getByPlaceholderText} = render( | ||
<BaseInput placeholder="Enter text" value="Test value" /> | ||
); | ||
|
||
const input = getByPlaceholderText('Enter text'); | ||
expect(input.props.value).toBe('Test value'); | ||
}); | ||
|
||
it('calls onChangeText when text changes', () => { | ||
const mockOnChangeText = jest.fn(); | ||
const {getByPlaceholderText} = render( | ||
<BaseInput placeholder="Enter text" value="" onChangeText={mockOnChangeText} /> | ||
); | ||
|
||
const input = getByPlaceholderText('Enter text'); | ||
fireEvent.changeText(input, 'New value'); | ||
expect(mockOnChangeText).toHaveBeenCalledWith('New value'); | ||
}); | ||
|
||
it('applies custom styles correctly', () => { | ||
const customStyle = {borderColor: 'red'}; | ||
const {getByPlaceholderText} = render( | ||
<BaseInput placeholder="Enter text" value="" style={customStyle} /> | ||
); | ||
|
||
const input = getByPlaceholderText('Enter text'); | ||
expect(input.props.style).toEqual( | ||
expect.arrayContaining([expect.objectContaining(customStyle)]) | ||
); | ||
}); | ||
|
||
it('defaults textAlign to center', () => { | ||
const {getByPlaceholderText} = render(<BaseInput placeholder="Enter text" value="" />); | ||
|
||
const input = getByPlaceholderText('Enter text'); | ||
expect(input.props.textAlign).toBe('center'); | ||
}); | ||
}); |
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,44 @@ | ||
import React, {forwardRef} from 'react'; | ||
import {StyleSheet, TextInput, TextInputProps} from 'react-native'; | ||
import {palette} from 'theme/palette'; | ||
import {moderateScale, scaledForDevice} from 'scale'; | ||
|
||
interface BaseInputProps extends TextInputProps { | ||
placeholder?: string; | ||
onChangeText?: (text: string) => void; | ||
style?: any; | ||
textAlign?: 'center' | 'left' | 'right' | undefined; | ||
} | ||
|
||
const BaseInput = forwardRef<TextInput, BaseInputProps>( | ||
({value, placeholder, onChangeText, style, textAlign, ...props}, ref) => { | ||
const styles = StyleSheet.create({ | ||
input: { | ||
padding: 0, | ||
height: scaledForDevice(70, moderateScale), | ||
borderColor: palette.primary.main, | ||
borderWidth: 2, | ||
borderRadius: 8, | ||
fontSize: scaledForDevice(42, moderateScale), | ||
backgroundColor: palette.white.light, | ||
color: palette.black.main, | ||
}, | ||
}); | ||
|
||
return ( | ||
<TextInput | ||
style={[styles.input, style]} | ||
ref={ref} | ||
value={value} | ||
placeholder={placeholder} | ||
textAlign={textAlign || 'center'} | ||
onChangeText={onChangeText} | ||
selectionColor={palette.primary.main} | ||
placeholderTextColor={palette.grey[500]} | ||
{...props} | ||
/> | ||
); | ||
} | ||
); | ||
|
||
export default BaseInput; |
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,25 @@ | ||
import React, {useState} from 'react'; | ||
import BaseInput from 'atoms/BaseInput'; | ||
|
||
export default { | ||
title: 'Components/BaseInput', | ||
argTypes: { | ||
textAlign: { | ||
options: ['center', 'left', 'right', null], | ||
defaultValue: 'center', | ||
control: {type: 'select'}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const DefaultProps = (props) => { | ||
const [value, setValue] = useState(''); | ||
|
||
return <BaseInput value={value} onChangeText={setValue} {...props} />; | ||
}; | ||
|
||
DefaultProps.storyName = 'Base input with default props'; | ||
|
||
DefaultProps.args = { | ||
placeholder: 'Janis Commerce', | ||
}; |