Я новичок в тестировании и не могу понять, как тестировать мой компонент, мой onScrollMock не запускается с кодом ниже, я использую, шутка и энзим. пожалуйста, помогите.
Я не уверен, как подойти к условию внутри handleScroll, как тестовое покрытие указывает на эту функцию.
StoryCardList. js
const handleScroll = (scrollEvent) => {
const { scrollWidth, scrollLeft, clientWidth } = scrollEvent.target;
const isRightEnd = scrollWidth - scrollLeft === clientWidth;
setAnimation(isRightEnd);
};
function overlay(id, heroImage) {
return (
<div
key={id}
className="story-card-list__overlay"
style={{
transition: `all ${transitionDuration}`,
opacity: animate && '1',
visibility: animate && 'visible',
transitionDelay: animate && transitionTiming,
bottom: !animate && '999px',
}}
>
<StoryCard title="" heroImage={heroImage} />
<div className="story-card-list__overlay-elements">
<p className="story-card-list__overlay-elements__title">Continue watching story</p>
<p className="story-card-list__overlay-elements__subtitle">Continue watching story</p>
<StoriesMoreButton path="/story-list" />
</div>
</div>
);
}
return (
<div className="story-card-list" onScroll={(scrollEvent) => handleScroll(scrollEvent)}>
{stories.map(({ id, title, heroImage }, index, sourceStories) => {
if (index === stories.length - 1) {
return lastStory(id, title, heroImage, index, sourceStories);
}
return renderStoryCards(id, title, heroImage, index, sourceStories);
})}
</div>
);
тест
let wrapper: ShallowWrapper;
const setAnimationMock = jest.fn(() => true);
const onScrollMock = jest.fn();
const setState = jest.fn();
const useStateSpy = jest.spyOn(React, 'useState');
useStateSpy.mockImplementation((init) => [init, setState]);
beforeEach(() => {
wrapper = shallow(
<StoryCardList
stories={stories}
onScroll={onScrollMock}
transitionDuration="2"
transitionTiming="2"
setAnimation={setAnimationMock}
onClick={setAnimationMock}
animate
/>
);
});
it('should have scrollable div', () => {
const logSpy = jest.spyOn(console, 'log');
const mEvent = {
target: {
scrollWidth: 100,
scrollLeft: 50,
clientWidth: 50,
},
};
wrapper.find('.story-card-list').simulate('scroll', mEvent);
expect(setAnimationMock).toBeCalledWith(true);
});