Skip to content

Commit a087deb

Browse files
committed
fix: proper definition for isBrowser and isNavigator states.
should fix: #1777 Also introduce jest config for ssr tests (it fails hard now).
1 parent f938995 commit a087deb

12 files changed

+58
-27
lines changed

jest.config.base.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Config } from '@jest/types';
2+
3+
export const baseJestConfig: Config.InitialOptions = {
4+
'preset': 'ts-jest',
5+
'clearMocks': true,
6+
'coverageDirectory': 'coverage',
7+
'testMatch': [
8+
'<rootDir>/tests/**/*.test.(ts|tsx)'
9+
],
10+
'setupFiles': [
11+
'<rootDir>/tests/setupTests.ts'
12+
]
13+
}

jest.config.node.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from '@jest/types';
2+
import { baseJestConfig } from './jest.config.base';
3+
4+
const config: Config.InitialOptions = {
5+
...baseJestConfig,
6+
testEnvironment: 'node', // browser-like
7+
}
8+
9+
export default config;

jest.config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from '@jest/types';
2+
import { baseJestConfig } from './jest.config.base';
3+
4+
const config: Config.InitialOptions = {
5+
...baseJestConfig,
6+
testEnvironment: 'jsdom', // browser-like
7+
}
8+
9+
export default config;

package.json

+2-12
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
"scripts": {
1515
"start": "yarn storybook",
1616
"test": "jest --maxWorkers 2",
17+
"test:ssr": "jest --maxWorkers 2 --config ./jest.config.node.ts",
1718
"test:watch": "jest --watch",
1819
"test:coverage": "jest --coverage",
1920
"lint": "eslint {src,tests}/**/*.{ts,tsx}",
2021
"lint:fix": "yarn lint --fix",
21-
"lint:types": "tsc --noEmit",
22+
"lint:types": "tsc --noEmit ",
2223
"lint:prettier": "prettier --write src/**/**/*.{ts,tsx}",
2324
"build:cjs": "tsc",
2425
"build:es": "tsc -m esNext --outDir esm",
@@ -162,16 +163,5 @@
162163
"collective": {
163164
"type": "opencollective",
164165
"url": "https://opencollective.com/react-use"
165-
},
166-
"jest": {
167-
"preset": "ts-jest",
168-
"clearMocks": true,
169-
"coverageDirectory": "coverage",
170-
"testMatch": [
171-
"<rootDir>/tests/**/*.test.(ts|tsx)"
172-
],
173-
"setupFiles": [
174-
"<rootDir>/tests/setupTests.ts"
175-
]
176166
}
177167
}

src/misc/util.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export function off<T extends Window | Document | HTMLElement | EventTarget>(
1818
}
1919
}
2020

21-
export const isBrowser = typeof window === 'object';
21+
export const isBrowser = typeof window !== 'undefined';
22+
export const isNavigator = typeof navigator !== 'undefined';

src/useBattery.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import { off, on } from './misc/util';
2+
import { isNavigator, off, on } from './misc/util';
33
import isDeepEqual from './misc/isDeepEqual';
44

55
export interface BatteryState {
@@ -25,7 +25,7 @@ type UseBatteryState =
2525
| { isSupported: true; fetched: false } // battery API supported but not fetched yet
2626
| (BatteryState & { isSupported: true; fetched: true }); // battery API supported and fetched
2727

28-
const nav: NavigatorWithPossibleBattery | undefined = typeof navigator === 'object' ? navigator : undefined;
28+
const nav: NavigatorWithPossibleBattery | undefined = isNavigator ? navigator : undefined;
2929
const isBatteryApiSupported = nav && typeof nav.getBattery === 'function';
3030

3131
function useBatteryMock(): UseBatteryState {

src/useIsomorphicLayoutEffect.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useLayoutEffect } from 'react';
2+
import { isBrowser } from './misc/util';
23

3-
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
4+
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
45

56
export default useIsomorphicLayoutEffect;

src/useMediaDevices.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import { noop, off, on } from './misc/util';
2+
import { isNavigator, noop, off, on } from './misc/util';
33

44
const useMediaDevices = () => {
55
const [state, setState] = useState({});
@@ -34,4 +34,4 @@ const useMediaDevices = () => {
3434

3535
const useMediaDevicesMock = () => ({});
3636

37-
export default typeof navigator === 'object' && !!navigator.mediaDevices ? useMediaDevices : useMediaDevicesMock;
37+
export default isNavigator && !!navigator.mediaDevices ? useMediaDevices : useMediaDevicesMock;

src/useNetworkState.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import { off, on } from './misc/util';
2+
import { isNavigator, off, on } from './misc/util';
33
import { IHookStateInitAction } from './misc/hookState';
44

55
export interface INetworkInformation extends EventTarget {
@@ -69,7 +69,7 @@ export interface IUseNetworkState {
6969

7070
const nav:
7171
| (Navigator & Partial<Record<'connection' | 'mozConnection' | 'webkitConnection', INetworkInformation>>)
72-
| undefined = navigator;
72+
| undefined = isNavigator ? navigator : undefined;
7373
const conn: INetworkInformation | undefined = nav && (nav.connection || nav.mozConnection || nav.webkitConnection);
7474

7575
function getConnectionState(previousState?: IUseNetworkState): IUseNetworkState {

src/useVibrate.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useEffect } from 'react';
2-
import { noop } from './misc/util';
2+
import { isNavigator, noop } from './misc/util';
33

44
export type VibrationPattern = number | number[];
55

6-
const isVibrationApiSupported = typeof navigator === 'object' && 'vibrate' in navigator;
6+
const isVibrationApiSupported = isNavigator && 'vibrate' in navigator;
77

88
function useVibrate(enabled: boolean = true, pattern: VibrationPattern = [1000, 1000], loop: boolean = true): void {
99
useEffect(() => {

tests/setupTests.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import 'jest-localstorage-mock';
2+
import { isBrowser } from '../src/misc/util';
23

3-
(window as any).ResizeObserver = class ResizeObserver {
4-
observe() {}
5-
disconnect() {}
6-
};
4+
if (isBrowser) {
5+
(window as any).ResizeObserver = class ResizeObserver {
6+
observe() {
7+
}
8+
9+
disconnect() {
10+
}
11+
};
12+
}

tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"lib",
2828
"esm",
2929
"tests",
30-
"stories"
30+
"stories",
31+
"jest.config.ts",
32+
"jest.config.*.ts"
3133
]
3234
}

0 commit comments

Comments
 (0)