Skip to content

Commit 0313ea0

Browse files
alexdudescuGabriela-David
authored andcommitted
refactor: extract testing utility files
1 parent b712b86 commit 0313ea0

File tree

7 files changed

+104
-166
lines changed

7 files changed

+104
-166
lines changed

projects/angular/testing/src/component-utils/grid-testing-utils.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import {
66
tick,
77
} from '@angular/core/testing';
88
import { By } from '@angular/platform-browser';
9-
import { EventGenerator } from '@uipath/angular/testing';
9+
10+
import { EventGenerator } from '../utilities/event-generator';
11+
import {
12+
FixtureTestingUtils,
13+
IStubEndpoint,
14+
} from '../utilities/fixture-testing-utils';
1015

1116
const selectors = {
1217
grid: 'ui-grid',
@@ -15,7 +20,7 @@ const selectors = {
1520

1621
export class GridUtils<T> {
1722
constructor(
18-
private _utils: IntegrationUtils<T>,
23+
private _utils: FixtureTestingUtils<T>,
1924
) { }
2025

2126
flush = (stub: IStubEndpoint, httpClient: HttpTestingController) => {
@@ -71,9 +76,11 @@ export class GridUtils<T> {
7176
.filter(el => this._utils.getDebugElement('.ui-grid-header-title', el))
7277
.map(el => el.attributes['data-property']);
7378

74-
getHeaderCell = (property: string, debugEl = this._utils.fixture.debugElement) => this._utils.getDebugElement(`.ui-grid-header-cell[data-property="${property}"]`, debugEl);
79+
getHeaderCell = (property: string, debugEl = this._utils.fixture.debugElement) =>
80+
this._utils.getDebugElement(`.ui-grid-header-cell[data-property="${property}"]`, debugEl);
7581

76-
getHeaderTitle = (property: string, debugEl = this._utils.fixture.debugElement) => this.getHeaderCell(property, debugEl).query(By.css('.ui-grid-header-title'));
82+
getHeaderTitle = (property: string, debugEl = this._utils.fixture.debugElement) =>
83+
this.getHeaderCell(property, debugEl).query(By.css('.ui-grid-header-title'));
7784

7885
isSortable = (property: string, debugEl = this._utils.fixture.debugElement) => {
7986
const headerCell = this.getHeaderCell(property, debugEl);
@@ -153,7 +160,7 @@ export class GridUtils<T> {
153160

154161
this.clickRowItem(rowIndex, inlineMenuSelector, {
155162
gridSelector,
156-
debugEl
163+
debugEl,
157164
});
158165
this._utils.fixture.detectChanges();
159166

@@ -172,7 +179,7 @@ export class GridUtils<T> {
172179
debugEl?: DebugElement;
173180
} = {}) => this.getRowItem(rowNumber, selector, {
174181
debugEl,
175-
gridSelector
182+
gridSelector,
176183
}).nativeElement
177184
.dispatchEvent(EventGenerator.click);
178185

@@ -202,7 +209,6 @@ export class GridUtils<T> {
202209
}) => {
203210
const selector = `[data-cy="ui-grid-search-filter-${columnName}"] [role="combobox"]`;
204211
const button = this._utils.fixture.debugElement.query(By.css(selector));
205-
expect(button).toBeTruthy();
206212

207213
button.nativeElement.dispatchEvent(EventGenerator.click);
208214
this._utils.fixture.detectChanges();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './grid-testing-utils';
2+
export * from './suggest-testing-utils';

projects/angular/testing/src/component-utils/suggest-testing-utils.ts

Lines changed: 29 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,40 @@ import { DebugElement } from '@angular/core';
33
import { tick } from '@angular/core/testing';
44
import { By } from '@angular/platform-browser';
55
import { UiSuggestComponent } from '@uipath/angular/components/ui-suggest';
6+
7+
import { EventGenerator } from '../utilities/event-generator';
68
import {
7-
EventGenerator,
8-
Key,
9-
} from '@uipath/angular/testing';
9+
FixtureTestingUtils,
10+
IStubEndpoint,
11+
} from '../utilities/fixture-testing-utils';
12+
import { Key } from '../utilities/key';
13+
14+
export interface ISuggestTestingOptions {
15+
debounce?: number;
16+
}
1017

11-
import { SUGGEST_DEBOUNCE } from './constants';
18+
const DEFAULT_SUGGEST_TESTING_OPTIONS: ISuggestTestingOptions = {
19+
debounce: 300,
20+
};
1221

1322
export class SuggestUtils<T> {
1423
dropdownSelector = '.ui-suggest-dropdown-item-list-container';
1524

1625
constructor(
17-
private _utils: IntegrationUtils<T>,
18-
) { }
26+
private _utils: FixtureTestingUtils<T>,
27+
private _options = DEFAULT_SUGGEST_TESTING_OPTIONS,
28+
) {
29+
}
1930

20-
openAndFlush = (selector: string, httpRequest: Function) => {
31+
openAndFlush = (selector: string, httpRequest: () => void) => {
2132
this._utils.click('.display', this._utils.getDebugElement(selector));
2233
this._utils.fixture.detectChanges();
2334
httpRequest();
2435
this._utils.fixture.detectChanges();
2536
};
2637

2738
// eslint-disable-next-line complexity
28-
searchAndSelect = (selector: string, httpRequest?: Function, searchStr = '', nth = 0, debugEl?: DebugElement) => {
39+
searchAndSelect = (selector: string, httpRequest?: () => void, searchStr = '', nth = 0, debugEl?: DebugElement) => {
2940
const suggest = this._utils.getDebugElement(selector, debugEl);
3041
const multiple = this.isMultiple(selector);
3142
const strategy = this.getFetchStrategy(selector, debugEl);
@@ -66,7 +77,7 @@ export class SuggestUtils<T> {
6677
}
6778

6879
this._utils.setInput('input', searchStr, parentContainer);
69-
tick(SUGGEST_DEBOUNCE);
80+
tick(this._options.debounce);
7081
this._utils.fixture.detectChanges();
7182

7283
if (httpRequest) { httpRequest(); }
@@ -93,7 +104,8 @@ export class SuggestUtils<T> {
93104
getFetchStrategy = (selector: string, debugEl?: DebugElement) => {
94105
const suggest = this._utils.getDebugElement(selector, debugEl);
95106
// maybe add a getter along the setter for fetchStrategy ?
96-
return (suggest.componentInstance as UiSuggestComponent)._fetchStrategy$?.value ?? 'eager';
107+
const fetchStrategyKey = '_fetchStrategy$';
108+
return (suggest.componentInstance as UiSuggestComponent)[fetchStrategyKey]?.value ?? 'eager';
97109
};
98110

99111
selectNthItem = (selector: string, nth = 0, config?: {
@@ -137,9 +149,14 @@ export class SuggestUtils<T> {
137149
return listItem;
138150
};
139151

140-
isMultiple = (selector: string, debugEl?: DebugElement) => !!this._utils.getNativeElement(`${selector} mat-chip-list`, debugEl);
152+
elementContains = (suffix: string) => (selector: string, debugEl?: DebugElement) =>
153+
!!this._utils.getNativeElement(`${selector} ${suffix}`, debugEl);
141154

142-
isOpen = (selector: string, debugEl?: DebugElement) => !!this._utils.getNativeElement(`${selector} [aria-expanded="true"]`, debugEl);
155+
// eslint-disable-next-line @typescript-eslint/member-ordering
156+
isMultiple = this.elementContains('mat-chip-list');
157+
158+
// eslint-disable-next-line @typescript-eslint/member-ordering
159+
isOpen = this.elementContains('[aria-expanded="true"]');
143160

144161
getValue = (selector: string, debugEl = this._utils.fixture.debugElement) => {
145162
if (this.isMultiple(selector)) {
@@ -156,87 +173,3 @@ export class SuggestUtils<T> {
156173
clear = (selector: string) =>
157174
this._utils.getNativeElement(`${selector} [role=button].mat-icon`)?.dispatchEvent(EventGenerator.click);
158175
}
159-
160-
class KVPUtils<T> {
161-
constructor(
162-
private _utils: IntegrationUtils<T>,
163-
) { }
164-
165-
/**
166-
* Creates a new key value pair and populates it with the specified values
167-
*
168-
* @param keySearchText key suggest text to be selected
169-
* @param valueSearchText value suggest text to be selected
170-
*/
171-
addAndPopulateKVPInput = (keySearchText: string, valueSearchText: string, keyHttpRequest?: Function, valueHttpRequest?: Function) => {
172-
this._utils.click('[data-cy=ui-kvp-add-new-entry]');
173-
this._utils.fixture.detectChanges();
174-
175-
this._utils.suggest.searchAndSelect(this._nthKeySuggestSelector(0), keyHttpRequest, keySearchText);
176-
this._utils.fixture.detectChanges();
177-
tick(1000);
178-
this._utils.fixture.detectChanges();
179-
180-
this._utils.suggest.searchAndSelect(this._nthValueSuggestSelector(0), valueHttpRequest, valueSearchText);
181-
this._utils.fixture.detectChanges();
182-
tick(1);
183-
this._utils.fixture.detectChanges();
184-
185-
tick(1000);
186-
this._utils.fixture.detectChanges();
187-
};
188-
189-
/**
190-
* Existing number of key value pairs.
191-
*
192-
* @param debugEl
193-
* @returns
194-
*/
195-
currentKVPCount(debugEl = this._utils.fixture.debugElement) {
196-
const selector = 'ui-key-value-input';
197-
return this._utils.getAllDebugElements(selector, debugEl).length;
198-
}
199-
200-
/**
201-
* Retrive a reference to the nth key suggest.
202-
* Index starts at 1.
203-
*
204-
* @param debugEl
205-
* @returns
206-
*/
207-
nthKeySuggest(index: number, debugEl = this._utils.fixture.debugElement) {
208-
const selector = this._nthKeySuggestSelector(index);
209-
return this._utils.getDebugElement(selector, debugEl);
210-
}
211-
212-
/**
213-
* Retrive a reference to the nth value suggest.
214-
* Index starts at 1.
215-
*
216-
* @param debugEl
217-
* @returns
218-
*/
219-
nthValueSuggest(index: number, debugEl = this._utils.fixture.debugElement) {
220-
const selector = this._nthValueSuggestSelector(index);
221-
return this._utils.getDebugElement(selector, debugEl);
222-
}
223-
224-
/**
225-
* Removes the nth key value pair.
226-
* Index starts at 1.
227-
*
228-
* @param debugEl
229-
* @returns
230-
*/
231-
removeNthKVP(index: number, debugEl = this._utils.fixture.debugElement) {
232-
const selector = this._nthRemoveButtonSelector(index);
233-
this._utils.click(selector, debugEl);
234-
235-
tick(1000);
236-
this._utils.fixture.detectChanges();
237-
}
238-
239-
private _nthKeySuggestSelector(index: number) { return `[data-cy=ui-kvp-input-nr-${index}] [data-cy=ui-kvp-key-suggest]`; }
240-
private _nthValueSuggestSelector(index: number) { return `[data-cy=ui-kvp-input-nr-${index}] [data-cy=ui-kvp-value-suggest]`; }
241-
private _nthRemoveButtonSelector(index: number) { return `[data-cy=ui-kvp-input-nr-${index}] [data-cy=ui-kvp-remove-button]`; }
242-
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './utilities';
2+
export * from './component-utils';

projects/angular/testing/src/utilities/fixture-testing-utils.ts

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import {
99
flush,
1010
} from '@angular/core/testing';
1111
import { By } from '@angular/platform-browser';
12-
import {
13-
EventGenerator,
14-
Key,
15-
} from '@uipath/angular/testing';
12+
13+
import { EventGenerator } from './event-generator';
14+
import { HtmlTestingUtils } from './html-testing-utils';
15+
import { Key } from './key';
1616

1717
export interface IStubEndpoint {
1818
url: string;
1919
response: any;
2020
}
2121

22-
export class IntegrationUtils<T> {
22+
export class FixtureTestingUtils<T> {
2323
get component() {
2424
return this.fixture.componentInstance;
2525
}
2626

27-
uiUtils = new UIUtils(this.fixture.nativeElement);
27+
htmlTestingUtils = new HtmlTestingUtils(this.fixture.nativeElement);
2828

2929
constructor(
3030
public fixture: ComponentFixture<T>,
@@ -57,8 +57,8 @@ export class IntegrationUtils<T> {
5757
return debugElement ? debugElement.componentInstance : null;
5858
};
5959

60-
switchToTab = async (number: number, debugEl = this.fixture.debugElement) => {
61-
const tab = this.getDebugElement(`.mat-tab-label:nth-of-type(${number}) .mat-tab-label-content`, debugEl);
60+
switchToTab = async (tabNumber: number, debugEl = this.fixture.debugElement) => {
61+
const tab = this.getDebugElement(`.mat-tab-label:nth-of-type(${tabNumber}) .mat-tab-label-content`, debugEl);
6262
tab.nativeElement.dispatchEvent(EventGenerator.click);
6363
this.fixture.detectChanges();
6464
await this.fixture.whenRenderingDone();
@@ -103,69 +103,60 @@ export class IntegrationUtils<T> {
103103
};
104104

105105
setInput = (selector: string, value: any, debugEl = this.fixture.debugElement) => {
106-
const input = this.uiUtils.setInput(selector, value, debugEl.nativeElement);
107-
this.fixture.detectChanges();
108-
return input;
109-
};
110-
111-
changeInput = (selector: string, value: any, debugEl = this.fixture.debugElement) => {
112-
const input = this.uiUtils.changeInput(selector, value, debugEl.nativeElement);
106+
const input = this.htmlTestingUtils.setInput(selector, value, debugEl.nativeElement);
113107
this.fixture.detectChanges();
114108
return input;
115109
};
116110

117-
isCheckboxChecked = (selector: string, debugEl = this.fixture.debugElement) =>
111+
elementContainsClass = (className: string) => (selector: string, debugEl = this.fixture.debugElement) =>
118112
this.getDebugElement(selector, debugEl)
119-
.nativeElement
120-
.classList
121-
.contains('mat-checkbox-checked');
113+
.nativeElement
114+
.classList
115+
.contains(className);
122116

123-
isCheckboxIndeterminate = (selector: string, debugEl = this.fixture.debugElement) =>
124-
this.getDebugElement(selector, debugEl)
125-
.nativeElement
126-
.classList
127-
.contains('mat-checkbox-indeterminate');
117+
// eslint-disable-next-line @typescript-eslint/member-ordering
118+
isCheckboxChecked = this.elementContainsClass('mat-checkbox-checked');
119+
120+
// eslint-disable-next-line @typescript-eslint/member-ordering
121+
isCheckboxIndeterminate = this.elementContainsClass('mat-checkbox-indeterminate');
128122

129123
toggleCheckbox = (selector: string, debugEl = this.fixture.debugElement) =>
130-
this.getDebugElement(selector, debugEl)
131-
.query(By.css('.mat-checkbox-label'))
132-
.nativeElement
133-
.dispatchEvent(EventGenerator.click);
124+
this.getDebugElement(selector, debugEl)
125+
.query(By.css('.mat-checkbox-label'))
126+
.nativeElement
127+
.dispatchEvent(EventGenerator.click);
134128

135129
setCheckboxState = (selector: string, state: boolean, debugEl = this.fixture.debugElement) => {
136130
const isChecked = this.isCheckboxChecked(selector, debugEl);
137131
if (
138132
!isChecked && state ||
139133
isChecked && !state
140-
) {
141-
this.toggleCheckbox(selector);
142-
}
143-
};
144-
145-
toggleSlider = (selector: string, debugEl = this.fixture.debugElement) =>
146-
this.uiUtils.toggleSlider(selector, debugEl.nativeElement);
147-
148-
setSliderState(selector: string, state: boolean, debugEl = this.fixture.debugElement) {
149-
const isToggled = this.isToggleChecked(selector, debugEl);
150-
if (
151-
isToggled && !state ||
152-
!isToggled && state
153-
) {
154-
debugEl.query(By.css(`${selector} input[type=checkbox]`)).nativeElement.click();
155-
}
156-
}
157-
158-
isToggleChecked = (selector: string, debugEl = this.fixture.debugElement) =>
159-
this.uiUtils.isToggleChecked(selector, debugEl.nativeElement);
160-
161-
isRadioButtonChecked = (selector: string, debugEl = this.fixture.debugElement) =>
162-
this.getDebugElement(selector, debugEl)
163-
.nativeElement
164-
.classList
165-
.contains('mat-radio-checked');
166-
167-
isRadioGroupDisabled = (selector: string, debugEl = this.fixture.debugElement) =>
168-
this.getDebugElement(selector, debugEl)
134+
) {
135+
this.toggleCheckbox(selector);
136+
}
137+
};
138+
139+
toggleSlider = (selector: string, debugEl = this.fixture.debugElement) =>
140+
this.htmlTestingUtils.toggleSlider(selector, debugEl.nativeElement);
141+
142+
setSliderState(selector: string, state: boolean, debugEl = this.fixture.debugElement) {
143+
const isToggled = this.isToggleChecked(selector, debugEl);
144+
if (
145+
isToggled && !state ||
146+
!isToggled && state
147+
) {
148+
debugEl.query(By.css(`${selector} input[type=checkbox]`)).nativeElement.click();
149+
}
150+
}
151+
152+
isToggleChecked = (selector: string, debugEl = this.fixture.debugElement) =>
153+
this.htmlTestingUtils.isToggleChecked(selector, debugEl.nativeElement);
154+
155+
// eslint-disable-next-line @typescript-eslint/member-ordering
156+
isRadioButtonChecked = this.elementContainsClass('mat-radio-checked');
157+
158+
isRadioGroupDisabled = (selector: string, debugEl = this.fixture.debugElement) =>
159+
this.getDebugElement(selector, debugEl)
169160
.queryAll(By.css('mat-radio-button'))
170161
.every((elem) => this._elementHasClass('mat-radio-button', elem, 'mat-radio-disabled'));
171162

0 commit comments

Comments
 (0)