У меня есть React Route, который отображает либо сообщение без результата, либо список элементов в зависимости от того, равна ли длина возвращаемых данных 0 или нет. Я создал тестовый пакет с Jest, который работает, но я чувствую, что он слишком избыточный и длинный. Может ли быть лучший способ сделать это?
Content.js (компонент React)
import React from 'react'
import { Route } from 'react-router-dom'
import Switch from 'react-router-dom/Switch';
import List from './List/List'
import NoResult from './NoResult/NoResult'
import LoadMore from './LoadMore/LoadMore'
const content = (props) => {
return (
<section className="content">
<Switch>
<Route
exact
path="/"
render={() =>
// display no result message if there no result is returned from search
props.searched && props.data.length === 0 ?
<NoResult>
Sorry, no result! Try again :)
</NoResult> :
// display list of items if result is returned
<div>
<List
data={props.data}
favorites={props.favorites}
addToFavorites={props.addToFavorites}
removeFromFavorites={props.removeFromFavorites}
/>
{ // display load more button if the search term has not changed
props.hasMore && props.searchTerm === props.prevSearchTerm ?
<LoadMore loadMore={props.loadMore} /> : null}
</div>
}
/>
<Route
path="/favorites"
render={() =>
// display no favorites message if nothing has been added
props.favorites.length === 0 ?
<NoResult>
You don't have any favorites yet.<br></br>Try adding some!
</NoResult> :
// else display list of favorite items
<List
data={props.favorites}
removeFromFavorites={props.removeFromFavorites}
/>
}
/>
</Switch>
</section>
)
}
export default content
Content.test.js (Jest test file)
import React from 'react'
import { MemoryRouter, Route } from 'react-router';
import { configure, shallow, mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import List from './List/List'
import NoResult from './NoResult/NoResult'
import LoadMore from './LoadMore/LoadMore'
configure({ adapter: new Adapter()})
describe('Path /', () => {
it('should render <NoResult /> component when there is no result returned from search', () => {
const mockProps = {
searched: true,
data: [],
}
const wrapper = mount(<MemoryRouter>
<Route
exact
path="/"
render={() =>
// display no result message if there no result is returned from search
mockProps.searched && mockProps.data.length === 0 ?
<NoResult>
Sorry, no result! Try again :)
</NoResult> :
// display list of items if result is returned
<div>
<List
data={mockProps.data}
favorites={mockProps.favorites}
addToFavorites={mockProps.addToFavorites}
removeFromFavorites={mockProps.removeFromFavorites}
/>
{ // display load more button if the search term has not changed
mockProps.hasMore && mockProps.searchTerm === mockProps.prevSearchTerm ?
<LoadMore loadMore={mockProps.loadMore} /> : null}
</div>
}
/>
</MemoryRouter>)
expect(wrapper.find(NoResult)).toHaveLength(1)
})
it('should render <List /> component when there is 1 or more search results', () => {
const mockProps = {
searched: true,
data: [{
id: 0,
images: {
fixed_width: {
width: 200
}
}
}],
favorites: []
}
const wrapper = mount(<MemoryRouter>
<Route
exact
path="/"
render={() =>
// display no result message if there no result is returned from search
mockProps.searched && mockProps.data.length === 0 ?
<NoResult>
Sorry, no result! Try again :)
</NoResult> :
// display list of items if result is returned
<div>
<List
data={mockProps.data}
favorites={mockProps.favorites}
addToFavorites={mockProps.addToFavorites}
removeFromFavorites={mockProps.removeFromFavorites}
/>
{ // display load more button if the search term has not changed
mockProps.hasMore && mockProps.searchTerm === mockProps.prevSearchTerm ?
<LoadMore loadMore={mockProps.loadMore} /> : null}
</div>
}
/>
</MemoryRouter>)
expect(wrapper.find(List)).toHaveLength(1)
})
})