From a936a3bfed274cf57792576528eb2e4e70684bdc Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Tue, 13 Nov 2018 12:43:42 -0500 Subject: [PATCH] Data: Add test to assure against lingering listeners --- .../src/components/with-select/test/index.js | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/data/src/components/with-select/test/index.js b/packages/data/src/components/with-select/test/index.js index 4a874872df032..b8cb523edfa00 100644 --- a/packages/data/src/components/with-select/test/index.js +++ b/packages/data/src/components/with-select/test/index.js @@ -7,7 +7,7 @@ import TestRenderer from 'react-test-renderer'; * WordPress dependencies */ import { compose } from '@wordpress/compose'; -import { Component } from '@wordpress/element'; +import { Component, renderToString } from '@wordpress/element'; /** * Internal dependencies @@ -300,6 +300,35 @@ describe( 'withSelect', () => { expect( OriginalComponent ).toHaveBeenCalledTimes( 1 ); } ); + it( 'should not leave lingering listeners if rendered to string', () => { + registry.registerStore( 'demo', { + reducer: () => ( {} ), + selectors: { + getState: ( state ) => state, + }, + actions: { + update: () => ( { type: 'update' } ), + }, + } ); + + const DataBoundComponent = compose( [ + withSelect( () => {} ), + ] )( () =>
); + + const unsubscribe = jest.fn(); + const subscribe = jest.fn( () => unsubscribe ); + + renderToString( + + + + ); + + // A case could be made to enforce that no subscriptions were made. For + // the purposes of the test, at least ensure there are none lingering. + expect( subscribe.mock.calls ).toHaveLength( unsubscribe.mock.calls.length ); + } ); + it( 'should render if props have changed but not state', () => { registry.registerStore( 'unchanging', { reducer: ( state = {} ) => state,