From 28bd4ba720bb77d5f5c48cd7a45e0015daddc9dd Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Thu, 10 Aug 2023 13:48:09 -0500 Subject: [PATCH] fix(tap-click): do not error in document-less environment (#27972) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue number: N/A --------- ## What is the current behavior? While working on getting our starter app tests running on CI, I ran into the following error: ``` ⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯ ReferenceError: document is not defined ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ ❯ Module.startTapClick node_modules/@ionic/core/components/index9.js:133:15 131| }; 132| const doc = document; 133| doc.addEventListener('ionGestureCaptured', cancelActive); | ^ 134| doc.addEventListener('touchstart', onTouchStart, true); 135| doc.addEventListener('touchcancel', onTouchEnd, true); ❯ node_modules/@ionic/core/components/ion-app.js:21:113 This error originated in "src/App.test.tsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. This error was caught after test environment was torn down. Make sure to cancel any running tasks before test finishes: ``` We are referencing `document` without any "document defined" checks. ## What is the new behavior? - Tap Click is only enabled if the `document` is available since we set event listeners on the document. ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information --- core/src/utils/tap-click/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/src/utils/tap-click/index.ts b/core/src/utils/tap-click/index.ts index 775e9f1f049..5b5f81e1c4e 100644 --- a/core/src/utils/tap-click/index.ts +++ b/core/src/utils/tap-click/index.ts @@ -1,7 +1,13 @@ +import { doc } from '@utils/browser'; + import type { Config } from '../../interface'; import { now, pointerCoord } from '../helpers'; export const startTapClick = (config: Config) => { + if (doc === undefined) { + return; + } + let lastTouch = -MOUSE_WAIT * 10; let lastActivated = 0; @@ -143,7 +149,6 @@ export const startTapClick = (config: Config) => { } }; - const doc = document; doc.addEventListener('ionGestureCaptured', cancelActive); doc.addEventListener('touchstart', onTouchStart, true);