From ac1023c867e4b4eb6be57f07564cddd31ceba716 Mon Sep 17 00:00:00 2001 From: Gabriele Petronella Date: Tue, 31 May 2016 16:58:10 +0200 Subject: [PATCH] Make sure banner hides when dismissOnScroll is false --- src/CookieBanner.js | 9 +++++++-- test/tests/CookieBanner-test.js | 26 +++++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/CookieBanner.js b/src/CookieBanner.js index b997486..5e14083 100644 --- a/src/CookieBanner.js +++ b/src/CookieBanner.js @@ -87,7 +87,12 @@ export default React.createClass({ const { cookie, onAccept } = this.props; cookieLite(cookie, true, 60*60*24*365); onAccept({ cookie }); - this.removeOnScrollListener(); + + if (this.state.listeningScroll) { + this.removeOnScrollListener(); + } else { + this.forceUpdate(); + } }, getStyle(style) { @@ -163,4 +168,4 @@ export default React.createClass({ this.removeOnScrollListener(); } -}); \ No newline at end of file +}); diff --git a/test/tests/CookieBanner-test.js b/test/tests/CookieBanner-test.js index 7a079c4..e6aa1e0 100644 --- a/test/tests/CookieBanner-test.js +++ b/test/tests/CookieBanner-test.js @@ -14,14 +14,17 @@ const resetCookies = function () { } }; -const renderBanner = () => { +const renderBanner = (props) => { const component =
- +
; const cookieWrapper = TestUtils.renderIntoDocument(component); - return TestUtils.scryRenderedDOMComponentsWithClass(cookieWrapper, 'react-cookie-banner'); + return { + wrapper: cookieWrapper, + banner: TestUtils.scryRenderedDOMComponentsWithClass(cookieWrapper, 'react-cookie-banner') + }; }; beforeEach(resetCookies); @@ -29,20 +32,29 @@ beforeEach(resetCookies); describe('CookieBanner', function() { it('should be displayed if no cookies are set', function() { - const banner = renderBanner(); + const banner = renderBanner().banner; expect(banner.length).toBe(1, 'cookie banner is not displayed'); }); it('should hide on click', function() { - const banner = renderBanner()[0]; + const banner = renderBanner().banner[0]; const closeButton = TestUtils.findRenderedDOMComponentWithClass(banner, 'button-close'); TestUtils.Simulate.click(closeButton); - const banner2 = renderBanner()[0]; + const banner2 = renderBanner().banner[0]; const cookieBanner2 = TestUtils.scryRenderedDOMComponentsWithClass(banner2, 'react-cookie-banner'); expect(cookieBanner2.length).toBe(0, 'cookie banner is displayed'); }); + it('should hide on click when dismissOnScroll is false', function() { + const { banner, wrapper } = renderBanner({ dismissOnScroll: false }); + const closeButton = TestUtils.findRenderedDOMComponentWithClass(banner[0], 'button-close'); + TestUtils.Simulate.click(closeButton); + + const cookieBanners = TestUtils.scryRenderedDOMComponentsWithClass(wrapper, 'react-cookie-banner'); + expect(cookieBanners.length).toBe(0, 'cookie banner is displayed'); + }); + it('should be displayed with correct message', function() { const cookieWrapper = TestUtils.renderIntoDocument(
@@ -79,4 +91,4 @@ describe('CookieBanner', function() { expect(_myComponent.length).toBe(1, 'cookie banner is not displaing custom child component'); }); -}); \ No newline at end of file +});