具有以下代碼:
import { useForm, Controller } from 'react-hook-form';
...
const { handleSubmit, reset, control, register, watch } = useForm({
resolver: yupResolver(schema)
});
const availableSensorPoolOptions = [
{ id: '0', name: 'sensor pool 0' },
{ id: '1', name: 'sensor pool 1' },
{ id: '2', name: 'sensor pool 2' }
];
...
onSubmit={handleSubmit(onAddSubmit)} // the action when the submit is called
...
const onAddSubmit = (data) => {
postSignalMapping(data); // the API call if all is good
toggle();
reset();
};
...
<div data-testid={MapSignalModalTestIds.AVAILABLE_SENSOR_POOL}>
<Controller
control={control}
name='availableSensorPool'
render={({ field: { onChange } }) =>
<SelectInput
label={t('calculation-engine.available-sensor-pool')}
initialSelectedOption={{ id: '0', name: '' }}
onChange={onChange}
options={availableSensorPoolOptions}
/>
}
/>
</div>
像 thi 這樣的 s有多個SelectInput,但在這個例子中它只有一個
const schema = yup.object().shape({
availableSensorPool: yup.object().shape({
id: yup.string(),
name: yup.string()
})
});
const { handleSubmit, reset, control, register, watch } = useForm({
resolver: yupResolver(schema)
});
這是測驗:
import { fireEvent, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderWithClientInstance } from '@oam/shared/test-utils';
import '@testing-library/jest-dom';
import MapSignalModal from './map-signal-modal';
describe('MapSignalModal', () => {
const title = 'title';
const toggle = jest.fn();
const postSignalMapping = jest.fn();
it('should call postSignalMapping function on clicking in Save button successfully', async () => {
const { getByTestId, getByLabelText } = renderWithClientInstance(
<MapSignalModal title={title} open={true} toggle={toggle} />
);
const saveButton = getByTestId('submit-button');
expect(saveButton).toBeInTheDocument();
userEvent.selectOptions(
getByLabelText('calculation-engine.available-sensor-pool'),
'sensor pool 0'
);
fireEvent.click(saveButton);
await waitFor(() => {
expect(postSignalMapping).toBeCalled();
});
});
});
它失敗并出現錯誤:
TestingLibraryElementError:在選項中找不到值“傳感器池 0”
uj5u.com熱心網友回復:
因此,由于select行為是使用 abutton和spans 實作的。
您需要首先單擊按鈕,這將在螢屏上顯示所有選項,然后您需要單擊其中一個選項。
然后您最終可以測驗所選選項現在是否在螢屏上。
it("test dropdpwn", async () => {
const { getByTestId, getByLabelText } = renderWithClientInstance(
<MapSignalModal title={title} open={true} toggle={toggle} />
);
userEvent.click(screen.getAllByTestId("selectButton")[0]);
userEvent.click(screen.getByText("sensor pool 1"));
expect(
await screen.findByText(screen.getByText("sensor pool 1"))
).toBeInTheDocument();
});
此外,為了確保您可以嘗試以下操作,這應該會失敗,因為“傳感器池 1”選項最初不在螢屏上。
它應該在文本更改為“傳感器池 0”時通過,因為它最初就在螢屏上。
it("test dropdpwn", async () => {
const { getByTestId, getByLabelText } = renderWithClientInstance(
<MapSignalModal title={title} open={true} toggle={toggle} />
);
expect(screen.getByText("sensor pool 1")).toBeInTheDocument();
// if you replace the above text to "sensor pool 0", it should work
});
為了測驗是否postSignalMapping被呼叫,您可以模擬它,如下所示:
let mockPostSignalMapping = jest.fn();
jest.mock("../lib/hooks/use-post-signal-mapping", () => ({
mutate: mockPostSignalMapping,
}));
it("test dropdpwn", async () => {
// Do stuff
await waitFor(() => {
expect(mockPostSignalMapping).toBeCalled();
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411667.html
標籤:
