2024-06-03 06:04:21 -07:00
|
|
|
import { useDeviceSupport } from 'n8n-design-system/composables/useDeviceSupport';
|
2024-01-10 06:42:01 -08:00
|
|
|
|
|
|
|
describe('useDeviceSupport()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
global.window = Object.create(window);
|
2024-03-14 01:19:28 -07:00
|
|
|
global.navigator = { userAgent: 'test-agent', maxTouchPoints: 0 } as Navigator;
|
2024-01-10 06:42:01 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('isTouchDevice', () => {
|
|
|
|
it('should be true if ontouchstart is in window', () => {
|
|
|
|
Object.defineProperty(window, 'ontouchstart', {});
|
|
|
|
const { isTouchDevice } = useDeviceSupport();
|
|
|
|
expect(isTouchDevice).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be true if navigator.maxTouchPoints > 0', () => {
|
|
|
|
Object.defineProperty(navigator, 'maxTouchPoints', { value: 1 });
|
|
|
|
const { isTouchDevice } = useDeviceSupport();
|
|
|
|
expect(isTouchDevice).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be false if no touch support', () => {
|
|
|
|
delete window.ontouchstart;
|
|
|
|
Object.defineProperty(navigator, 'maxTouchPoints', { value: 0 });
|
|
|
|
const { isTouchDevice } = useDeviceSupport();
|
|
|
|
expect(isTouchDevice).toEqual(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isMacOs', () => {
|
|
|
|
it('should be true for macOS user agent', () => {
|
|
|
|
Object.defineProperty(navigator, 'userAgent', { value: 'macintosh' });
|
|
|
|
const { isMacOs } = useDeviceSupport();
|
|
|
|
expect(isMacOs).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be false for non-macOS user agent', () => {
|
|
|
|
Object.defineProperty(navigator, 'userAgent', { value: 'windows' });
|
|
|
|
const { isMacOs } = useDeviceSupport();
|
|
|
|
expect(isMacOs).toEqual(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('controlKeyCode', () => {
|
|
|
|
it('should return Meta on macOS', () => {
|
|
|
|
Object.defineProperty(navigator, 'userAgent', { value: 'macintosh' });
|
|
|
|
const { controlKeyCode } = useDeviceSupport();
|
|
|
|
expect(controlKeyCode).toEqual('Meta');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return Control on non-macOS', () => {
|
|
|
|
Object.defineProperty(navigator, 'userAgent', { value: 'windows' });
|
|
|
|
const { controlKeyCode } = useDeviceSupport();
|
|
|
|
expect(controlKeyCode).toEqual('Control');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isCtrlKeyPressed()', () => {
|
|
|
|
it('should return true for metaKey press on macOS', () => {
|
|
|
|
Object.defineProperty(navigator, 'userAgent', { value: 'macintosh' });
|
|
|
|
const { isCtrlKeyPressed } = useDeviceSupport();
|
|
|
|
const event = new KeyboardEvent('keydown', { metaKey: true });
|
|
|
|
expect(isCtrlKeyPressed(event)).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true for ctrlKey press on non-macOS', () => {
|
|
|
|
Object.defineProperty(navigator, 'userAgent', { value: 'windows' });
|
|
|
|
const { isCtrlKeyPressed } = useDeviceSupport();
|
|
|
|
const event = new KeyboardEvent('keydown', { ctrlKey: true });
|
|
|
|
expect(isCtrlKeyPressed(event)).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|