Skip to content

Commit

Permalink
feat(testing): Adds testing module for propagation of an attribute to…
Browse files Browse the repository at this point in the history
… a components overlay.
  • Loading branch information
rowaaudil authored and Yngrid Coello committed Nov 9, 2020
1 parent 2b8a61c commit 8d36d39
Show file tree
Hide file tree
Showing 33 changed files with 484 additions and 22 deletions.
5 changes: 5 additions & 0 deletions apps/dev/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ import { ToggleButtonGroupDemo } from './toggle-button-group/toggle-button-group
import { TopBarNavigationDemo } from './top-bar-navigation/top-bar-navigation-demo.component';
import { TreeTableDemo } from './tree-table/tree-table-demo.component';
import { DtIconModule } from '@dynatrace/barista-components/icon';
import {
DT_UI_TEST_CONFIG,
DT_DEFAULT_UI_TEST_CONFIG,
} from '@dynatrace/barista-components/core';

// tslint:disable-next-line: use-component-selector
@Component({ template: '' })
Expand Down Expand Up @@ -164,6 +168,7 @@ export class NoopRouteComponent {}
providers: [
Location,
{ provide: LocationStrategy, useClass: PathLocationStrategy },
{ provide: DT_UI_TEST_CONFIG, useValue: DT_DEFAULT_UI_TEST_CONFIG },
{ provide: STEPPER_GLOBAL_OPTIONS, useValue: { showError: true } },
],
})
Expand Down
1 change: 1 addition & 0 deletions apps/dev/src/autocomplete/autocomplete-demo.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[dtAutocomplete]="auto"
[(ngModel)]="value"
placeholder="Start typing"
dt-ui-test-id="autocomplete"
/>
<dt-autocomplete #auto="dtAutocomplete">
<dt-option *ngFor="let option of options" [value]="option">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
aria-label="Open context dialog"
aria-label-close-button="Close context dialog"
[overlayPanelClass]="panel"
dt-ui-test-id="context-dialog"
>
<button dt-button>First button</button>
<button dt-button variant="secondary">Second button</button>
Expand Down
3 changes: 2 additions & 1 deletion apps/dev/src/devapp.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<dt-sidenav #sidenav mode="side" opened>
<dt-sidenav-header>
<div class="sidenav-title">
Barista dev app
<dt-icon class="sidenav-title-image" name="dynatrace"></dt-icon>
Dynatrace
</div>
</dt-sidenav-header>
<dt-menu *ngFor="let navItem of filteredNavItems">
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/src/select/select-demo.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<dt-select placeholder="Choose your coffee">
<dt-select placeholder="Choose your coffee" dt-ui-test-id="select">
<dt-option value="ThePerfectPour">
ThePerfectPour with some very long text even longer longlonglong
</dt-option>
Expand Down
1 change: 1 addition & 0 deletions apps/dev/src/tag/tag-demo.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<dt-tag-add
placeholder="insert tag here"
(tagAdded)="addTag($event)"
dt-ui-test-id="tag-add"
></dt-tag-add>
</dt-tag-list>
</div>
13 changes: 12 additions & 1 deletion components/autocomplete/src/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ import {
_readKeyCode,
stringify,
DtFlexibleConnectedPositionStrategy,
dtSetUiTestAttribute,
DT_UI_TEST_CONFIG,
DtUiTestConfiguration,
} from '@dynatrace/barista-components/core';
import { DtFormField } from '@dynatrace/barista-components/form-field';

