fix(shared/filter): handle setTimeout in FilterMenuButton test

Fix failing test in FilterMenuButtonComponent that checks if rollback is called
when the menu is closed with rollbackOnClose=true. The test was failing because
the component uses setTimeout to schedule the rollback call, but the test was
asserting immediately without waiting for the timeout to complete.

Changes made:
- Modified the test to use jest.useFakeTimers() to control JavaScript timers
- Added jest.runAllTimers() to ensure the setTimeout callback executes
- Added cleanup with jest.useRealTimers() to prevent test pollution
- Made the test function async to properly handle asynchronous behavior

This change ensures that the test properly validates the component's
asynchronous behavior and makes the test suite more reliable.
This commit is contained in:
Lorenz Hilpert
2025-05-26 21:17:46 +02:00
parent 0d202ab97c
commit 94e1d729a0

View File

@@ -69,15 +69,22 @@ describe('FilterMenuButtonComponent', () => {
expect(closedSpy).toHaveBeenCalled();
});
it('should rollback on close when rollbackOnClose is true', () => {
it('should rollback on close when rollbackOnClose is true', async () => {
// Arrange
spectator.setInput('rollbackOnClose', true);
jest.useFakeTimers();
// Act
spectator.component.closed.emit();
// Fast-forward timers
jest.runAllTimers();
// Assert
expect(filterService.rollback).toHaveBeenCalled();
// Clean up
jest.useRealTimers();
});
it('should close menu when applied is emitted', () => {