ant-design/components/affix/__tests__/Affix.test.js

77 lines
1.7 KiB
JavaScript
Raw Normal View History

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() {
2017-10-09 13:23:20 +08:00
return (
<div
style={{
2017-10-09 13:23:20 +08:00
height: 100,
overflowY: 'scroll',
}}
2017-10-09 13:23:20 +08:00
ref={(node) => { this.container = node; }}
>
2017-10-09 13:23:20 +08:00
<div
className="background"
style={{
paddingTop: 60,
height: 300,
}}
>
2017-10-09 13:23:20 +08:00
<Affix
target={() => this.container}
ref={ele => this.affix = ele}
>
<Button type="primary" >
Fixed at the top of container
</Button>
</Affix>
</div>
</div>
2017-10-09 13:23:20 +08:00
);
}
}
describe('Affix Render', () => {
2017-11-13 10:46:22 +08:00
beforeAll(() => {
jest.useFakeTimers();
});
2017-11-09 20:33:25 +08:00
afterAll(() => {
jest.useRealTimers();
});
2017-11-13 10:46:22 +08:00
it('Anchor render perfectly', () => {
document.body.innerHTML = '<div id="mounter" />';
const wrapper = mount(<AffixMounter />, { attachTo: document.getElementById('mounter') });
jest.runAllTimers();
2017-09-20 16:26:18 +08:00
wrapper.instance().affix.fixedNode.parentNode.getBoundingClientRect = jest.fn(() => {
return {
bottom: 100, height: 28, left: 0, right: 0, top: -50, width: 195,
};
});
events.scroll({
type: 'scroll',
});
jest.runAllTimers();
2017-09-20 16:26:18 +08:00
expect(wrapper.instance().affix.state.affixStyle).not.toBe(null);
});
});