Jest unit tests for react fails with the same error: Method “example” is meant to be run on 1 node. 0 found instead
I have next react component:
import React from 'react';
import { connect } from 'react-redux';
import { makeSelectFavoriteByMerchantId } from 'core/selectors/favorites';
import MerchantExperienceLink from 'core/modules/MerchantExperienceLink/MerchantExperienceLink';
import FavoriteIcon from 'core/modules/FavoriteIcon/FavoriteIcon';
import MerchantClickUrlLink from 'core/modules/MerchantClickUrlLink/MerchantClickUrlLink';
import MerchantRebate from 'core/modules/MerchantRebate/MerchantRebate';
import MerchantNoRebateLabel from 'core/modules/MerchantNoRebateLabel/MerchantNoRebateLabel';
function FavoritesListItem({ merchant, config, isFavorited }) {
const {
name, id, logoUrls = {}, offersCount, rebate, showRebate,
} = merchant;
const { rebateOptions } = config;
const renderActiveMerchants = () => (
<div
className="mn_favoriteMerchant"
data-merchant-id={id}
data-merchant-name={name}
role="listitem"
data-test="favorite-merchant"
>
<div className="mn_favoriteMerchantInner">
<MerchantExperienceLink
className="mn_favoriteMerchantLink"
merchant={merchant}
title={`Opens merchant detail page at ${name}`}
>
<FavoriteIcon
merchantId={id}
merchantName={name}
labelUnfavorite={`Remove ${name} from Favorites list`}
showSpinner={!isFavorited}
/>
<div className="mn_logo"><img data-test="favorited-merchant-logo" src={logoUrls._120x60} alt={name} /></div>
<p className="mn_offersCount" data-test="favorited-merchant-offers-count">{offersCount} offers available </p>
</MerchantExperienceLink>
{rebate && (
<MerchantClickUrlLink className="mn_favoriteMerchantRebateLink" merchant={merchant}>
<div className="mn_rebate">
{showRebate
? <MerchantRebate {...rebate} {...rebateOptions} />
: <MerchantNoRebateLabel />}
</div>
</MerchantClickUrlLink>
)}
</div>
</div>
);
const renderDeactivatedMerchants = () => (
<div
className="mn_favoriteMerchant"
data-merchant-id={id}
data-merchant-name={name}
role="listitem"
data-test="favorite-merchant"
>
<div className="mn_favoriteMerchantInner">
<MerchantExperienceLink
className="mn_favoriteMerchantLink"
merchant={merchant}
title={`Opens merchant detail page at ${name}`}
>
<FavoriteIcon
merchantId={id}
merchantName={name}
labelUnfavorite={`Remove ${name} from Favorites list`}
showSpinner={!isFavorited}
/>
<div className="mn_logo norebateMerchantLogo"><img data-test="favorited-merchant-logo" src={logoUrls._120x60} alt={name} /></div>
{rebate && (
<div className="mn_rebate mn_deactivatedRebate">
{showRebate
? <MerchantNoRebateLabel />
: <MerchantRebate {...rebate}/>}
</div>
)}
</MerchantExperienceLink>
</div>
</div>);
return (
merchant.type === "Deactivated Merchant" ? renderDeactivatedMerchants() : renderActiveMerchants()
);
}
const mapStateToProps = () => {
const selectFavoriteByMerchantId = makeSelectFavoriteByMerchantId();
return (state, { merchant }) => ({
isFavorited: selectFavoriteByMerchantId(state, merchant.id),
});
};
export default connect(mapStateToProps)(FavoritesListItem);
And now trying to write some unit tests for it: this.is my .test file:
import React from 'react';
import { shallow, mount } from 'enzyme';
import { get, merge, } from 'lodash';
import { findByTestAttr, storeFactory } from '../../testUtils';
import FavoritesListItem from 'core/modules/FavoritesManager/components/FavoritesListItem';
import MerchantExperienceLink from 'core/modules/MerchantExperienceLink/MerchantExperienceLink';
import FavoriteIcon from 'core/modules/FavoriteIcon/FavoriteIcon';
import MerchantClickUrlLink from 'core/modules/MerchantClickUrlLink/MerchantClickUrlLink';
import MerchantRebate from 'core/modules/MerchantRebate/MerchantRebate';
import MerchantNoRebateLabel from 'core/modules/MerchantNoRebateLabel/MerchantNoRebateLabel';
jest.mock('./assets/images/favorite.inline.svg', () => () => 'FavoriteIcon');
jest.mock('./assets/images/spin.inline.svg', () => () => 'FavoriteIcon');
import {
storeMock,
} from './storeMocks';
describe('Favorites List Item', () => {
let wrapper;
let store;
let merchantMock;
let configMock;
let spy;
const setup = (initialState={}, customProps={}) => {
const state = merge(
{},
storeMock,
initialState,
);
const props = merge(
{},
{
merchant: merchantMock,
config: configMock
},
customProps,
);
store = storeFactory(state);
return shallow(<FavoritesListItem store={store} {...props} />).dive().dive();
};
beforeEach(() => {
merchantMock = get(storeMock, 'favoritedStoresData.merchants[0]');
configMock = get(storeMock, 'favoritesManager.config.favoritesSection');
wrapper = setup();
});
test('renders a correct text for NoRebate Merchant', () => {
wrapper = setup({}, {
merchant: merge({}, merchantMock, { showRebate: false }),
});
const component = wrapper.find(MerchantNoRebateLabel).dive().find('.mn_rebateValue');
expect(component.text().toEqual('No reward'));
});
test('pass correct merchant.type if we have showRebate: false', () => {
wrapper = setup({}, {
merchant: merge({}, merchantMock, { showRebate: false }, {type: 'Deactivated Merchant'}),
});
const component = wrapper.find(MerchantNoRebateLabel);
expect(component.prop('type')).toEqual('Deactivated Merchant');
});
test('calls a "renderDeactivatedMerchants" method if merchant have type: "Deactivated Merchant"', () => {
wrapper = setup({}, {
merchant: merge({}, merchantMock, { showRebate: false }, {type: 'Deactivated Merchant'}),
});
const component = wrapper.find(MerchantNoRebateLabel).dive();
spy = jest.spyOn(component.instance(), 'renderDeactivatedMerchants');
expect(spy).toHaveBeenCalled();
});
});
But all of them are failed with the same error:
Method "text"/“props”/"dive"/ is meant to be run on 1 node. 0 found instead.
Please help me correct this tests - its my first unit tests, and I do it something wrong. maybe also I can test something another in this component, but regarding this renderDeactivatedMerchants function because its new in this code.