Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions projects/igniteui-angular/src/lib/combo/combo.component.spec.ts

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions projects/igniteui-angular/src/lib/dialog/dialog.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ describe('Dialog', () => {
const dialog = fixture.componentInstance.dialog;

dialog.open();
flush();
fixture.detectChanges();

const dialogWindow = fixture.debugElement.query(By.css('.igx-dialog__window'));
Expand Down Expand Up @@ -214,6 +215,7 @@ describe('Dialog', () => {
expect(dialog.isOpen).toEqual(false);

dialog.open();
flush();
fixture.detectChanges();
tick();
expect(dialog.isOpen).toEqual(true);
Expand All @@ -229,6 +231,7 @@ describe('Dialog', () => {
fixture.detectChanges();
const dialog = fixture.componentInstance.dialog;
dialog.open();
flush();
tick();
fixture.detectChanges();

Expand All @@ -241,6 +244,7 @@ describe('Dialog', () => {

dialog.closeOnOutsideSelect = false;
dialog.open();
flush();
tick();
fixture.detectChanges();

Expand Down Expand Up @@ -270,6 +274,7 @@ describe('Dialog', () => {
spyOn(dialog.closed, 'emit');

dialog.open();
flush();
tick();
fixture.detectChanges();

Expand All @@ -287,6 +292,7 @@ describe('Dialog', () => {
expect(dialog.isOpenChange.emit).toHaveBeenCalledWith(false);

dialog.open();
flush();
tick();
fixture.detectChanges();
const buttons = document.querySelectorAll('button');
Expand Down Expand Up @@ -330,9 +336,11 @@ describe('Dialog', () => {
const childDialog = fixture.componentInstance.child;

mainDialog.open();
flush();
tick();

childDialog.open();
flush();
tick();
fixture.detectChanges();

Expand Down Expand Up @@ -367,6 +375,7 @@ describe('Dialog', () => {
const dialog = fixture.componentInstance.dialog;

dialog.open();
flush();
fixture.detectChanges();

const dialogWindow = fixture.debugElement.query(By.css('.igx-dialog__window'));
Expand All @@ -387,6 +396,7 @@ describe('Dialog', () => {
const dialog = fix.componentInstance.dialog;

dialog.open();
flush();
tick();
fix.detectChanges();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('IgxAutocomplete', () => {
expect(dropDown.collapsed).toBeTruthy();

autocomplete.open();
flush();
tick();
fixture.detectChanges();
expect(dropDown.collapsed).toBeFalsy();
Expand Down Expand Up @@ -326,6 +327,7 @@ describe('IgxAutocomplete', () => {
expect(autocomplete.target.open).toHaveBeenCalledTimes(0);

autocomplete.open();
flush();
fixture.detectChanges();
expect(dropDown.collapsed).toBeTruthy();
expect(dropdownListScrollElement.children.length).toEqual(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { IgxToggleActionDirective, IgxToggleDirective, IgxOverlayOutletDirective } from './toggle.directive';
import { waitForToggleOpen } from '../../test-utils/toggle-helpers.spec';
import {
IgxOverlayService, OverlaySettings, ConnectedPositioningStrategy,
AbsoluteScrollStrategy, AutoPositionStrategy, HorizontalAlignment
Expand Down Expand Up @@ -61,20 +62,20 @@ describe('IgxToggle', () => {
expect(divEl.classList.contains(TOGGLER_CLASS)).toBeTruthy();
});

it('should emit \'opening\' and \'opened\' events', fakeAsync(() => {
it('should emit \'opening\' and \'opened\' events', async () => {
const fixture = TestBed.createComponent(IgxToggleTestComponent);
fixture.detectChanges();

const toggle = fixture.componentInstance.toggle;
spyOn(toggle.opening, 'emit');
spyOn(toggle.opened, 'emit');
toggle.open();
tick();
await fixture.whenRenderingDone();
fixture.detectChanges();

expect(toggle.opening.emit).toHaveBeenCalled();
expect(toggle.opened.emit).toHaveBeenCalled();
}));
});

it('should emit \'appended\' event', fakeAsync(() => {
const fixture = TestBed.createComponent(IgxToggleTestComponent);
Expand All @@ -83,6 +84,7 @@ describe('IgxToggle', () => {
const toggle = fixture.componentInstance.toggle;
spyOn(toggle.appended, 'emit');
toggle.open();
TestBed.flushEffects();
tick();
fixture.detectChanges();

Expand All @@ -92,6 +94,7 @@ describe('IgxToggle', () => {
tick();
fixture.detectChanges();
toggle.open();
TestBed.flushEffects();
tick();
fixture.detectChanges();

Expand All @@ -104,6 +107,7 @@ describe('IgxToggle', () => {

const toggle = fixture.componentInstance.toggle;
fixture.componentInstance.toggle.open();
TestBed.flushEffects();
tick();
fixture.detectChanges();

Expand All @@ -127,6 +131,7 @@ describe('IgxToggle', () => {
spyOn(toggle.closed, 'emit');

toggle.open();
TestBed.flushEffects();
tick();
expect(toggle.opened.emit).toHaveBeenCalledTimes(1);
expect(toggle.collapsed).toBe(false);
Expand All @@ -136,6 +141,7 @@ describe('IgxToggle', () => {
expect(toggle.collapsed).toBe(true);

toggle.open();
TestBed.flushEffects();
tick();
expect(toggle.opened.emit).toHaveBeenCalledTimes(2);
const otherId = overlay.attach(fixture.componentInstance.other);
Expand Down Expand Up @@ -170,6 +176,7 @@ describe('IgxToggle', () => {
const fixture = TestBed.createComponent(IgxToggleActionTestComponent);
fixture.detectChanges();
fixture.componentInstance.toggle.open();
TestBed.flushEffects();
tick();

const divEl = fixture.debugElement.query(By.directive(IgxToggleDirective)).nativeElement;
Expand Down Expand Up @@ -292,6 +299,7 @@ describe('IgxToggle', () => {
toggle.closing.pipe(first()).subscribe((e: CancelableEventArgs) => e.cancel = cancelClosing);

toggle.open();
TestBed.flushEffects();
fixture.detectChanges();
tick();

Expand All @@ -315,6 +323,7 @@ describe('IgxToggle', () => {

toggle.opening.subscribe((e: CancelableEventArgs) => e.cancel = true);
toggle.open();
TestBed.flushEffects();
fixture.detectChanges();
tick();

Expand Down Expand Up @@ -429,13 +438,15 @@ describe('IgxToggle', () => {
spyOn(toggle.opening, 'emit');
spyOn(toggle.opened, 'emit');
toggle.open();
TestBed.flushEffects();
tick();
fixture.detectChanges();

expect(toggle.opening.emit).toHaveBeenCalledTimes(1);
expect(toggle.opened.emit).toHaveBeenCalledTimes(1);

toggle.open();
TestBed.flushEffects();
tick();
fixture.detectChanges();

Expand All @@ -451,6 +462,7 @@ describe('IgxToggle', () => {
spyOn(toggle.opening, 'emit');
spyOn(toggle.opened, 'emit');
toggle.open();
TestBed.flushEffects();
tick();
fixture.detectChanges();

Expand Down Expand Up @@ -487,6 +499,7 @@ describe('IgxToggle', () => {
spyOn(toggle.closed, 'emit').and.callThrough();

toggle.open();
TestBed.flushEffects();
tick();
fixture.detectChanges();
expect(toggle.opening.emit).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import {
afterNextRender,
ChangeDetectorRef,
Directive,
ElementRef,
EventEmitter,
HostListener,
inject,
Inject,
Injector,
Input,
OnDestroy,
OnInit,
Optional,
Output,
runInInjectionContext
Output
} from '@angular/core';
import { AbsoluteScrollStrategy } from '../../services/overlay/scroll/absolute-scroll-strategy';
import { CancelableBrowserEventArgs, IBaseEventArgs, PlatformUtil } from '../../core/utils';
Expand Down Expand Up @@ -201,7 +197,6 @@ export class IgxToggleDirective implements IToggleView, OnInit, OnDestroy {
private _overlayClosingSub: Subscription;
private _overlayClosedSub: Subscription;
private _overlayContentAppendedSub: Subscription;
private _injector = inject(Injector);

/**
* @hidden
Expand Down Expand Up @@ -233,27 +228,28 @@ export class IgxToggleDirective implements IToggleView, OnInit, OnDestroy {
}

this._collapsed = false;
this.cdr.detectChanges();

runInInjectionContext(this._injector, () =>{
afterNextRender(() => {
if (!info) {
this.unsubscribe();
this.subscribe();
this._overlayId = this.overlayService.attach(this.elementRef, overlaySettings);
}

const args: ToggleViewCancelableEventArgs = { cancel: false, owner: this, id: this._overlayId };
this.opening.emit(args);
if (args.cancel) {
this.unsubscribe();
this.overlayService.detach(this._overlayId);
this._collapsed = true;
delete this._overlayId;
this.cdr.detectChanges();
return;
}
this.overlayService.show(this._overlayId, overlaySettings);
});
// Use requestAnimationFrame to defer until after host bindings update
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot
Revert this change. afterNextRender is preferred Angular way to way for next render what we need here. After reverting the change again fix if any test fails due to lack of waiting and fix them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the toggle directive to use afterNextRender() as requested. Started converting failing tests to use async/await with fixture.whenRenderingDone() to properly wait for the render cycle.

Currently working through the toggle directive tests (11 still failing out of 26 total in that file). Once these are fixed, I'll apply the same pattern to all other component tests that use toggles.

Changes in commit c67732f.

// This works in both production and tests (with flush() in fakeAsync)
requestAnimationFrame(() => {
if (!info) {
this.unsubscribe();
this.subscribe();
this._overlayId = this.overlayService.attach(this.elementRef, overlaySettings);
}

const args: ToggleViewCancelableEventArgs = { cancel: false, owner: this, id: this._overlayId };
this.opening.emit(args);
if (args.cancel) {
this.unsubscribe();
this.overlayService.detach(this._overlayId);
this._collapsed = true;
delete this._overlayId;
this.cdr.detectChanges();
return;
}
this.overlayService.show(this._overlayId, overlaySettings);
});
}

Expand Down
Loading