* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { isEmpty, keyBy } from 'lodash';
+import { isEmpty, keyBy, throttle } from 'lodash';
import * as React from 'react';
import { getValues } from '../../api/settings';
import { Alert } from '../../components/ui/Alert';
} from './available-features/withAvailableFeatures';
import './SystemAnnouncement.css';
+const THROTTLE_TIME_MS = 10000;
+
interface State {
displayMessage: boolean;
message: string;
componentDidMount() {
if (this.props.hasFeature(Feature.Announcement)) {
this.getSettings();
- document.addEventListener('visibilitychange', this.handleVisibilityChange);
+ window.addEventListener('focus', this.handleVisibilityChange);
}
}
componentWillUnmount() {
if (this.props.hasFeature(Feature.Announcement)) {
- document.removeEventListener('visibilitychange', this.handleVisibilityChange);
+ window.removeEventListener('focus', this.handleVisibilityChange);
}
}
});
};
- handleVisibilityChange = () => {
+ // eslint-disable-next-line react/sort-comp
+ handleVisibilityChange = throttle(() => {
if (document.visibilityState === 'visible') {
this.getSettings();
}
- };
+ }, THROTTLE_TIME_MS);
render() {
const { displayMessage, message } = this.state;
getValues: jest.fn()
}));
+jest.mock('lodash', () => {
+ const lodash = jest.requireActual('lodash');
+ lodash.throttle = (fn: any) => () => fn();
+ return lodash;
+});
+
it('should display system announcement', async () => {
(getValues as jest.Mock)
.mockResolvedValueOnce([
renderSystemAnnouncement();
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
- fireEvent(document, new Event('visibilitychange'));
+ fireEvent(window, new Event('focus'));
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
- fireEvent(document, new Event('visibilitychange'));
+ fireEvent(window, new Event('focus'));
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
- fireEvent(document, new Event('visibilitychange'));
+ fireEvent(window, new Event('focus'));
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
- fireEvent(document, new Event('visibilitychange'));
+ fireEvent(window, new Event('focus'));
expect(await screen.findByRole('alert')).toBeInTheDocument();
expect(screen.getByText('Foo')).toBeInTheDocument();
});