Skip to content

Commit 4098f62

Browse files
authored
test: add Angular Material Autocomplete test
1 parent 662d598 commit 4098f62

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

packages/angular-material/test/autocomplete-control.spec.ts

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,17 @@ describe('AutoComplete control Input Event Tests', () => {
289289
zone.runOutsideAngular(() => zone.onStable.emit(null));
290290
fixture.detectChanges();
291291

292-
const options = overlayContainerElement.querySelectorAll(
293-
'mat-option'
294-
) as NodeListOf<HTMLElement>;
295-
options.item(0).click();
296-
tick();
297-
fixture.detectChanges();
298-
299-
expect(spy).toHaveBeenCalled();
300-
const event = spy.calls.mostRecent()
301-
.args[0] as MatAutocompleteSelectedEvent;
302-
303-
expect(event.option.value).toBe('X');
292+
fixture.whenStable().then(() => {
293+
const options = overlayContainerElement?.querySelectorAll(
294+
'mat-option'
295+
) as NodeListOf<HTMLElement>;
296+
(options[1] as HTMLElement).click();
297+
fixture.detectChanges();
298+
tick();
299+
const event = spy.calls.mostRecent()
300+
.args[0] as MatAutocompleteSelectedEvent;
301+
expect(event.option.value).toBe('Y');
302+
});
304303
}));
305304
});
306305
describe('AutoComplete control Error Tests', () => {
@@ -347,3 +346,51 @@ describe('AutoComplete control Error Tests', () => {
347346
).toBe('Hi, this is me, test error!');
348347
});
349348
});
349+
350+
describe('AutoComplete control updateFilter function', () => {
351+
let fixture: ComponentFixture<AutocompleteControlRenderer>;
352+
let component: AutocompleteControlRenderer;
353+
354+
beforeEach(() => {
355+
TestBed.configureTestingModule({
356+
declarations: [componentUT],
357+
imports: imports,
358+
providers: providers,
359+
}).compileComponents();
360+
});
361+
362+
beforeEach(() => {
363+
fixture = TestBed.createComponent(componentUT);
364+
component = fixture.componentInstance;
365+
});
366+
367+
it('should not filter options on ENTER key press', () => {
368+
component.shouldFilter = false;
369+
component.options = ['X', 'Y', 'Z'];
370+
setupMockStore(fixture, { uischema, schema, data });
371+
getJsonFormsService(component).updateCore(
372+
Actions.init(data, schema, uischema)
373+
);
374+
component.ngOnInit();
375+
fixture.detectChanges();
376+
component.updateFilter({ keyCode: 13 });
377+
fixture.detectChanges();
378+
expect(component.shouldFilter).toBe(false);
379+
});
380+
381+
it('should filter options when a key other than ENTER is pressed', () => {
382+
component.shouldFilter = false;
383+
component.options = ['X', 'Y', 'Z'];
384+
setupMockStore(fixture, { uischema, schema, data });
385+
getJsonFormsService(component).updateCore(
386+
Actions.init(data, schema, uischema)
387+
);
388+
component.ngOnInit();
389+
fixture.detectChanges();
390+
391+
component.updateFilter({ keyCode: 65 });
392+
fixture.detectChanges();
393+
394+
expect(component.shouldFilter).toBe(true);
395+
});
396+
});

0 commit comments

Comments
 (0)