diff --git a/src/components/navigation/www/navigation.jsx b/src/components/navigation/www/navigation.jsx index 3346aeb79..4844fe8ca 100644 --- a/src/components/navigation/www/navigation.jsx +++ b/src/components/navigation/www/navigation.jsx @@ -37,14 +37,16 @@ class Navigation extends React.Component { componentDidMount () { if (this.props.user) { // Setup polling for messages to start in 2 minutes. - setTimeout(this.pollForMessages.bind(this, 2), 2 * 60 * 1000); + const twoMinInMs = 2 * 60 * 1000; + setTimeout(this.pollForMessages.bind(this, twoMinInMs), twoMinInMs); } } componentDidUpdate (prevProps) { if (prevProps.user !== this.props.user) { this.props.handleCloseAccountNav(); if (this.props.user) { - setTimeout(this.pollForMessages.bind(this, 2), 2 * 60 * 1000); + const twoMinInMs = 2 * 60 * 1000; + setTimeout(this.pollForMessages.bind(this, twoMinInMs), twoMinInMs); } else { // clear message count check, and set to default id. clearTimeout(this.state.messageCountIntervalId); @@ -70,16 +72,16 @@ class Navigation extends React.Component { return `/users/${this.props.user.username}/`; } - pollForMessages (minutes) { + pollForMessages (ms) { this.props.getMessageCount(this.props.user.username); // We only poll if it has been less than 30 minutes. // Chances of someone actively using the page for that long without // a navigation is low. - if (minutes < 32) { - const nextFetch = minutes * 2; + if (ms < 32 * 60 * 1000) { // 32 minutes + const nextFetch = ms * 2; // exponentially back off next fetch time. const timeoutId = setTimeout(() => { this.pollForMessages(nextFetch); - }, nextFetch * 60000); + }, nextFetch); this.setState({ // eslint-disable-line react/no-did-mount-set-state messageCountIntervalId: timeoutId }); diff --git a/test/unit/components/navigation.test.jsx b/test/unit/components/navigation.test.jsx index 2333bc509..07c8a7139 100644 --- a/test/unit/components/navigation.test.jsx +++ b/test/unit/components/navigation.test.jsx @@ -167,16 +167,16 @@ describe('Navigation', () => { // has already called pollForMessages. jest.clearAllTimers(); jest.clearAllMocks(); - - navInstance.pollForMessages(2); + let twoMinInMs = 2 * 60 * 1000; // 2 minutes in ms. + navInstance.pollForMessages(twoMinInMs); // Check that we set the timeout to backoff exponentially - let minutes = 2 * 60 * 1000; + for (let count = 1; count < 5; ++count) { - jest.advanceTimersByTime(minutes + 1); - expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), minutes * 2); + jest.advanceTimersByTime(twoMinInMs + 1); + expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), twoMinInMs * 2); expect(props.getMessageCount).toHaveBeenCalledTimes(count); - minutes = minutes * 2; + twoMinInMs = twoMinInMs * 2; } // Exhaust all timers (there shouldn't be any left)