Files
gridpilot.gg/apps/website/lib/blockers/ThrottleBlocker.test.ts
2025-12-18 14:26:17 +01:00

59 lines
1.5 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { ThrottleBlocker } from './ThrottleBlocker';
describe('ThrottleBlocker', () => {
let blocker: ThrottleBlocker;
beforeEach(() => {
vi.useFakeTimers();
blocker = new ThrottleBlocker(100); // 100ms delay
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should allow execution initially', () => {
expect(blocker.canExecute()).toBe(true);
});
it('should block execution immediately after block() is called', () => {
blocker.block();
expect(blocker.canExecute()).toBe(false);
});
it('should allow execution again after the delay has passed', () => {
blocker.block();
expect(blocker.canExecute()).toBe(false);
// Advance time by 100ms
vi.advanceTimersByTime(100);
expect(blocker.canExecute()).toBe(true);
});
it('should handle multiple block calls', () => {
blocker.block();
expect(blocker.canExecute()).toBe(false);
vi.advanceTimersByTime(50);
expect(blocker.canExecute()).toBe(false); // Still blocked
vi.advanceTimersByTime(50);
expect(blocker.canExecute()).toBe(true);
blocker.block(); // Block again
expect(blocker.canExecute()).toBe(false);
vi.advanceTimersByTime(100);
expect(blocker.canExecute()).toBe(true);
});
it('should work with different delay values', () => {
const fastBlocker = new ThrottleBlocker(50);
fastBlocker.block();
expect(fastBlocker.canExecute()).toBe(false);
vi.advanceTimersByTime(50);
expect(fastBlocker.canExecute()).toBe(true);
});
});