Skip to content
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
31 changes: 17 additions & 14 deletions .test/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/* eslint-disable prettier/prettier */
import '@testing-library/jest-dom/extend-expect';
import MutationObserver from 'mutation-observer';

// Global MutationObserver Mocks
window.MutationObserver = MutationObserver;
// React SSR when environment is not js-dom
if (global.window !== undefined) {

// Global Test Environment Mocks
window.matchMedia = jest.fn((query: string): MediaQueryList => ({
media: query,
onchange: null,
matches: false,
addListener: jest.fn(),
dispatchEvent: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn()
}));
// Global MutationObserver Mocks
window.MutationObserver = require('mutation-observer');

// Global Test Environment Mocks
window.matchMedia = jest.fn((query: string): MediaQueryList => ({
media: query,
onchange: null,
matches: false,
addListener: jest.fn(),
dispatchEvent: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn()
}));
}
13 changes: 13 additions & 0 deletions __tests__/ReactSSR.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @jest-environment node
*/

import React from 'react';
import { renderToString } from 'react-dom/server';
import { Select } from '../src';

test('select element can be rendered using react-dom/server', () => {

expect(() => renderToString(<Select />)).not.toThrow();

});
7 changes: 7 additions & 0 deletions src/hooks/useIsTouchDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export const useIsTouchDevice = (): boolean => {
const isTouchDevice = useRef<boolean | null>(null);

if (isTouchDevice.current === null) {
// Check for SSR where navigator and window may not be defined.
// tslint:disable-next-line:no-typeof-undefined
if (typeof navigator === 'undefined' || typeof window === 'undefined') {
isTouchDevice.current = false;
return isTouchDevice.current;
}

isTouchDevice.current = ('ontouchstart' in window || !!navigator.maxTouchPoints);
return isTouchDevice.current;
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function smoothScrollTo(
* Determines if the current browser is IE or Edge (standard/chromium).
*/
export const isEdgeOrIE = (): boolean => IE_EDGE_BROWSER_REGEXP.test(navigator.userAgent);
// tslint:disable-next-line:no-typeof-undefined
export const isEdgeOrIE = (): boolean => typeof navigator !== 'undefined' && IE_EDGE_BROWSER_REGEXP.test(navigator.userAgent);

/**
* Tests object for type of array with a length of at least 1.
Expand Down