Skip to content

Commit 0991fd0

Browse files
committed
refactor(*): change tests to account for new DI
1 parent 0c65ec8 commit 0991fd0

File tree

9 files changed

+226
-265
lines changed

9 files changed

+226
-265
lines changed

projects/igniteui-angular/src/lib/calendar/days-view/days-view.component.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { UIInteractions } from "../../test-utils/ui-interactions.spec";
66
import { CalendarDay } from "../common/model";
77
import { DateRangeDescriptor, DateRangeType } from 'igniteui-webcomponents';
88
import { ScrollDirection } from "../calendar";
9+
import { KeyboardNavigationService } from '../calendar.services';
910

1011
const TODAY = new Date(2024, 6, 12);
1112

@@ -15,6 +16,9 @@ describe("Days View Component", () => {
1516
beforeEach(waitForAsync(() => {
1617
TestBed.configureTestingModule({
1718
imports: [InitDaysViewComponent],
19+
providers: [
20+
KeyboardNavigationService
21+
]
1822
}).compileComponents();
1923
}));
2024

projects/igniteui-angular/src/lib/date-range-picker/date-range-picker.component.spec.ts

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ComponentFixture, TestBed, fakeAsync, tick, waitForAsync, flush } from '@angular/core/testing';
2-
import { Component, OnInit, ViewChild, DebugElement, ChangeDetectionStrategy, inject } from '@angular/core';
2+
import { Component, OnInit, ViewChild, DebugElement, ChangeDetectionStrategy, inject, ApplicationRef, NgZone, DOCUMENT, ChangeDetectorRef, ElementRef } from '@angular/core';
33
import { IgxInputDirective, IgxInputState, IgxLabelDirective, IgxPrefixDirective, IgxSuffixDirective } from '../input-group/public_api';
44
import { PickerInteractionMode } from '../date-common/types';
55
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
@@ -8,13 +8,13 @@ import { By } from '@angular/platform-browser';
88
import { ControlsFunction } from '../test-utils/controls-functions.spec';
99
import { UIInteractions } from '../test-utils/ui-interactions.spec';
1010
import { HelperTestFunctions } from '../test-utils/calendar-helper-utils';
11-
import { CancelableEventArgs } from '../core/utils';
11+
import { CancelableEventArgs, PlatformUtil } from '../core/utils';
1212
import { DateRange, IgxDateRangeSeparatorDirective, IgxDateRangeStartComponent } from './date-range-picker-inputs.common';
1313
import { IgxDateTimeEditorDirective } from '../directives/date-time-editor/public_api';
1414
import { DateRangeType } from '../core/dates';
1515
import { IgxDateRangePickerComponent, IgxDateRangeEndComponent } from './public_api';
1616
import { AutoPositionStrategy, IgxOverlayService } from '../services/public_api';
17-
import { AnimationMetadata, AnimationOptions } from '@angular/animations';
17+
import { AnimationBuilder, AnimationMetadata, AnimationOptions } from '@angular/animations';
1818
import { IgxCalendarComponent, WEEKDAYS } from '../calendar/public_api';
1919
import { Subject } from 'rxjs';
2020
import { AsyncPipe } from '@angular/common';
@@ -25,6 +25,7 @@ import { IgxIconComponent } from '../icon/icon.component';
2525
import { registerLocaleData } from "@angular/common";
2626
import localeJa from "@angular/common/locales/ja";
2727
import localeBg from "@angular/common/locales/bg";
28+
import { KeyboardNavigationService } from '../calendar/calendar.services';
2829

2930
// The number of milliseconds in one day
3031
const DEBOUNCE_TIME = 16;
@@ -48,18 +49,9 @@ const CSS_CLASS_INACTIVE_DATE = 'igx-days-view__date--inactive';
4849
describe('IgxDateRangePicker', () => {
4950
describe('Unit tests: ', () => {
5051
let mockElement: any;
51-
let mockApplicationRef: any;
52-
let mockAnimationBuilder: any;
53-
let mockDocument: any;
54-
let mockNgZone: any;
55-
let mockPlatformUtil: any;
56-
let overlay: IgxOverlayService;
57-
let mockInjector;
5852
let mockCalendar: IgxCalendarComponent;
5953
let mockDaysView: any;
60-
let mockAnimationService: AnimationService;
6154
const elementRef = { nativeElement: null };
62-
const platform = {} as any;
6355
const mockNgControl = jasmine.createSpyObj('NgControl',
6456
['registerOnChangeCb',
6557
'registerOnTouchedCb',
@@ -79,54 +71,21 @@ describe('IgxDateRangePicker', () => {
7971
};
8072
mockElement.parent = mockElement;
8173
mockElement.parentElement = mockElement;
82-
mockApplicationRef = { attachView: (h: any) => { }, detachView: (h: any) => { } };
83-
mockInjector = jasmine.createSpyObj('Injector', {
84-
get: mockNgControl
74+
75+
TestBed.configureTestingModule({
76+
imports: [NoopAnimationsModule],
77+
providers: [
78+
{ provide: ElementRef, useValue: elementRef },
79+
IgxAngularAnimationService,
80+
IgxOverlayService,
81+
IgxCalendarComponent,
82+
KeyboardNavigationService,
83+
ChangeDetectorRef,
84+
IgxDateRangePickerComponent
85+
]
8586
});
86-
mockAnimationBuilder = {
87-
build: (a: AnimationMetadata | AnimationMetadata[]) => ({
88-
create: (e: any, opt?: AnimationOptions) => ({
89-
onDone: (fn: any) => { },
90-
onStart: (fn: any) => { },
91-
onDestroy: (fn: any) => { },
92-
init: () => { },
93-
hasStarted: () => true,
94-
play: () => { },
95-
pause: () => { },
96-
restart: () => { },
97-
finish: () => { },
98-
destroy: () => { },
99-
rest: () => { },
100-
setPosition: (p: any) => { },
101-
getPosition: () => 0,
102-
parentPlayer: {},
103-
totalTime: 0,
104-
beforeDestroy: () => { },
105-
_renderer: {
106-
engine: {
107-
players: [
108-
{}
109-
]
110-
}
111-
}
112-
})
113-
})
114-
};
115-
mockDocument = {
116-
body: mockElement,
117-
defaultView: mockElement,
118-
documentElement: document.documentElement,
119-
createElement: () => mockElement,
120-
appendChild: () => { },
121-
addEventListener: (type: string, listener: (this: HTMLElement, ev: MouseEvent) => any) => { },
122-
removeEventListener: (type: string, listener: (this: HTMLElement, ev: MouseEvent) => any) => { }
123-
};
124-
mockNgZone = {};
125-
mockPlatformUtil = { isIOS: false };
126-
mockAnimationService = new IgxAngularAnimationService(mockAnimationBuilder);
127-
overlay = new IgxOverlayService(
128-
mockApplicationRef, mockDocument, mockNgZone, mockPlatformUtil, mockAnimationService);
129-
mockCalendar = new IgxCalendarComponent(platform, 'en');
87+
88+
mockCalendar = TestBed.inject(IgxCalendarComponent);
13089

13190
mockDaysView = {
13291
focusActiveDate: jasmine.createSpy()
@@ -135,7 +94,7 @@ describe('IgxDateRangePicker', () => {
13594
});
13695
/* eslint-enable @typescript-eslint/no-unused-vars */
13796
it('should set range dates correctly through selectRange method', () => {
138-
const dateRange = new IgxDateRangePickerComponent(elementRef, 'en-US', platform, null, null, null, null);
97+
const dateRange = TestBed.inject(IgxDateRangePickerComponent);
13998
// dateRange.calendar = calendar;
14099
let startDate = new Date(2020, 3, 7);
141100
const endDate = new Date(2020, 6, 27);
@@ -153,7 +112,7 @@ describe('IgxDateRangePicker', () => {
153112
});
154113

155114
it('should emit valueChange on selection', () => {
156-
const dateRange = new IgxDateRangePickerComponent(elementRef, 'en-US', platform, null, null, null, null);
115+
const dateRange = TestBed.inject(IgxDateRangePickerComponent);
157116
// dateRange.calendar = calendar;
158117
spyOn(dateRange.valueChange, 'emit');
159118
let startDate = new Date(2017, 4, 5);
@@ -180,7 +139,7 @@ describe('IgxDateRangePicker', () => {
180139
const rangeUpdate = { start: new Date(2020, 2, 22), end: new Date(2020, 2, 25) };
181140

182141
// init
183-
const dateRangePicker = new IgxDateRangePickerComponent(null, 'en', platform, null, null, null, null);
142+
const dateRangePicker = TestBed.inject(IgxDateRangePickerComponent);
184143
dateRangePicker.registerOnChange(mockNgControl.registerOnChangeCb);
185144
dateRangePicker.registerOnTouched(mockNgControl.registerOnTouchedCb);
186145
spyOn(dateRangePicker as any, 'handleSelection').and.callThrough();
@@ -214,7 +173,7 @@ describe('IgxDateRangePicker', () => {
214173
});
215174

216175
it('should validate correctly minValue and maxValue', () => {
217-
const dateRange = new IgxDateRangePickerComponent(elementRef, 'en-US', platform, mockInjector, null, null, null);
176+
const dateRange = TestBed.inject(IgxDateRangePickerComponent);
218177
dateRange.ngOnInit();
219178

220179
// dateRange.calendar = calendar;
@@ -239,7 +198,7 @@ describe('IgxDateRangePicker', () => {
239198
});
240199

241200
it('should disable calendar dates when min and/or max values as dates are provided', () => {
242-
const dateRange = new IgxDateRangePickerComponent(elementRef, 'en-US', platform, mockInjector, null, overlay);
201+
const dateRange = TestBed.inject(IgxDateRangePickerComponent);
243202
dateRange.ngOnInit();
244203

245204
spyOnProperty((dateRange as any), 'calendar').and.returnValue(mockCalendar);
@@ -255,7 +214,7 @@ describe('IgxDateRangePicker', () => {
255214
});
256215

257216
it('should disable calendar dates when min and/or max values as strings are provided', fakeAsync(() => {
258-
const dateRange = new IgxDateRangePickerComponent(elementRef, 'en', platform, mockInjector, null, null, null);
217+
const dateRange = TestBed.inject(IgxDateRangePickerComponent);
259218
dateRange.ngOnInit();
260219

261220
spyOnProperty((dateRange as any), 'calendar').and.returnValue(mockCalendar);
@@ -687,7 +646,7 @@ describe('IgxDateRangePicker', () => {
687646
.withContext('focus should return to the picker input')
688647
.toBeTrue();
689648
expect(dateRange.isFocused).toBeTrue();
690-
}));
649+
}));
691650

692651
it('should not open calendar with ALT + DOWN ARROW key if disabled is set to true', fakeAsync(() => {
693652
fixture.componentInstance.mode = PickerInteractionMode.DropDown;
@@ -1791,8 +1750,8 @@ export class DateRangeTwoInputsDisabledComponent extends DateRangeDisabledCompon
17911750
export class DateRangeReactiveFormComponent {
17921751
private fb = inject(UntypedFormBuilder);
17931752

1794-
@ViewChild('range', {read: IgxDateRangePickerComponent}) public dateRange: IgxDateRangePickerComponent;
1795-
@ViewChild('twoInputs', {read: IgxDateRangePickerComponent}) public dateRangeWithTwoInputs: IgxDateRangePickerComponent;
1753+
@ViewChild('range', { read: IgxDateRangePickerComponent }) public dateRange: IgxDateRangePickerComponent;
1754+
@ViewChild('twoInputs', { read: IgxDateRangePickerComponent }) public dateRangeWithTwoInputs: IgxDateRangePickerComponent;
17961755

17971756
public form = this.fb.group({
17981757
range: ['', Validators.required],

projects/igniteui-angular/src/lib/grids/grid-navigation.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface IActiveNode {
3333
/** @hidden */
3434
@Injectable()
3535
export class IgxGridNavigationService {
36+
protected platform = inject(PlatformUtil);
3637
public grid: GridType;
3738
public _activeNode: IActiveNode = {} as IActiveNode;
3839
public lastActiveNode: IActiveNode = {} as IActiveNode;
@@ -44,9 +45,7 @@ export class IgxGridNavigationService {
4445

4546
public set activeNode(value: IActiveNode) {
4647
this._activeNode = value;
47-
}
48-
49-
protected platform = inject(PlatformUtil)
48+
}
5049

5150
public handleNavigation(event: KeyboardEvent) {
5251
const key = event.key.toLowerCase();

projects/igniteui-angular/src/lib/grids/grid/grid.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ export class IgxGridComponent extends IgxGridBaseDirective implements GridType,
13461346

13471347
private _setupNavigationService() {
13481348
if (this.hasColumnLayouts) {
1349-
this.navigation = new IgxGridMRLNavigationService();
1349+
this.navigation = this.injector.get(IgxGridMRLNavigationService);
13501350
this.navigation.grid = this;
13511351
}
13521352
}

projects/igniteui-angular/src/lib/grids/grid/grid.multi-row-layout.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ICellPosition } from '../common/events';
1111
import { GridFunctions, GRID_MRL_BLOCK } from '../../test-utils/grid-functions.spec';
1212
import { IgxColumnGroupComponent } from '../columns/column-group.component';
1313
import { IgxColumnComponent } from '../columns/column.component';
14+
import { IgxGridMRLNavigationService } from '../grid-mrl-navigation.service';
1415

1516
const GRID_COL_THEAD_CLASS = '.igx-grid-th';
1617
const GRID_MRL_BLOCK_CLASS = `.${GRID_MRL_BLOCK}`;
@@ -24,6 +25,9 @@ describe('IgxGrid - multi-row-layout #grid', () => {
2425
NoopAnimationsModule,
2526
ColumnLayoutTestComponent,
2627
ColumnLayoutAndGroupsTestComponent
28+
],
29+
providers: [
30+
IgxGridMRLNavigationService
2731
]
2832
}).compileComponents();
2933
}));
@@ -677,7 +681,7 @@ describe('IgxGrid - multi-row-layout #grid', () => {
677681

678682
gridFirstRow = grid.rowList.first;
679683
GridFunctions.verifyLayoutHeadersAreAligned(grid, gridFirstRow);
680-
GridFunctions.verifyDOMMatchesLayoutSettings(grid,gridFirstRow, fixture.componentInstance.colGroups);
684+
GridFunctions.verifyDOMMatchesLayoutSettings(grid, gridFirstRow, fixture.componentInstance.colGroups);
681685
}));
682686

683687
it('should initialize correctly when grid width is in % and no widths are set for columns.', fakeAsync(() => {

0 commit comments

Comments
 (0)