From 3e4a1d0f8836bedba69f946fb0cbc83bac9ab893 Mon Sep 17 00:00:00 2001 From: Douglas Nassif Roma Junior Date: Fri, 26 Aug 2022 09:48:59 -0300 Subject: [PATCH 1/3] add tests to should not fire on non-editable TextInput getting by testID --- src/__tests__/fireEvent.test.tsx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/__tests__/fireEvent.test.tsx b/src/__tests__/fireEvent.test.tsx index a8a7dca84..5ae48d9da 100644 --- a/src/__tests__/fireEvent.test.tsx +++ b/src/__tests__/fireEvent.test.tsx @@ -235,6 +235,25 @@ test('should not fire on non-editable TextInput', () => { expect(onChangeTextMock).not.toHaveBeenCalled(); }); +test('should not fire on non-editable TextInput getting by testID', () => { + const testID = 'my-text-input'; + const onChangeTextMock = jest.fn(); + const NEW_TEXT = 'New text'; + + const { getByTestId } = render( + + + + ); + + fireEvent.changeText(getByTestId(testID), NEW_TEXT); + expect(onChangeTextMock).not.toHaveBeenCalled(); +}); + test('should not fire on non-editable TextInput with nested Text', () => { const placeholder = 'Test placeholder'; const onChangeTextMock = jest.fn(); From 4aedb1055fd963fdf09cbaf8b6bc5000f7a2a11f Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Fri, 9 Sep 2022 09:44:13 +0200 Subject: [PATCH 2/3] fix: detect both composite and host TextInput in fireEvent --- src/fireEvent.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/fireEvent.ts b/src/fireEvent.ts index 0f95381e4..497531b45 100644 --- a/src/fireEvent.ts +++ b/src/fireEvent.ts @@ -7,9 +7,16 @@ const isHostElement = (element?: ReactTestInstance) => { return typeof element?.type === 'string'; }; +// Rendering TextInput results in having composite `TextInput` component with +// child text input host component. Both will have the same `editable` and +// `onChangeText` props. const isTextInput = (element?: ReactTestInstance) => { const { TextInput } = require('react-native'); - return element?.type === TextInput; + + const isExportedCompositeTextInput = element?.type === TextInput; + const isChildHostTextInput = element?.parent?.type === TextInput; + + return isExportedCompositeTextInput || isChildHostTextInput; }; const isTouchResponder = (element?: ReactTestInstance) => { From 5f844e444ddb8087358bded57e14143d68e8ff63 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Fri, 9 Sep 2022 09:57:23 +0200 Subject: [PATCH 3/3] refactor: clean-up test code --- src/__tests__/fireEvent.test.tsx | 46 +++++++++++--------------------- 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/src/__tests__/fireEvent.test.tsx b/src/__tests__/fireEvent.test.tsx index 5ae48d9da..3de94a59a 100644 --- a/src/__tests__/fireEvent.test.tsx +++ b/src/__tests__/fireEvent.test.tsx @@ -216,42 +216,28 @@ test('should not fire on disabled Pressable', () => { expect(handlePress).not.toHaveBeenCalled(); }); -test('should not fire on non-editable TextInput', () => { - const placeholder = 'Test placeholder'; - const onChangeTextMock = jest.fn(); - const NEW_TEXT = 'New text'; - - const { getByPlaceholderText } = render( - - - +test('should not fire on non-editable composite TextInput', () => { + const onChangeText = jest.fn(); + const view = render( + ); - fireEvent.changeText(getByPlaceholderText(placeholder), NEW_TEXT); - expect(onChangeTextMock).not.toHaveBeenCalled(); + fireEvent.changeText(view.getByPlaceholderText('subject'), 'new text'); + expect(onChangeText).not.toHaveBeenCalled(); }); -test('should not fire on non-editable TextInput getting by testID', () => { - const testID = 'my-text-input'; - const onChangeTextMock = jest.fn(); - const NEW_TEXT = 'New text'; - - const { getByTestId } = render( - - - +test('should not fire on non-editable host TextInput', () => { + const onChangeText = jest.fn(); + const view = render( + ); - fireEvent.changeText(getByTestId(testID), NEW_TEXT); - expect(onChangeTextMock).not.toHaveBeenCalled(); + fireEvent.changeText(view.getByTestId('subject'), 'new text'); + expect(onChangeText).not.toHaveBeenCalled(); }); test('should not fire on non-editable TextInput with nested Text', () => {