From 056286636d4c03165791f1b6578b4eb3225aa9c6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 13 Jan 2019 23:03:31 +0100 Subject: [PATCH] Created ScrollToTop test --- src/common/ScrollToTop.js | 4 ++-- test/common/ScrollToTop.test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/common/ScrollToTop.test.js diff --git a/src/common/ScrollToTop.js b/src/common/ScrollToTop.js index 67ccb7d8..28e776b6 100644 --- a/src/common/ScrollToTop.js +++ b/src/common/ScrollToTop.js @@ -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); } } diff --git a/test/common/ScrollToTop.test.js b/test/common/ScrollToTop.test.js new file mode 100644 index 00000000..77d428e5 --- /dev/null +++ b/test/common/ScrollToTop.test.js @@ -0,0 +1,29 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import * as sinon from 'sinon'; +import createScrollToTop from '../../src/common/ScrollToTop'; + +describe('', () => { + let wrapper; + const window = { + scrollTo: sinon.spy(), + }; + + beforeEach(() => { + const ScrollToTop = createScrollToTop(window); + + wrapper = shallow(Foobar); + }); + + 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); + }); +});