| import { act } from '@testing-library/react'; |
| import { CallbackType, CancelableTimeout, TimeoutController } from '../../src/animation/timeoutController'; |
|
|
| |
| |
| |
| |
| export class MockTimeoutController implements TimeoutController { |
| private timeouts: Array<{ callback: CallbackType; delay: number | undefined }> = []; |
|
|
| private cancelledFramesCount = 0; |
|
|
| setTimeout(callback: CallbackType, delay?: number): CancelableTimeout { |
| this.timeouts.push({ callback, delay }); |
|
|
| |
| return () => { |
| this.removeTimeout(callback); |
| this.cancelledFramesCount++; |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async triggerNextTimeout(now: number): Promise<void> { |
| if (this.timeouts.length === 0) { |
| throw new Error('No timeouts to trigger'); |
| } |
|
|
| const { callback } = this.timeouts.shift(); |
| await Promise.resolve(); |
| |
| this.removeTimeout(callback); |
| act(() => { |
| callback(now); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async flushAllTimeouts(tickSize: number = 1000): Promise<void> { |
| let time = 0; |
| while (this.timeouts.length > 0) { |
| |
| await this.triggerNextTimeout(tickSize * time++); |
| } |
| } |
|
|
| clear() { |
| this.timeouts = []; |
| this.cancelledFramesCount = 0; |
| } |
|
|
| private removeTimeout(callback: CallbackType) { |
| this.timeouts = this.timeouts.filter(t => t.callback !== callback); |
| } |
|
|
| getCallbacksCount() { |
| return this.timeouts.length; |
| } |
|
|
| getTimeouts() { |
| return this.timeouts.map(t => t.delay); |
| } |
|
|
| getCancelledFramesCount() { |
| return this.cancelledFramesCount; |
| } |
| } |
|
|