Created ScrollToTop test

This commit is contained in:
Alejandro Celaya 2019-01-13 23:03:31 +01:00
parent d020ed0b13
commit 056286636d
2 changed files with 31 additions and 2 deletions

View file

@ -7,10 +7,10 @@ const ScrollToTop = ({ scrollTo }) => class ScrollToTop extends React.Component
children: PropTypes.node,
};
componentDidUpdate(prevProps) {
componentDidUpdate({ location: prevLocation }) {
const { location } = this.props;
if (location !== prevProps.location) {
if (location !== prevLocation) {
scrollTo(0, 0);
}
}

View file

@ -0,0 +1,29 @@
import React from 'react';
import { shallow } from 'enzyme';
import * as sinon from 'sinon';
import createScrollToTop from '../../src/common/ScrollToTop';
describe('<ScrollToTop />', () => {
let wrapper;
const window = {
scrollTo: sinon.spy(),
};
beforeEach(() => {
const ScrollToTop = createScrollToTop(window);
wrapper = shallow(<ScrollToTop locaction={{ href: 'foo' }}>Foobar</ScrollToTop>);
});
afterEach(() => {
wrapper.unmount();
window.scrollTo.resetHistory();
});
it('just renders children', () => expect(wrapper.text()).toEqual('Foobar'));
it('scrolls to top when location changes', () => {
wrapper.instance().componentDidUpdate({ location: { href: 'bar' } });
expect(window.scrollTo.calledOnce).toEqual(true);
});
});