Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-native): bring back check for window.addEventListener #3345

Merged
merged 1 commit into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/focusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/core/onlineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/core/tests/focusManager.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions src/core/tests/onlineManager.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down