UNPKG

@rayners/foundry-test-utils

Version:

Shared testing utilities and mocks for FoundryVTT modules

65 lines (64 loc) 1.91 kB
/** * Common test setup for FoundryVTT modules * * This file sets up the basic environment needed for testing Foundry modules. * Import this in your vitest.config.js setupFiles array. */ /// <reference types="@rayners/foundry-dev-tools/types" /> import { vi, beforeEach } from 'vitest'; import '../mocks/foundry-mocks.js'; // Mock jQuery if not already available const g = globalThis; if (typeof g.$ === 'undefined') { g.$ = vi.fn(() => ({ ready: vi.fn(), on: vi.fn(), off: vi.fn(), find: vi.fn(() => ({ length: 0 })), append: vi.fn(), remove: vi.fn(), addClass: vi.fn(), removeClass: vi.fn(), hasClass: vi.fn(() => false), attr: vi.fn(), prop: vi.fn(), val: vi.fn(), text: vi.fn(), html: vi.fn(), css: vi.fn(), hide: vi.fn(), show: vi.fn(), trigger: vi.fn(), click: vi.fn(), submit: vi.fn() })); g.jQuery = g.$; } // Setup DOM environment beforeEach(() => { // Clear any existing DOM document.body.innerHTML = ''; // Reset all mocks vi.clearAllMocks(); // Ensure game object is properly reset if (g.game) { // Reset collections Object.keys(g.game).forEach((key) => { if (g.game[key] && typeof g.game[key] === 'object' && g.game[key].clear) { g.game[key].clear(); } }); } }); // Common test utilities export const createMockElement = (tagName = 'div', attributes = {}) => { const element = document.createElement(tagName); Object.entries(attributes).forEach(([key, value]) => { element.setAttribute(key, value); }); return element; }; export const createMockEvent = (type, options = {}) => { return new Event(type, options); }; export const waitForNextTick = () => new Promise(resolve => setTimeout(resolve, 0));