我目前正在嘗試在我的 React 應用程序上獲得完整的測試覆蓋率,但是在嘗試測試來自 Material UI 組件的回調事件參數時,我遇到了笑話。我認為通過測試轉義事件我可以覆蓋onClose參數,但它仍然顯示為未經測試。該測試的示例:function renderWithRedux( ui: any, startingState: any = initialState, store?: any) { if (!store) { store = createStore(reducer, startingState); } return { ...render(<Provider store={store}>{ui}</Provider>), // adding `store` to the returned utilities to allow us // to reference it in our tests (just try to avoid using // this to test implementation details). store, };}test("Should close the dialog on exit event eg esc key pressed", () => { const { container, queryByTestId } = renderWithRedux( <PermissionGroupList />, permissionGroupCat ); fireEvent( queryByTestId("add-group"), new MouseEvent("click", { bubbles: true, cancelable: true, }) ); let dialogBox = queryByTestId("add-group-dialog"); // Check that the dialog is open. expect(dialogBox).toBeTruthy(); // Check that the dialog it closes. fireEvent.keyDown(document.body, { key: "Escape", keyCode: 27, which: 27 }) setTimeout(() => { // Try to re get the element. dialogBox = queryByTestId("add-group-dialog"); expect(dialogBox).toBeNull(); }, 500);})將綁定closeDialog方法傳遞給子組件時出現相同或類似的問題。它似乎未經測試。我將如何測試這個/如果它觸發方法(在子組件上),它是否會被子組件的測試覆蓋,我還沒有創建子組件測試。正如您在上面的屏幕截圖中所看到的,這兩行都未經測試,所以我如何用我的測試來覆蓋這些。我正在使用 react-testing-library 和 jest --coverage 與 redux 和 react-redux。
使用材質 UI 和反應測試庫時從對話框測試 onClose 回調?
瀟湘沐
2021-12-02 14:59:24