|
| 1 | +/** |
| 2 | + * Copyright since 2025 Mifos Initiative |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | + |
| 9 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 10 | +import { of } from 'rxjs'; |
| 11 | +import { ClientsComponent } from './clients.component'; |
| 12 | +import { ClientsService } from './clients.service'; |
| 13 | +import { AuthenticationService } from 'app/core/authentication/authentication.service'; |
| 14 | +import { TranslateModule } from '@ngx-translate/core'; |
| 15 | +import { provideAnimationsAsync } from '@angular/platform-browser/animations/async'; |
| 16 | +import { provideRouter } from '@angular/router'; |
| 17 | +import { FaIconLibrary } from '@fortawesome/angular-fontawesome'; |
| 18 | +import * as solidIcons from '@fortawesome/free-solid-svg-icons'; |
| 19 | +import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals'; |
| 20 | + |
| 21 | +describe('ClientsComponent — debounce search', () => { |
| 22 | + let component: ClientsComponent; |
| 23 | + let fixture: ComponentFixture<ClientsComponent>; |
| 24 | + let clientsService: jest.Mocked<ClientsService>; |
| 25 | + |
| 26 | + const DEBOUNCE_MS = 500; |
| 27 | + const emptyPage = { content: [] as any[], totalElements: 0, numberOfElements: 0 }; |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + jest.useFakeTimers(); |
| 31 | + |
| 32 | + clientsService = { |
| 33 | + searchByText: jest.fn(() => of(emptyPage)) |
| 34 | + } as any; |
| 35 | + |
| 36 | + const authService = { getCredentials: jest.fn(() => ({ permissions: ['ALL_FUNCTIONS'] })) } as any; |
| 37 | + |
| 38 | + await TestBed.configureTestingModule({ |
| 39 | + imports: [ |
| 40 | + ClientsComponent, |
| 41 | + TranslateModule.forRoot() |
| 42 | + ], |
| 43 | + providers: [ |
| 44 | + { provide: ClientsService, useValue: clientsService }, |
| 45 | + { provide: AuthenticationService, useValue: authService }, |
| 46 | + provideAnimationsAsync(), |
| 47 | + provideRouter([]) |
| 48 | + ] |
| 49 | + }).compileComponents(); |
| 50 | + |
| 51 | + const faIconLibrary = TestBed.inject(FaIconLibrary); |
| 52 | + const iconList = Object.keys(solidIcons) |
| 53 | + .filter((key) => key !== 'fas' && key !== 'prefix' && key.startsWith('fa')) |
| 54 | + .map((icon) => (solidIcons as any)[icon]); |
| 55 | + faIconLibrary.addIcons(...iconList); |
| 56 | + |
| 57 | + fixture = TestBed.createComponent(ClientsComponent); |
| 58 | + component = fixture.componentInstance; |
| 59 | + fixture.detectChanges(); |
| 60 | + |
| 61 | + // Reset call count from ngOnInit (preloadClients may trigger a call) |
| 62 | + clientsService.searchByText.mockClear(); |
| 63 | + }); |
| 64 | + |
| 65 | + afterEach(() => { |
| 66 | + jest.useRealTimers(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should not search before 500 ms have elapsed', () => { |
| 70 | + component.onSearchInput('alice'); |
| 71 | + jest.advanceTimersByTime(DEBOUNCE_MS - 1); |
| 72 | + expect(clientsService.searchByText).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should search after 500 ms pause', () => { |
| 76 | + component.onSearchInput('alice'); |
| 77 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 78 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 79 | + expect(clientsService.searchByText).toHaveBeenCalledWith('alice', 0, expect.any(Number), '', ''); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should reset the timer on rapid typing and fire only once', () => { |
| 83 | + component.onSearchInput('a'); |
| 84 | + jest.advanceTimersByTime(200); |
| 85 | + component.onSearchInput('al'); |
| 86 | + jest.advanceTimersByTime(200); |
| 87 | + component.onSearchInput('ali'); |
| 88 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 89 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 90 | + expect(clientsService.searchByText).toHaveBeenCalledWith('ali', 0, expect.any(Number), '', ''); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should ignore duplicate consecutive values', () => { |
| 94 | + component.onSearchInput('alice'); |
| 95 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 96 | + component.onSearchInput('alice'); |
| 97 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 98 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should search immediately when Enter is pressed', () => { |
| 102 | + component.search('bob'); |
| 103 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 104 | + expect(clientsService.searchByText).toHaveBeenCalledWith('bob', 0, expect.any(Number), '', ''); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should not fire a second request when Enter is pressed while debounce is pending', () => { |
| 108 | + component.onSearchInput('dave'); |
| 109 | + jest.advanceTimersByTime(200); |
| 110 | + component.search('dave'); |
| 111 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 112 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 113 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should not fire debounced search after component is destroyed', () => { |
| 117 | + component.onSearchInput('carol'); |
| 118 | + component.ngOnDestroy(); |
| 119 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 120 | + expect(clientsService.searchByText).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments