致力于為大型組件撰寫單元測驗。我之前為一個較小的組件撰寫了一個,但是其中嵌套了其他幾個自定義組件。我正在嘗試選擇它,這樣我就可以弄清楚如何處理它,但我現在什至無法達到這一點。
PriceOverride.tsx
這是組件渲染的一部分。PriceOverrideDateRange 是另一個自定義組件,我正在嘗試使用 testID 來定位它 <Counter
containerMargin={{ marginTop: -5 }}
defaultCounter={itemCount}
labelText={t('priceMultiple')}
onSelectCounter={itemQuantity}
min={1}
/>
<PriceOverrideDateRange testID='dateRange' startDate={startDate} endDate={endDate} onSelectDateRange={onChangeDateInterval} />
<Counter
containerMargin={{ marginTop: -5 }}
defaultCounter={labelCount}
labelText={t('common:labelQuantity')}
onSelectCounter={labelQuantity}
/>
PriceOverride.test.tsx
這是我正在做的測驗的一部分 it('should be able to change the date interval', () =>{
const navContextValue: any = {
isFocused: () => false,
addListener: jest.fn(() => jest.fn())
};
//Mock out dependent function with jest
//Nothing here right now...
//Render with the props you want
const { getByTestId } = render(
<Provider store={store}>
<NavigationContext.Provider value={navContextValue}>
<PriceOverride navigation={props.navigation}/>
</NavigationContext.Provider>
</Provider>, );
//Locate screen components for test
const dateRange = getByTestId('dateRange');
//Perform user actions
fireEvent.changeText(dateRange, "01/22/2022");
//Measure against expect cases
expect(dateRange).toBe("1/22/2022");
});
這是我收到的錯誤訊息:
● Price Details Test ? should be able to change the date interval
Unable to find an element with testID: dateRange
143 |
144 | //Locate screen components for test
> 145 | const dateRange = getByTestId('dateRange');
| ^
146 |
147 |
uj5u.com熱心網友回復:
您需要將 testID 作為道具傳遞給組件,然后將其添加到您正在使用的視圖中,我將添加一個簡單的示例。
const App = () => {
return <PriceOverrideDateRange testID="dateRange" />
}
const PriceOverrideDateRange = ({testID}) => {
return <View testID="dateRange" ><Text>Hello</Text></View>
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418412.html
標籤:
上一篇:授予PermissionsAndroid.PERMISSIONS.CAMERA后仍然看不到照片
下一篇:圖示未顯示在本機底部選項卡中
