score:1
You are mocking ./SearchBox
module, so you'd better don't extends original SearchBox
component. You should create a simple mocked version for SearchBox
component.
E.g.
MyComponent.tsx
:
import React from 'react';
import { SearchSection } from './SearchSection';
export class MyComponent extends React.Component {
render() {
return <SearchSection />;
}
}
SearchBox.tsx
:
import React from 'react';
export class SearchBox extends React.Component {
private timer: ReturnType<typeof setTimeout> | null = null;
public asyncmethod() {
if (this.timer !== null) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
console.log('doSomething');
}, 1000);
console.log('using original class method');
return 'search real text';
}
render() {
return <div>{this.asyncmethod()}</div>;
}
}
SearchBoxSection.tsx
:
import React from 'react';
import { SearchBox } from './SearchBox';
export class SearchSection extends React.Component {
render() {
return <SearchBox />;
}
}
testUtil.tsx
:
import React from 'react';
export class MockedSearchBox extends React.Component {
public asyncMethod() {
console.log('using mocked class method');
return 'Search mock text';
}
render() {
return <div>{this.asyncMethod()}</div>;
}
}
MyComponent.spec.tsx
:
import React from 'react';
import { render } from '@testing-library/react';
import { MyComponent } from './MyComponent';
jest.mock('./SearchBox', () => {
const { MockedSearchBox } = require('./testUtil');
return {
SearchBox: MockedSearchBox,
};
});
describe('My component', () => {
test('that my component shows text', () => {
const { getByText } = render(<MyComponent />);
expect(getByText('Search mock text')).toBeDefined();
});
});
unit test result:
PASS examples/65933863/MyComponent.spec.tsx (9.544 s)
My component
√ that my component shows text (59 ms)
console.log
using mocked class method
at MockedSearchBox.asyncMethod (examples/65933863/testUtil.tsx:5:13)
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
MyComponent.tsx | 100 | 100 | 100 | 100 |
SearchSection.tsx | 100 | 100 | 100 | 100 |
testUtil.tsx | 100 | 100 | 100 | 100 |
-------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.973 s
Source: stackoverflow.com
Related Query
- How to mock a nested component import in react js and test using react testing library
- How to test a component with a nested container with React and Redux?
- How to properly test React Native Modals using Jest and Native Testing Library
- How to test if React component is returning null or its children using React Testing Library?
- How to mock useRef using jest.mock and react testing library
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- how to test Routes in react using jest and react testing library
- How to test axios api using jest and react testing library?
- How to Unit test the rendering of a child component in the parent component using react testing library?
- How to test a function inside React functional component using jest.spyOn with React Testing Library
- How to test react functional component correctly using jest and enzyme?
- how to test button that call submit form using jest and react testing library
- How to test the value of the props of react component rendered using react testing library
- How can I test if child React component was rendered based on a URL change using Jest and Enzyme
- How to unit test a React functional component using hooks useEffect,useDispatch and useSelector and Redux?
- How to Test React's Error Boundary using Jest and React Testing Library
- Problem with testing for error in React component using react-query and axios. Test with React Testing Library, Jest, msw
- How to test React component with children using jest and enzyme?
- How can I pass in methods and states to a nested component while using React Router?
- How to test side-effect when state hook is passed to a child component using React and jest?
- How to write a test case for a simple React component using Jest and Enzyme
- How to test the anchor tag in react class component using jest and enzyme?
- How to mock a parent component which passes props to child component using react testing library
- How do I map dispatch to props within a recursively nested component using React and Redux?
- How to test the style inside the CSS class for React component using Jest and Enzyme?
- How to test react component with hooks using react testing library
- How to get all the child elements inside a component in unit test using react and enzyme?
- How to test if a prop is rendered correctly in a Component using Jest and Enzyme in React
- How to test if state of a component is changed by the function using jest and Enzyme in React TDD
- How can I mock an imported React hook/module and test that it's being called properly on different test cases using Jest
More Query from same tag
- Why we use Bind method on onClick event
- How to call imported helperfunction in react app?
- how to output axios returned data to screen
- How to convert a React array to a JSON object with same key and value?
- Passing the value of page from Pagination.jsx to App.jsx
- Mocking the content of an iframe with React Testing Library
- TypeError: Cannot destructure property 'credentials' of 'this.props' as it is undefined
- handleChange with React and Mobx
- Disabled input is affecting the wrapper div and onClick event isn't working on div on Firefox browser
- gatsby & graphql : filtering for a specific single image
- How to Disable Row in Antd Table
- React.js only reading first if statement
- IndexRoute in react-router
- How can i change a color of button thats inside a map function without changing all the buttons color in react?
- MUIDataTable : onRowclick show data from json in selectable tooltip rather then as a column
- (react-csv) Assign csv header that have the same column name but from different tables
- Which unit test set up is correct? Are both tests checking if the function is right and working correctly?
- Jest,Enzyme,React - Testing Iframe OnLoad
- ReactJS - JavaScript: How to zoom on customized marker after click on it
- Async/await inside promise
- Create a simple react multi-ternary operator on large scale to merge arrays?
- ReactJs PWA not updating on iOS
- How to send newline commands over Websocket message?
- How can I change state of current map item in react component
- Problem with using setTimeout function together with an arrow function that sets a particular state
- Display an image from an api for 24 hours
- React .map() is not a function error
- Hierarchical stacked element using JointJS
- Access this.props in imported function
- React: pass Component as prop and use it with additional props