Skip to content

Commit 796b31a

Browse files
crisbetojosephperrott
authored andcommitted
test(autocomplete): fix failing test (#12587)
1 parent 9bd08e7 commit 796b31a

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,40 +1508,46 @@ describe('MatAutocomplete', () => {
15081508

15091509
it('should fall back to above position when requested if options are added while ' +
15101510
'the panel is open', fakeAsync(() => {
1511-
let fixture = createComponent(AutocompleteWithOnPushDelay);
1511+
let fixture = createComponent(SimpleAutocomplete);
1512+
fixture.componentInstance.states = fixture.componentInstance.states.slice(0, 1);
1513+
fixture.componentInstance.filteredStates = fixture.componentInstance.states.slice();
15121514
fixture.detectChanges();
15131515

15141516
let inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
15151517
let inputReference = fixture.debugElement.query(By.css('.mat-form-field-flex')).nativeElement;
15161518

15171519
// Push the element down so it has a little bit of space, but not enough to render.
1518-
inputReference.style.bottom = '10px';
1520+
inputReference.style.bottom = '75px';
15191521
inputReference.style.position = 'fixed';
15201522

1521-
// Focus the input to load the deferred options.
15221523
dispatchFakeEvent(inputEl, 'focusin');
1523-
tick(1000);
1524-
15251524
fixture.detectChanges();
1526-
tick();
1525+
zone.simulateZoneExit();
1526+
fixture.detectChanges();
15271527

1528-
const inputBottom = inputReference.getBoundingClientRect().bottom;
1529-
const panel = overlayContainerElement.querySelector('.mat-autocomplete-panel')!;
1530-
const panelTop = panel.getBoundingClientRect().top;
1528+
let panel = overlayContainerElement.querySelector('.mat-autocomplete-panel')!;
1529+
let inputRect = inputReference.getBoundingClientRect();
1530+
let panelRect = panel.getBoundingClientRect();
15311531

1532-
expect(Math.floor(inputBottom))
1533-
.toEqual(Math.floor(panelTop),
1532+
expect(Math.floor(panelRect.top))
1533+
.toBe(Math.floor(inputRect.bottom),
15341534
`Expected panel top to be below input before repositioning.`);
15351535

1536+
for (let i = 0; i < 20; i++) {
1537+
fixture.componentInstance.filteredStates.push({code: 'FK', name: 'Fake State'});
1538+
fixture.detectChanges();
1539+
}
1540+
15361541
// Request a position update now that there are too many suggestions to fit in the viewport.
15371542
fixture.componentInstance.trigger.updatePosition();
15381543

1539-
const inputTop = inputReference.getBoundingClientRect().top;
1540-
const panelBottom = panel.getBoundingClientRect().bottom;
1544+
inputRect = inputReference.getBoundingClientRect();
1545+
panelRect = panel.getBoundingClientRect();
15411546

1542-
expect(Math.floor(inputTop))
1543-
.toEqual(Math.floor(panelBottom),
1547+
expect(Math.floor(panelRect.bottom))
1548+
.toBe(Math.floor(inputRect.top),
15441549
`Expected panel to fall back to above position after repositioning.`);
1550+
tick();
15451551
}));
15461552

15471553
it('should not throw if a panel reposition is requested while the panel is closed', () => {

0 commit comments

Comments
 (0)