import React from 'react'; import { mount } from 'enzyme'; import Affix from '..'; import Button from '../../button'; const events = {}; class AffixMounter extends React.Component { componentDidMount() { this.container.scrollTop = 100; this.container.addEventListener = jest.fn().mockImplementation((event, cb) => { events[event] = cb; }); } getTarget = () => { return this.container; } render() { return (
{ this.container = node; }} >
this.container} ref={ele => this.affix = ele} >
); } } describe('Affix Render', () => { beforeAll(() => { jest.useFakeTimers(); }); afterAll(() => { jest.useRealTimers(); }); it('Anchor render perfectly', () => { document.body.innerHTML = '
'; const wrapper = mount(, { attachTo: document.getElementById('mounter') }); jest.runAllTimers(); wrapper.node.affix.refs.fixedNode.parentNode.getBoundingClientRect = jest.fn(() => { return { bottom: 100, height: 28, left: 0, right: 0, top: -50, width: 195, }; }); events.scroll({ type: 'scroll', }); jest.runAllTimers(); expect(wrapper.node.affix.state.affixStyle).not.toBe(null); }); });