Skip to content

Commit

Permalink
fix(react-native): bring back check for window.addEventListener (#3345)
Browse files Browse the repository at this point in the history
because it does not exist in RN, but window is defined
  • Loading branch information
TkDodo authored Feb 27, 2022
1 parent f426899 commit 02fd793
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
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

0 comments on commit 02fd793

Please sign in to comment.