React 测试无法找到用笑话模拟的元素
React test unable to find an element mocked with jest
我正在尝试模拟一个 React select 组件,以便在测试触发事件中使用它。下面是我使用 jest 创建的反应 select。
jest.mock("react-select", () => ({ options, onChange }) => {
function handleChange(event) {
const option = [];
option.push(options.find(option => option === event.currentTarget.value));
onChange(option);
console.log(option);............................................................//line 1
}
return (
<select
data-testid="test-select"
name="users"
label="users"
options={[
"a",
"b",
"c",
"d",
"e"
]}
onChange={handleChange}
>
{options.map(value => (
<option key={value} value={value}>
{value}
</option>
))}
</select>
);
});
问题是 line 1
上的控制台在控制台中打印 undefined。下面是我测试的火灾事件模型:
const chooseInput = getByTestId("test-select");
fireEvent.change(chooseInput, {
target: {
value: "b"
}
});
测试失败为:
expect(received).toBeInTheDocument()
received value must be an HTMLElement or an SVGElement.
Received has value: null
为什么在火灾事件开始时选项没有更新?
模拟库默认导出的结构是:
jest.mock("react-select", () => {
return {
__esModule: true,
default: ({ options, onChange }) => {
// Your mock implementation goes here
},
};
})
__esModule: true
很重要
主函数需要 return 一个具有 default
属性 的对象来代表您的模拟实现
所以完整的代码应该是
jest.mock("react-select", () => {
return {
__esModule: true,
default: ({
options,
onChange
}) => {
function handleChange(event) {
const option = [];
option.push(options.find(option => option === event.currentTarget.value));
onChange(option);
console.log(option);
}
return ( <
select data - testid = "test-select"
name = "users"
label = "users"
options = {
[
"a",
"b",
"c",
"d",
"e"
]
}
onChange = {
handleChange
} > {
options.map(value => ( <
option key = {
value
}
value = {
value
} > {
value
} <
/option>
))
} <
/select>
);
},
};
})
我正在尝试模拟一个 React select 组件,以便在测试触发事件中使用它。下面是我使用 jest 创建的反应 select。
jest.mock("react-select", () => ({ options, onChange }) => {
function handleChange(event) {
const option = [];
option.push(options.find(option => option === event.currentTarget.value));
onChange(option);
console.log(option);............................................................//line 1
}
return (
<select
data-testid="test-select"
name="users"
label="users"
options={[
"a",
"b",
"c",
"d",
"e"
]}
onChange={handleChange}
>
{options.map(value => (
<option key={value} value={value}>
{value}
</option>
))}
</select>
);
});
问题是 line 1
上的控制台在控制台中打印 undefined。下面是我测试的火灾事件模型:
const chooseInput = getByTestId("test-select");
fireEvent.change(chooseInput, {
target: {
value: "b"
}
});
测试失败为:
expect(received).toBeInTheDocument()
received value must be an HTMLElement or an SVGElement.
Received has value: null
为什么在火灾事件开始时选项没有更新?
模拟库默认导出的结构是:
jest.mock("react-select", () => {
return {
__esModule: true,
default: ({ options, onChange }) => {
// Your mock implementation goes here
},
};
})
__esModule: true
很重要
主函数需要 return 一个具有 default
属性 的对象来代表您的模拟实现
所以完整的代码应该是
jest.mock("react-select", () => {
return {
__esModule: true,
default: ({
options,
onChange
}) => {
function handleChange(event) {
const option = [];
option.push(options.find(option => option === event.currentTarget.value));
onChange(option);
console.log(option);
}
return ( <
select data - testid = "test-select"
name = "users"
label = "users"
options = {
[
"a",
"b",
"c",
"d",
"e"
]
}
onChange = {
handleChange
} > {
options.map(value => ( <
option key = {
value
}
value = {
value
} > {
value
} <
/option>
))
} <
/select>
);
},
};
})