Skip to content

Commit 6ee740d

Browse files
committed
add test cover for dynamic index types
Signed-off-by: Winner95 <Winner95@users.noreply.github.com>
1 parent 28f80b3 commit 6ee740d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

__tests__/handler.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ describe('typescript-react-function-component-props-handler', () => {
154154
expect(doc.props).not.toHaveProperty('placeholder');
155155
});
156156

157+
// @TODO add support for index signature in type
158+
test('handles React.FC<Props> components with dynamic type props', () => {
159+
const doc = parseFixture('TextInput.tsx');
160+
161+
expect(doc).toHaveProperty('props');
162+
expect(doc.props).toHaveProperty('onChange');
163+
expect(doc.props.onChange).toHaveProperty('tsType');
164+
expect(doc.props.onChange.tsType.raw).toBe('(event: React.ChangeEvent<HTMLInputElement>) => void');
165+
expect(doc.props.onChange.tsType.signature).toHaveProperty('arguments');
166+
expect(doc.props.onChange.tsType.signature.arguments).toHaveLength(1);
167+
expect(doc.props.onChange.tsType.signature.arguments[0]).toMatchObject({ name: 'event' } );
168+
expect(doc.props.onChange.tsType.signature.arguments[0].type).toHaveProperty('elements')
169+
// data-testid comes from DataAttributes index signature
170+
// which is not yet supported
171+
expect(doc.props).not.toHaveProperty('data-testid');
172+
});
173+
157174
// Line 31 in index.js without type - can't be tested directly because of early return
158175
test('handles components without type', () => {
159176
const doc = parseFixture('ComponentWithoutType.tsx');

fixtures/TextInput.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
3+
type DataAttributes = {
4+
[key: `data-${string}`]: string | number | undefined;
5+
};
6+
7+
type TextInputProps = {
8+
id?: string;
9+
name?: string;
10+
value: string;
11+
label?: string;
12+
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
13+
onBlur?: (event: React.FocusEvent<HTMLInputElement>) => void;
14+
onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
15+
isDisabled?: boolean;
16+
isReadOnly?: boolean;
17+
placeholder?: string;
18+
} & DataAttributes &
19+
Omit<React.InputHTMLAttributes<HTMLInputElement>, 'value' | 'onChange'>;
20+
21+
export const TextInput: React.FC<TextInputProps> = ({ id, label, 'data-testid': dataTestid }) => {
22+
return <span id={id} data-testid={dataTestid}>{label}</span>;
23+
};

0 commit comments

Comments
 (0)