Skip to content

Commit 230d74a

Browse files
ElenaBitireyjose
authored andcommitted
refactor: refactor tests for Select
1 parent 491ac1a commit 230d74a

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

src/ui/select.test.tsx

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ describe('Select component ', () => {
2121
label="Select options"
2222
options={options}
2323
onSelect={onSelect}
24-
testID="test-select"
24+
testID="select"
2525
/>
2626
);
27-
expect(screen.getByTestId('test-select-trigger')).toBeOnTheScreen();
28-
expect(screen.getByTestId('test-select-label')).toBeOnTheScreen();
27+
expect(screen.getByTestId('select-trigger')).toBeOnTheScreen();
28+
expect(screen.getByTestId('select-label')).toBeOnTheScreen();
2929
});
3030

3131
it('should render the label correctly ', () => {
@@ -35,14 +35,12 @@ describe('Select component ', () => {
3535
label="Select"
3636
options={options}
3737
onSelect={onSelect}
38-
testID="test-select"
38+
testID="select"
3939
/>
4040
);
41-
expect(screen.getByTestId('test-select-trigger')).toBeOnTheScreen();
42-
expect(screen.getByTestId('test-select-label')).toBeOnTheScreen();
43-
expect(screen.getByTestId('test-select-label')?.props.children).toBe(
44-
'Select'
45-
);
41+
expect(screen.getByTestId('select-trigger')).toBeOnTheScreen();
42+
expect(screen.getByTestId('select-label')).toBeOnTheScreen();
43+
expect(screen.getByTestId('select-label')?.props.children).toBe('Select');
4644
});
4745

4846
it('should render the error correctly ', () => {
@@ -52,13 +50,13 @@ describe('Select component ', () => {
5250
label="Select"
5351
options={options}
5452
onSelect={onSelect}
55-
testID="test-select"
53+
testID="select"
5654
error="Please select an option"
5755
/>
5856
);
59-
expect(screen.getByTestId('test-select-trigger')).toBeOnTheScreen();
60-
expect(screen.getByTestId('test-select-error')).toBeOnTheScreen();
61-
expect(screen.getByTestId('test-select-error')?.props.children).toBe(
57+
expect(screen.getByTestId('select-trigger')).toBeOnTheScreen();
58+
expect(screen.getByTestId('select-error')).toBeOnTheScreen();
59+
expect(screen.getByTestId('select-error')?.props.children).toBe(
6260
'Please select an option'
6361
);
6462
});
@@ -68,30 +66,28 @@ describe('Select component ', () => {
6866
<Select
6967
label="Select"
7068
options={options}
71-
testID="test-select"
69+
testID="select"
7270
placeholder="Select an option"
7371
/>
7472
);
7573

76-
const selectTrigger = screen.getByTestId('test-select-trigger');
74+
const selectTrigger = screen.getByTestId('select-trigger');
7775
fireEvent.press(selectTrigger);
7876

79-
expect(screen.getByTestId('test-select-item-chocolate')).toBeOnTheScreen();
80-
expect(screen.getByTestId('test-select-item-strawberry')).toBeOnTheScreen();
81-
expect(screen.getByTestId('test-select-item-vanilla')).toBeOnTheScreen();
77+
expect(screen.getByTestId('select-item-chocolate')).toBeOnTheScreen();
78+
expect(screen.getByTestId('select-item-strawberry')).toBeOnTheScreen();
79+
expect(screen.getByTestId('select-item-vanilla')).toBeOnTheScreen();
8280
});
8381

8482
it('should call onSelect on selecting an option', () => {
8583
const onSelect = jest.fn();
8684

87-
render(
88-
<Select options={options} onSelect={onSelect} testID="test-select" />
89-
);
85+
render(<Select options={options} onSelect={onSelect} testID="select" />);
9086

91-
const optionModal = screen.getByTestId('test-select-modal');
87+
const optionModal = screen.getByTestId('select-modal');
9288
fireEvent(optionModal, 'onPresent');
9389

94-
const optionItem1 = screen.getByTestId('test-select-item-chocolate');
90+
const optionItem1 = screen.getByTestId('select-item-chocolate');
9591
fireEvent.press(optionItem1);
9692

9793
expect(onSelect).toHaveBeenCalledWith(options[0].value);

src/ui/select.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const Options = React.forwardRef<BottomSheetModal, OptionsProps>(
7979
label={item.label}
8080
selected={value === item.value}
8181
onPress={() => onSelect(item)}
82-
testID={`${testID}-item-${item.value}`}
82+
testID={testID ? `${testID}-item-${item.value}` : undefined}
8383
/>
8484
),
8585
[onSelect, value, testID]
@@ -98,7 +98,7 @@ export const Options = React.forwardRef<BottomSheetModal, OptionsProps>(
9898
data={options}
9999
keyExtractor={keyExtractor}
100100
renderItem={renderSelectItem}
101-
testID={`${testID}-modal`}
101+
testID={testID ? `${testID}-modal` : undefined}
102102
/>
103103
</Modal>
104104
);
@@ -149,7 +149,7 @@ export const Select = (props: SelectProps) => {
149149
placeholder = 'select...',
150150
disabled = false,
151151
onSelect,
152-
testID = 'select',
152+
testID,
153153
} = props;
154154
const modal = useModal();
155155

@@ -182,15 +182,18 @@ export const Select = (props: SelectProps) => {
182182
<>
183183
<View className={styles.container()}>
184184
{label && (
185-
<Text testID={`${testID}-label`} className={styles.label()}>
185+
<Text
186+
testID={testID ? `${testID}-label` : undefined}
187+
className={styles.label()}
188+
>
186189
{label}
187190
</Text>
188191
)}
189192
<TouchableOpacity
190193
className={styles.input()}
191194
disabled={disabled}
192195
onPress={modal.present}
193-
testID={`${testID}-trigger`}
196+
testID={testID ? `${testID}-trigger` : undefined}
194197
>
195198
<View className="flex-1">
196199
<Text className={styles.inputValue()}>{textValue}</Text>

0 commit comments

Comments
 (0)