簡體   English   中英

我如何在以查詢開頭的組件中測試 onCompleted、onError 或 onSuccess

[英]How can i test onCompleted, onError or onSuccess in a component starting with a Query

嗨,我是測試新手,我不知道如何輸入查詢的 onCompleted 回調。 我在 react-testing-library 中使用 jest。

我需要輸入 onCompleted 回調來檢查其中的 if 語句。

組件:

...

  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>
  );

...

當前測試套件:

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>
  );
}

所以,我想通了。 我正在測試 onError 回調,我傳遞給模擬組件的變量是不完整的,我需要做的就是傳遞我在模擬查詢中定義的所有變量。

這是一個非常初學者的問題,但正如我所說,我是測試 Graphql 查詢和突變的新手。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM