2023-02-17 15:13:28 +02:00
|
|
|
/**
|
|
|
|
* @jest-environment jsdom
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { createEventHandlingListeners, Options } from './createEventHandlingAttrs';
|
|
|
|
import { describe, beforeAll, it, jest, expect } from '@jest/globals';
|
|
|
|
|
|
|
|
describe('createEventHandlingAttrs', () => {
|
|
|
|
let lastMessage: string|undefined = undefined;
|
|
|
|
const postMessageFn = (message: string) => {
|
|
|
|
lastMessage = message;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
lastMessage = undefined;
|
|
|
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not create listeners to handle long-press when long press is disabled', () => {
|
|
|
|
const options: Options = {
|
|
|
|
enableLongPress: false,
|
|
|
|
postMessageSyntax: 'postMessageFn',
|
2023-10-02 16:15:51 +02:00
|
|
|
enableEditPopup: false,
|
2023-02-17 15:13:28 +02:00
|
|
|
};
|
|
|
|
const listeners = createEventHandlingListeners('someresourceid', options, 'postMessage("click")');
|
|
|
|
|
2023-10-02 16:15:51 +02:00
|
|
|
// Should not add touchstart/mouseenter/leave listeners when not creating
|
|
|
|
// an edit popup or long-pressing.
|
2023-02-17 15:13:28 +02:00
|
|
|
expect(listeners.onmouseenter).toBe('');
|
|
|
|
expect(listeners.onmouseleave).toBe('');
|
|
|
|
expect(listeners.ontouchstart).toBe('');
|
|
|
|
expect(listeners.ontouchmove).toBe('');
|
|
|
|
expect(listeners.ontouchend).toBe('');
|
|
|
|
expect(listeners.ontouchcancel).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create click listener for given click action', () => {
|
|
|
|
const options: Options = {
|
|
|
|
enableLongPress: false,
|
|
|
|
postMessageSyntax: 'postMessageFn',
|
2023-10-02 16:15:51 +02:00
|
|
|
enableEditPopup: false,
|
2023-02-17 15:13:28 +02:00
|
|
|
};
|
|
|
|
const clickAction = 'postMessageFn("click")';
|
|
|
|
const listeners = createEventHandlingListeners('someresourceid', options, clickAction);
|
|
|
|
expect(listeners.onclick).toContain(clickAction);
|
|
|
|
|
|
|
|
postMessageFn('test');
|
|
|
|
eval(listeners.onclick);
|
|
|
|
expect(lastMessage).toBe('click');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create ontouch listeners for long press', () => {
|
|
|
|
const options: Options = {
|
|
|
|
enableLongPress: true,
|
|
|
|
postMessageSyntax: 'postMessageFn',
|
2023-10-02 16:15:51 +02:00
|
|
|
enableEditPopup: false,
|
2023-02-17 15:13:28 +02:00
|
|
|
};
|
|
|
|
const clickAction: null|string = null;
|
|
|
|
const listeners = createEventHandlingListeners('resourceidhere', options, clickAction);
|
|
|
|
|
|
|
|
expect(listeners.onclick).toBe('');
|
|
|
|
expect(listeners.ontouchstart).not.toBe('');
|
|
|
|
|
|
|
|
// Clear lastMessage
|
|
|
|
postMessageFn('test');
|
|
|
|
|
|
|
|
eval(listeners.ontouchstart);
|
|
|
|
jest.advanceTimersByTime(1000 * 4);
|
|
|
|
|
|
|
|
expect(lastMessage).toBe('longclick:resourceidhere');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('motion during a long press should cancel the timeout', () => {
|
|
|
|
const options: Options = {
|
|
|
|
enableLongPress: true,
|
|
|
|
postMessageSyntax: 'postMessageFn',
|
2023-10-02 16:15:51 +02:00
|
|
|
enableEditPopup: false,
|
2023-02-17 15:13:28 +02:00
|
|
|
};
|
|
|
|
const listeners = createEventHandlingListeners('id', options, null);
|
|
|
|
|
|
|
|
lastMessage = '';
|
|
|
|
eval(listeners.ontouchstart);
|
|
|
|
jest.advanceTimersByTime(100);
|
|
|
|
eval(listeners.ontouchmove);
|
|
|
|
jest.advanceTimersByTime(1000 * 100);
|
|
|
|
|
|
|
|
// Message handler should not have been called.
|
|
|
|
expect(lastMessage).toBe('');
|
|
|
|
});
|
|
|
|
});
|