|
| 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, DEBOUNCE_MS } 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 { faDownload, faPlus, faStop } 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 emptyPage = { content: [] as any[], totalElements: 0, numberOfElements: 0 }; |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + jest.useFakeTimers(); |
| 30 | + |
| 31 | + clientsService = { |
| 32 | + searchByText: jest.fn(() => of(emptyPage)) |
| 33 | + } as any; |
| 34 | + |
| 35 | + const authService = { getCredentials: jest.fn(() => ({ permissions: ['ALL_FUNCTIONS'] })) } as any; |
| 36 | + |
| 37 | + await TestBed.configureTestingModule({ |
| 38 | + imports: [ |
| 39 | + ClientsComponent, |
| 40 | + TranslateModule.forRoot() |
| 41 | + ], |
| 42 | + providers: [ |
| 43 | + { provide: ClientsService, useValue: clientsService }, |
| 44 | + { provide: AuthenticationService, useValue: authService }, |
| 45 | + provideAnimationsAsync(), |
| 46 | + provideRouter([]) |
| 47 | + ] |
| 48 | + }).compileComponents(); |
| 49 | + |
| 50 | + TestBed.inject(FaIconLibrary).addIcons(faDownload, faPlus, faStop); |
| 51 | + |
| 52 | + fixture = TestBed.createComponent(ClientsComponent); |
| 53 | + component = fixture.componentInstance; |
| 54 | + fixture.detectChanges(); |
| 55 | + |
| 56 | + // Reset call count from ngOnInit (preloadClients may trigger a call) |
| 57 | + clientsService.searchByText.mockClear(); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + jest.useRealTimers(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should not search before 500 ms have elapsed', () => { |
| 65 | + component.onSearchInput('amara'); |
| 66 | + jest.advanceTimersByTime(DEBOUNCE_MS - 1); |
| 67 | + expect(clientsService.searchByText).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should search after 500 ms pause', () => { |
| 71 | + component.onSearchInput('amara'); |
| 72 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 73 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 74 | + expect(clientsService.searchByText).toHaveBeenCalledWith('amara', 0, expect.any(Number), '', ''); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should reset the timer on rapid typing and fire only once', () => { |
| 78 | + component.onSearchInput('k'); |
| 79 | + jest.advanceTimersByTime(200); |
| 80 | + component.onSearchInput('ka'); |
| 81 | + jest.advanceTimersByTime(200); |
| 82 | + component.onSearchInput('kwame'); |
| 83 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 84 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 85 | + expect(clientsService.searchByText).toHaveBeenCalledWith('kwame', 0, expect.any(Number), '', ''); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should ignore duplicate consecutive values', () => { |
| 89 | + component.onSearchInput('agaba'); |
| 90 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 91 | + component.onSearchInput('agaba'); |
| 92 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 93 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should search immediately when Enter is pressed', () => { |
| 97 | + component.search('bob'); |
| 98 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 99 | + expect(clientsService.searchByText).toHaveBeenCalledWith('bob', 0, expect.any(Number), '', ''); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should not fire a second request when Enter is pressed while debounce is pending', () => { |
| 103 | + component.onSearchInput('kofi'); |
| 104 | + jest.advanceTimersByTime(200); |
| 105 | + component.search('kofi'); |
| 106 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 107 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 108 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should search correctly with non-ASCII characters', () => { |
| 112 | + component.onSearchInput('مريم'); |
| 113 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 114 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 115 | + expect(clientsService.searchByText).toHaveBeenCalledWith('مريم', 0, expect.any(Number), '', ''); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should not search during IME composition but should search on compositionend', () => { |
| 119 | + const inputEl: HTMLInputElement = fixture.nativeElement.querySelector('input[matInput]'); |
| 120 | + |
| 121 | + inputEl.dispatchEvent(new CompositionEvent('compositionstart')); |
| 122 | + fixture.detectChanges(); |
| 123 | + |
| 124 | + // Partial composition — input fires but should be suppressed |
| 125 | + inputEl.value = 'مر'; |
| 126 | + inputEl.dispatchEvent(new Event('input')); |
| 127 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 128 | + expect(clientsService.searchByText).not.toHaveBeenCalled(); |
| 129 | + |
| 130 | + // Composition ends with final value |
| 131 | + inputEl.value = 'مريم'; |
| 132 | + inputEl.dispatchEvent(new CompositionEvent('compositionend')); |
| 133 | + fixture.detectChanges(); |
| 134 | + |
| 135 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 136 | + expect(clientsService.searchByText).toHaveBeenCalledTimes(1); |
| 137 | + expect(clientsService.searchByText).toHaveBeenCalledWith('مريم', 0, expect.any(Number), '', ''); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should not fire debounced search after component is destroyed', () => { |
| 141 | + component.onSearchInput('carol'); |
| 142 | + component.ngOnDestroy(); |
| 143 | + jest.advanceTimersByTime(DEBOUNCE_MS); |
| 144 | + expect(clientsService.searchByText).not.toHaveBeenCalled(); |
| 145 | + }); |
| 146 | +}); |
0 commit comments