-
Notifications
You must be signed in to change notification settings - Fork 192
How do you test this component? #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@myou11 did you find a way to test it? I got the same issue. |
@myou11 I found how to test that component. The main issue is related to measureInWindow method used to put in right position the modal and an issue with react-native-testing-library: #125 According to that, take a look on mine test code /*
* This is my reduced component that's wrapping the Dropdown component
* used to allow enduser to choose from tree different chart types
*/
export const ChartSelector = (props) => {
const { view, onChange } = props
return <Dropdown
testID="chart-selector"
itemTestIDField="itemTestIDField"
data={[
{ itemTestIDField: 'testID-1', label: 'label-value-1', value: "value-1" },
{ itemTestIDField: 'testID-2', label: 'label-value-2', value: "value-2" },
{ itemTestIDField: 'testID-3', label: 'label-value-3', value: "value-3" },
]}
maxHeight={maxHeight}
labelField="label"
valueField="value"
value={view}
onChange={onChange}
/>
} import React from 'react'
import { fireEvent, render, screen, waitFor } from "@testing-library/react-native"
import { ChartSelector } from "./ChartSelector"
import { Wrapper } from "../../../__tests__/components/wrapper"
// According to the issue on @testing-library/react-native
import { View } from 'react-native'
jest.spyOn(View.prototype, 'measureInWindow').mockImplementation((cb) => {
cb(18, 113, 357, 50)
});
describe('ChartSelector component', () => {
it('should invoke onChange view', async () => {
const spy = jest.fn()
const { getByText, getByTestId } = render(<ChartSelector
view="value-1"
onChange={spy}
/>, { wrapper: Wrapper(getState()) })
fireEvent.press(getByTestId("chart-selector"))
const choosedChart = getByText('label-value-1')
await waitFor(() => expect(choosedChart).toBeDefined())
fireEvent.press(choosedChart)
await waitFor(() => expect(spy).toBeCalledWith(expect.objectContaining({
label: 'label-value-1',
value: 'value-1'
})))
})
}) I hope this helps! |
Hi @carlolucadei, I was finally able to get around to testing this out today and it works perfectly for me! Thanks so much for figuring this out and sharing the code sample. |
dropdown selection turns out to be a massive pain to test, so I didn't (hoaphantn7604/react-native-element-dropdown#175)
dropdown selection turns out to be a massive pain to test, so I didn't (hoaphantn7604/react-native-element-dropdown#175)
I am trying to test this component with the latest Jest v29, react-native-testing-library v12, but the dropdown does not seem to open upon pressing it in the test. How do you initiate opening the dropdown in a test?
I have provided my test file below
The text was updated successfully, but these errors were encountered: