From fbd2bf4e1ef08844a38ff9595722d446e3b16af6 Mon Sep 17 00:00:00 2001 From: Henric Trotzig + Joe Lencioni Date: Fri, 30 Jan 2015 09:29:01 -0800 Subject: [PATCH] Fix spec for window scroll The spec that I previously wrote for when the scrollable parent is equal to the window element (a3dc4593) only worked in Chrome. Our Travis integration runs a Firefox instance, and in that browser the spec failed. It turns out that Chrome will allow you to do `window.scrollTop = x` and it will scroll to x. Firefox silently ignores that call. The way we found to work in both browsers were `window.scroll(0, x)`. Once that was fixed, I found another issue with our test setup. We weren't cleaning up the divs we leave on the page. This caused the last spec (testing scroll on the window element) to be contaminated by those divs. We added an afterEach hook to unmount components as they were tested. --- spec/waypoint_spec.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/spec/waypoint_spec.js b/spec/waypoint_spec.js index ef1ceee..ba1867b 100644 --- a/spec/waypoint_spec.js +++ b/spec/waypoint_spec.js @@ -1,15 +1,21 @@ var React = require('react'); var Waypoint = require('../src/waypoint'); +var div; + var renderAttached = function(component) { - var div = document.createElement('div'); + div = document.createElement('div'); document.documentElement.appendChild(div); var renderedComponent = React.render(component, div); return renderedComponent; }; var scrollNodeTo = function(node, scrollTop) { - node.scrollTop = scrollTop; + if (node === window) { + window.scroll(0, scrollTop); + } else { + node.scrollTop = scrollTop; + } var event = document.createEvent('Event'); event.initEvent('scroll', false, false); node.dispatchEvent(event); @@ -44,6 +50,12 @@ describe('Waypoint', function() { }; }); + afterEach(function() { + if (div) { + React.unmountComponentAtNode(div); + } + }); + describe('when the Waypoint is visible on mount', function() { beforeEach(function() { this.topSpacerHeight = 90; @@ -245,8 +257,8 @@ describe('Waypoint', function() { this.parentStyle = {}; // Make the spacers large enough to make the Waypoint render off-screen - this.topSpacerHeight = 4000; - this.bottomSpacerHeight = 4000; + this.topSpacerHeight = window.innerHeight + 1000; + this.bottomSpacerHeight = 1000; }); it('does not fire the onEnter handler on mount', function() { @@ -256,7 +268,7 @@ describe('Waypoint', function() { describe('when the Waypoint is in view', function() { beforeEach(function() { this.subject(); - scrollNodeTo(window, 3900); + scrollNodeTo(window, this.topSpacerHeight - window.innerHeight / 2); }); it('fires the onEnter handler', function() {