Javascript 如何在以查询开始的组件中测试onCompleted、onError或onSuccess
嗨,我是测试新手,我不知道如何输入查询的未完成回调。我正在使用jest和react测试库 我需要输入onCompleted回调来检查其中的if语句 组成部分:Javascript 如何在以查询开始的组件中测试onCompleted、onError或onSuccess,javascript,reactjs,unit-testing,graphql,react-testing-library,Javascript,Reactjs,Unit Testing,Graphql,React Testing Library,嗨,我是测试新手,我不知道如何输入查询的未完成回调。我正在使用jest和react测试库 我需要输入onCompleted回调来检查其中的if语句 组成部分: ... return ( <Query query={ALL_STICKS} variables={{ name: inputValue, idProcess, isExceptional, codeClasse: data &am
...
return (
<Query
query={ALL_STICKS}
variables={{
name: inputValue,
idProcess,
isExceptional,
codeClasse: data && data.codeClasse,
codeSubject: data && data.codeSubject,
codeCompetence: data && data.codeCompetence
}}
notifyOnNetworkStatusChange
onCompleted={dataVaras => {
if (
dataVaras.varas_competentes &&
dataVaras.varas_competentes.items.length === 1
) {
onChange(dataVaras.varas_competentes.items[0]);
}
}}
>
{({ data: dataSticks, loading }) => {
return (
...
);
}}
</Query>
);
...
。。。
返回(
{
如果(
dataVaras.varas_competintes&&
dataVaras.varas_competintes.items.length==1
) {
onChange(dataVaras.varas_competintes.items[0]);
}
}}
>
{({data:dataSticks,loading})=>{
返回(
...
);
}}
);
...
当前测试套件:
imports ...
describe('SelectStick Component', () => {
afterEach(cleanup);
it('should render component', async () => {
const component = createComponent();
await wait(0);
expect(component).toBeDefined();
});
});
function createComponent(props = {}) {
const defaultProps = {
onChange: jest.fn(),
value: {},
idProcess: 'II00000020000',
...props
};
return render(
<MockedProvider mocks={[sticks]} addTypename={false}>
<SelectStick {...defaultProps} />
</MockedProvider>
);
}
导入。。。
描述('SelectStick Component',()=>{
每次之后(清理);
它('应该呈现组件',异步()=>{
const component=createComponent();
等待等待(0);
expect(component.toBeDefined();
});
});
函数createComponent(props={}){
const defaultProps={
onChange:jest.fn(),
值:{},
idProcess:'II00000020000',
…道具
};
返回渲染(
);
}
所以,我已经找到了答案。我正在测试onError回调,而传递给模拟组件的变量不完整,我需要做的就是传递我在模拟查询中定义的所有变量
这是一个非常初级的问题,但正如我所说的,我对测试Graphql查询和变异还不熟悉