houser-js-utils
Version:
A comprehensive collection of TypeScript utility functions for common development tasks including array manipulation, string processing, date handling, random number generation, validation, and much more.
181 lines (180 loc) • 4.82 kB
JavaScript
const TestUtils = {
/**
* Creates a test animation event
* @param type - Event type
* @param options - Event options
* @returns Test animation event
*/
createTestAnimationEvent(type, options = {}) {
return new AnimationEvent(type, options);
},
/**
* Creates a test clipboard event
* @param type - Event type
* @param options - Event options
* @returns Test clipboard event
*/
createTestClipboardEvent(type, options = {}) {
return new ClipboardEvent(type, options);
},
/**
* Creates a test data array generator
* @param template - Template object
* @returns Function that generates an array of test data
*/
createTestDataArrayGenerator(template) {
return (count, overrides = {}) => {
return Array.from({ length: count }, () => ({
...template,
...overrides
}));
};
},
/**
* Creates a test data generator
* @param template - Template object
* @returns Function that generates test data
*/
createTestDataGenerator(template) {
return (overrides = {}) => ({
...template,
...overrides
});
},
/**
* Creates a test drag event
* @param type - Event type
* @param options - Event options
* @returns Test drag event
*/
createTestDragEvent(type, options = {}) {
return new DragEvent(type, options);
},
/**
* Creates a test event
* @param type - Event type
* @param options - Event options
* @returns Test event
*/
createTestEvent(type, options = {}) {
return new Event(type, options);
},
/**
* Creates a test focus event
* @param type - Event type
* @param options - Event options
* @returns Test focus event
*/
createTestFocusEvent(type, options = {}) {
return new FocusEvent(type, options);
},
/**
* Creates a test input event
* @param type - Event type
* @param options - Event options
* @returns Test input event
*/
createTestInputEvent(type, options = {}) {
return new InputEvent(type, options);
},
/**
* Creates a test keyboard event
* @param type - Event type
* @param options - Event options
* @returns Test keyboard event
*/
createTestKeyboardEvent(type, options = {}) {
return new KeyboardEvent(type, options);
},
/**
* Creates a test mouse event
* @param type - Event type
* @param options - Event options
* @returns Test mouse event
*/
createTestMouseEvent(type, options = {}) {
return new MouseEvent(type, options);
},
/**
* Creates a test touch event
* @param type - Event type
* @param options - Event options
* @returns Test touch event
*/
createTestTouchEvent(type, options = {}) {
return new TouchEvent(type, options);
},
/**
* Creates a test transition event
* @param type - Event type
* @param options - Event options
* @returns Test transition event
*/
createTestTransitionEvent(type, options = {}) {
return new TransitionEvent(type, options);
},
/**
* Creates a mock error promise
* @param error - Error to reject with
* @param delay - Delay in milliseconds
* @returns Mock error promise
*/
createMockErrorPromise(error, delay = 0) {
return new Promise((_, reject) => setTimeout(() => reject(error), delay));
},
/**
* Creates a mock function
* @param implementation - Optional implementation function
* @returns Mock function
*/
createMock(implementation) {
return implementation || (() => {
});
},
/**
* Creates a mock object
* @param template - Template object to mock
* @returns Mock object
*/
createMockObject(template) {
return template;
},
/**
* Creates a mock promise
* @param value - Value to resolve with
* @param delay - Delay in milliseconds
* @returns Mock promise
*/
createMockPromise(value, delay = 0) {
return new Promise((resolve) => setTimeout(() => resolve(value), delay));
},
/**
* Waits for a number of milliseconds
* @param ms - Milliseconds to wait
* @returns Promise that resolves after the delay
*/
async wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
},
/**
* Waits for a condition to be true
* @param condition - Function that returns a boolean
* @param timeout - Timeout in milliseconds
* @param interval - Check interval in milliseconds
* @returns Promise that resolves when condition is true
*/
async waitFor(condition, timeout = 5e3, interval = 100) {
const start = Date.now();
while (Date.now() - start < timeout) {
if (condition()) {
return;
}
await new Promise((resolve) => setTimeout(resolve, interval));
}
throw new Error("Timeout waiting for condition");
}
};
export {
TestUtils
};
//# sourceMappingURL=TestUtils.mjs.map