aneagoie / robofriends-testing Goto Github PK
View Code? Open in Web Editor NEWZTM tutorial - adding tests to a react app
ZTM tutorial - adding tests to a react app
I wonder if there is someone who was able to write any tests for MainPage as Functional Component
I did manage to maintain the project used functional components only. However testing seems to be the most bizarre topic to me.
Andrei uses wrapper.instance() to get to the filterRobots function. As it turns out it doesn't work with Functional components. It returns null.
import React, { useEffect } from 'react';
import CardList from '../components/cards/CardList';
import SearchBox from '../components/SearchBox';
import Spinner from '../components/Spinner';
import Header from '../components/Header'
import { Suspense } from 'react';
import './MainPage.css';
const MainPage = ({ robots, fetchRobotsList, isPending, search, onSearchChange }) => {
useEffect(() => {
fetchRobotsList()
}, []);
const filteredRobots = robots.filter(({ name }) =>
name.toLowerCase().includes(search))
return (
<Suspense fallback={Spinner}>
<div className="tc">
<Header />
<SearchBox search={onSearchChange} />
{ isPending ? <Spinner /> : <CardList robots={filteredRobots} /> }
</div>
</Suspense>
);
};
export default MainPage;
Now, my intentions were to find an input of type search simulating an actual input and then comparing a result with props search. I cannot do this. Wrapper returns found 0 nodes. I feel like I'm thrown in at the deep end. What? How? Whyyyyyyyyyyyy?
import { shallow, mount } from 'enzyme';
import React from 'react';
import MainPage from './MainPage';
let wrapper;
it('filters robots correctly', () => {
const mockedProps = {
robots: [
{
id: 1,
name: 'Leanne Graham',
username: 'Bret',
email: '[email protected]'
},
],
fetchRobotsList: jest.fn(),
isPending: false,
search: '',
onSearchChange: jest.fn(),
}
wrapper = shallow(<MainPage { ...mockedProps } />);
wrapper.find('input[type="search"]').simulate('input', {target: { value: 'Leanne'}})
expect(wrapper.props().search).toEqual('Leanne')
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.