Expand Down Expand Up @@ -269,6 +272,9 @@ export class DtAutocompleteTrigger<T>
@Optional() private _platform?: Platform,
/** @breaking-change 7.0.0 Make the _overlayContainer non optional. */
@Optional() private _overlayContainer?: OverlayContainer,
@Optional()
@Inject(DT_UI_TEST_CONFIG)
private _config?: DtUiTestConfiguration,
) {
// tslint:disable-next-line:strict-type-predicates
if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -433,7 +439,12 @@ export class DtAutocompleteTrigger<T>

if (!this._overlayRef) {
this._overlayRef = this._overlay.create(this._getOverlayConfig());

dtSetUiTestAttribute(
this._overlayRef.overlayElement,
this._overlayRef.overlayElement.id,
this._element,
this._config,
);
this._overlayRef.keydownEvents().subscribe(event => {
const keyCode = _readKeyCode(event);
// Close when pressing ESCAPE or ALT + UP_ARROW, based on the a11y guidelines.
Expand Down
46 changes: 46 additions & 0 deletions components/autocomplete/src/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ import {
import {
DtOption,
DtOptionSelectionChange,
DT_UI_TEST_CONFIG,
DtUiTestConfiguration,
} from '@dynatrace/barista-components/core';
import {
DtFormField,
Expand All @@ -82,6 +84,13 @@ describe('DtAutocomplete', () => {
let overlayContainerElement: HTMLElement;
let zone: MockNgZone;

const overlayConfig: DtUiTestConfiguration = {
attributeName: 'dt-ui-test-id',
constructOverlayAttributeValue(attributeName: string): string {
return `${attributeName}-overlay`;
},
};

// Creates a test component fixture.
function createComponent<T>(
component: Type<T>,
Expand All @@ -99,6 +108,7 @@ describe('DtAutocomplete', () => {
declarations: [component],
providers: [
{ provide: NgZone, useFactory: () => (zone = new MockNgZone()) },
{ provide: DT_UI_TEST_CONFIG, useValue: overlayConfig },
...providers,
],
});
Expand Down Expand Up @@ -1568,6 +1578,22 @@ describe('DtAutocomplete', () => {
expect(panel.classList).toContain('class-two');
}));
});
describe('propagate attribute to overlay', () => {
it('should propagate attribute to overlay when `dt-ui-test-id` is provided', () => {
const fixture: ComponentFixture<PropagateAttribute> = createComponent(
PropagateAttribute,
);
fixture.detectChanges();
const trigger = fixture.componentInstance.trigger;
trigger.openPanel();
const overlay = overlayContainerElement.querySelector(
'.dt-autocomplete-panel',
)!.parentElement;
expect(overlay!.outerHTML).toContain(
'dt-ui-test-id="autocomplete-overlay"',
);
});
});
});

@Component({
Expand Down Expand Up @@ -1888,3 +1914,23 @@ class DynamicallyChangingAutocomplete {
@ViewChild(DtAutocompleteTrigger, { static: false })
trigger: DtAutocompleteTrigger<any>;
}

@Component({
template: `
<input
#input
dt-ui-test-id="autocomplete"
class="test"
type="number"
dtInput
[dtAutocomplete]="auto"
/>
<dt-autocomplete #auto>
<dt-option [value]="0">First</dt-option>
</dt-autocomplete>
`,
})
class PropagateAttribute {
@ViewChild(DtAutocompleteTrigger, { static: false })
trigger: DtAutocompleteTrigger<any>;
}
27 changes: 26 additions & 1 deletion components/chart/src/tooltip/chart-tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,23 @@ import { DtThemingModule } from '@dynatrace/barista-components/theming';
import { createComponent } from '@dynatrace/barista-components/testing';
import { MockIntersectionObserver } from '@dynatrace/barista-components/testing/mock';
import { DtChartTooltipData } from '../highcharts/highcharts-tooltip-types';
import {
DT_UI_TEST_CONFIG,
DtUiTestConfiguration,
} from '@dynatrace/barista-components/core';

describe('DtChartTooltip', () => {
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let chartComponent: DtChart;
let fixture: ComponentFixture<ChartTest>;
const mockIntersectionObserver = new MockIntersectionObserver();
const overlayConfig: DtUiTestConfiguration = {
attributeName: 'dt-ui-test-id',
constructOverlayAttributeValue(attributeName: string): string {
return `${attributeName}-overlay`;
},
};

beforeEach(async(() => {
TestBed.configureTestingModule({
Expand All @@ -62,6 +72,7 @@ describe('DtChartTooltip', () => {
NoopAnimationsModule,
],
declarations: [ChartTest],
providers: [{ provide: DT_UI_TEST_CONFIG, useValue: overlayConfig }],
});

TestBed.compileComponents();
Expand Down Expand Up @@ -163,13 +174,27 @@ describe('DtChartTooltip', () => {
expect(overlayContainerElement.textContent).toContain('54321');
});
});
describe('propagate attribute to overlay', () => {
it('should propagate attribute to overlay when `dt-ui-test-id` is provided', fakeAsync(() => {
mockIntersectionObserver.mockAllIsIntersecting(true);
chartComponent._highChartsTooltipOpened$.next({
data: DUMMY_TOOLTIP_DATA_LINE_SERIES,
});
fixture.detectChanges();
tick();
flush();
expect(overlayContainerElement.innerHTML).toContain(
'dt-ui-test-id="tooltip-overlay',
);
}));
});
});

@Component({
selector: 'dt-line',
template: `
<dt-chart [series]="series" [options]="options">
<dt-chart-tooltip>
<dt-chart-tooltip dt-ui-test-id="tooltip">
<ng-template let-series>
<dt-key-value-list style="min-width: 100px">
<dt-key-value-list-item *ngFor="let data of series.points">
Expand Down
20 changes: 19 additions & 1 deletion components/chart/src/tooltip/chart-tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ import {
TemplateRef,
ViewContainerRef,
ViewEncapsulation,
ElementRef,
Inject,
Optional,
} from '@angular/core';
import { Subject, Subscription } from 'rxjs';

import { isDefined } from '@dynatrace/barista-components/core';
import {
isDefined,
DT_UI_TEST_CONFIG,
DtUiTestConfiguration,
dtSetUiTestAttribute,
} from '@dynatrace/barista-components/core';

import { DtChart } from '../chart';
import { DtChartTooltipData } from '../highcharts/highcharts-tooltip-types';
Expand Down Expand Up @@ -86,6 +94,10 @@ export class DtChartTooltip implements OnDestroy {
private _overlay: Overlay,
private _viewContainerRef: ViewContainerRef,
private _changeDetectorRef: ChangeDetectorRef,
private _elementRef: ElementRef<HTMLElement>,
@Optional()
@Inject(DT_UI_TEST_CONFIG)
private _config?: DtUiTestConfiguration,
) {}

ngOnDestroy(): void {
Expand Down Expand Up @@ -133,6 +145,12 @@ export class DtChartTooltip implements OnDestroy {
overlayRef.attach(this._portal);

this._overlayRef = overlayRef;
dtSetUiTestAttribute(
this._overlayRef.overlayElement,
this._overlayRef.overlayElement.id,
this._elementRef,
this._config,
);
}
}

Expand Down
29 changes: 27 additions & 2 deletions components/confirmation-dialog/src/confirmation-dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,27 @@ import {
} from './confirmation-dialog-constants';
import { DtConfirmationDialogModule } from './confirmation-dialog-module';
import { DtConfirmationDialog } from './confirmation-dialog';
import {
DT_UI_TEST_CONFIG,
DtUiTestConfiguration,
} from '@dynatrace/barista-components/core';

describe('ConfirmationDialogComponent', () => {
const UP = 'translateY(0)';
const DOWN = 'translateY(100%)';

let overlayContainerElement: HTMLElement;
const overlayConfig: DtUiTestConfiguration = {
attributeName: 'dt-ui-test-id',
constructOverlayAttributeValue(attributeName: string): string {
return `${attributeName}-overlay`;
},
};

beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [NoopAnimationsModule, DtConfirmationDialogModule],
providers: [],
providers: [{ provide: DT_UI_TEST_CONFIG, useValue: overlayConfig }],
declarations: [TestComponent, DynamicStates, TwoDialogsComponent],
}).compileComponents();
}));
Expand Down Expand Up @@ -210,6 +220,17 @@ describe('ConfirmationDialogComponent', () => {
expect(dialog2State.textContent!.trim()).toEqual('state2');
}));
});
describe('propagate attribute to overlay', () => {
it('should propagate attribute to overlay when `dt-ui-test-id` is provided', fakeAsync(() => {
const fixture = TestBed.createComponent(TestComponent);
fixture.componentInstance.testState = 'missingState';
fixture.detectChanges();
tick();
expect(overlayContainerElement.innerHTML).toContain(
'dt-ui-test-id="confirmation-dialog-overlay',
);
}));
});
});

function testTextContent(
Expand Down Expand Up @@ -242,7 +263,11 @@ function getState(oc: HTMLElement, name: string): HTMLElement {
@Component({
selector: 'dt-test-component',
template: `
<dt-confirmation-dialog [state]="testState" [showBackdrop]="showBackdrop">
<dt-confirmation-dialog
dt-ui-test-id="confirmation-dialog"
[state]="testState"
[showBackdrop]="showBackdrop"
>
<dt-confirmation-dialog-state name="state1">
state1
</dt-confirmation-dialog-state>
Expand Down
21 changes: 20 additions & 1 deletion components/confirmation-dialog/src/confirmation-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ import {
ViewContainerRef,
ViewEncapsulation,
isDevMode,
Optional,
Inject,
ElementRef,
} from '@angular/core';
import { Subscription } from 'rxjs';

import { DtLogger, DtLoggerFactory } from '@dynatrace/barista-components/core';
import {
DtLogger,
DtLoggerFactory,
DtUiTestConfiguration,
dtSetUiTestAttribute,
DT_UI_TEST_CONFIG,
} from '@dynatrace/barista-components/core';

import {
DT_CONFIRMATION_BACKDROP_ACTIVE_OPACITY,
Expand Down Expand Up @@ -136,6 +145,10 @@ export class DtConfirmationDialog
private _overlay: Overlay,
private _viewContainerRef: ViewContainerRef,
private _changeDetectorRef: ChangeDetectorRef,
private _elementRef: ElementRef<HTMLElement>,
@Optional()
@Inject(DT_UI_TEST_CONFIG)
private _config?: DtUiTestConfiguration,
) {}

ngAfterContentChecked(): void {
Expand Down Expand Up @@ -236,6 +249,12 @@ export class DtConfirmationDialog
},
positionStrategy,
});
dtSetUiTestAttribute(
overlayRef.overlayElement,
overlayRef.overlayElement.id,
this._elementRef,
this._config,
);
const containerPortal = new TemplatePortal<{}>(
this._templateRef,
this._viewContainerRef,
Expand Down
Loading

0 comments on commit 8d36d39

Please sign in to comment.