From 6a97f80d13fc6a4ebb38798b02ce79cb3897cd72 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Sun, 27 Feb 2022 07:49:06 +0100 Subject: [PATCH] fix(react-native): bring back check for window.addEventListener because it does not exist in RN, but window is defined --- src/core/focusManager.ts | 4 +++- src/core/onlineManager.ts | 4 +++- src/core/tests/focusManager.test.tsx | 13 +++++++++++++ src/core/tests/onlineManager.test.tsx | 13 +++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/core/focusManager.ts b/src/core/focusManager.ts index 2ffb62eaaf..7bd68b6781 100644 --- a/src/core/focusManager.ts +++ b/src/core/focusManager.ts @@ -14,7 +14,9 @@ export class FocusManager extends Subscribable { constructor() { super() this.setup = onFocus => { - if (!isServer) { + // addEventListener does not exist in React Native, but window does + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (!isServer && window.addEventListener) { const listener = () => onFocus() // Listen to visibillitychange and focus window.addEventListener('visibilitychange', listener, false) diff --git a/src/core/onlineManager.ts b/src/core/onlineManager.ts index 7c3d832b63..844e232e03 100644 --- a/src/core/onlineManager.ts +++ b/src/core/onlineManager.ts @@ -14,7 +14,9 @@ export class OnlineManager extends Subscribable { constructor() { super() this.setup = onOnline => { - if (!isServer) { + // addEventListener does not exist in React Native, but window does + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (!isServer && window.addEventListener) { const listener = () => onOnline() // Listen to online window.addEventListener('online', listener, false) diff --git a/src/core/tests/focusManager.test.tsx b/src/core/tests/focusManager.test.tsx index b3086179ce..3089abfc94 100644 --- a/src/core/tests/focusManager.test.tsx +++ b/src/core/tests/focusManager.test.tsx @@ -73,6 +73,19 @@ describe('focusManager', () => { restoreIsServer() }) + test('cleanup should still be undefined if window.addEventListener is not defined', async () => { + const { addEventListener } = globalThis.window + + // @ts-expect-error + globalThis.window.addEventListener = undefined + + const unsubscribe = focusManager.subscribe(() => undefined) + expect(focusManager['cleanup']).toBeUndefined() + + unsubscribe() + globalThis.window.addEventListener = addEventListener + }) + it('should replace default window listener when a new event listener is set', async () => { const addEventListenerSpy = jest.spyOn( globalThis.window, diff --git a/src/core/tests/onlineManager.test.tsx b/src/core/tests/onlineManager.test.tsx index 78def64c60..cdf6a0ce54 100644 --- a/src/core/tests/onlineManager.test.tsx +++ b/src/core/tests/onlineManager.test.tsx @@ -67,6 +67,19 @@ describe('onlineManager', () => { restoreIsServer() }) + test('cleanup should still be undefined if window.addEventListener is not defined', async () => { + const { addEventListener } = globalThis.window + + // @ts-expect-error + globalThis.window.addEventListener = undefined + + const unsubscribe = onlineManager.subscribe(() => undefined) + expect(onlineManager['cleanup']).toBeUndefined() + + unsubscribe() + globalThis.window.addEventListener = addEventListener + }) + test('it should replace default window listener when a new event listener is set', async () => { const addEventListenerSpy = jest.spyOn( globalThis.window,