Skip to content

Commit 0c65ec8

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

File tree

11 files changed

+220
-283
lines changed

11 files changed

+220
-283
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
IgxOverlayService,
1313
OverlayCancelableEventArgs, OverlayClosingEventArgs, OverlayEventArgs, OverlaySettings
1414
} from '../services/public_api';
15-
import { Component, DebugElement, ElementRef, EventEmitter, QueryList, Renderer2, ViewChild } from '@angular/core';
15+
import { ChangeDetectorRef, Component, DebugElement, ElementRef, EventEmitter, Injector, QueryList, Renderer2, ViewChild } from '@angular/core';
1616
import { By } from '@angular/platform-browser';
1717
import { PickerHeaderOrientation, PickerInteractionMode } from '../date-common/types';
1818
import { DatePart } from '../directives/date-time-editor/date-time-editor.common';
@@ -849,7 +849,19 @@ describe('IgxDatePicker', () => {
849849
},
850850
focus: () => { }
851851
};
852-
datePicker = new IgxDatePickerComponent(elementRef, 'en-US', overlay, mockInjector, renderer2, null, mockCdr);
852+
853+
TestBed.configureTestingModule({
854+
providers: [
855+
{ provide: ElementRef, useValue: elementRef },
856+
{ provide: IgxOverlayService, useValue: overlay },
857+
{ provide: Injector, useValue: mockInjector },
858+
{ provide: Renderer2, useValue: renderer2 },
859+
{ provide: ChangeDetectorRef, useValue: mockCdr },
860+
IgxDatePickerComponent
861+
]
862+
});
863+
864+
datePicker = TestBed.inject(IgxDatePickerComponent);
853865
(datePicker as any).inputGroup = mockInputGroup;
854866
(datePicker as any).inputDirective = mockInputDirective;
855867
(datePicker as any).dateTimeEditor = mockDateEditor;

projects/igniteui-angular/src/lib/directives/date-time-editor/date-time-editor.directive.spec.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IgxDateTimeEditorDirective } from './date-time-editor.directive';
22
import { DatePart } from './date-time-editor.common';
33
import { formatDate, registerLocaleData } from '@angular/common';
4-
import { Component, ViewChild, DebugElement, EventEmitter, Output, SimpleChange, SimpleChanges, DOCUMENT, inject } from '@angular/core';
4+
import { Component, ViewChild, DebugElement, EventEmitter, Output, SimpleChange, SimpleChanges, DOCUMENT, inject, Renderer2, ElementRef } from '@angular/core';
55
import { fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
66
import { FormsModule, UntypedFormGroup, UntypedFormBuilder, ReactiveFormsModule, Validators, NgControl } from '@angular/forms';
77
import { By } from '@angular/platform-browser';
@@ -12,28 +12,48 @@ import { UIInteractions } from '../../test-utils/ui-interactions.spec';
1212
import { ViewEncapsulation } from '@angular/core';
1313
import localeJa from "@angular/common/locales/ja";
1414
import localeBg from "@angular/common/locales/bg";
15+
import { MaskParsingService } from '../mask/mask-parsing.service';
1516

1617
describe('IgxDateTimeEditor', () => {
1718
let dateTimeEditor: IgxDateTimeEditorDirective;
1819
describe('Unit tests', () => {
19-
const maskParsingService = jasmine.createSpyObj('MaskParsingService',
20-
['parseMask', 'restoreValueFromMask', 'parseMaskValue', 'applyMask', 'parseValueFromMask']);
21-
const renderer2 = jasmine.createSpyObj('Renderer2', ['setAttribute']);
20+
let maskParsingService: jasmine.SpyObj<MaskParsingService>;
21+
let renderer2: jasmine.SpyObj<Renderer2>;
2222
let locale = 'en';
23-
let elementRef = { nativeElement: null };
23+
let elementRef: ElementRef;
2424
let inputFormat: string;
2525
let displayFormat: string;
2626
let inputDate: string;
2727
const initializeDateTimeEditor = (_control?: NgControl) => {
2828
// const injector = { get: () => control };
29-
dateTimeEditor = new IgxDateTimeEditorDirective(renderer2, elementRef, maskParsingService, null, DOCUMENT, locale);
29+
dateTimeEditor = TestBed.inject(IgxDateTimeEditorDirective);
3030
dateTimeEditor.inputFormat = inputFormat;
3131
dateTimeEditor.ngOnInit();
3232

3333
const change: SimpleChange = new SimpleChange(undefined, inputFormat, true);
3434
const changes: SimpleChanges = { inputFormat: change };
3535
dateTimeEditor.ngOnChanges(changes);
3636
};
37+
38+
beforeEach(() => {
39+
const mockNativeEl = document.createElement("div");
40+
(mockNativeEl as any).setSelectionRange = () => {};
41+
maskParsingService = jasmine.createSpyObj('MaskParsingService',
42+
['parseMask', 'restoreValueFromMask', 'parseMaskValue', 'applyMask', 'parseValueFromMask']);
43+
renderer2 = jasmine.createSpyObj('Renderer2', ['setAttribute']);
44+
elementRef = { nativeElement: mockNativeEl };
45+
46+
TestBed.configureTestingModule({
47+
providers: [
48+
{ provide: MaskParsingService, useValue: maskParsingService },
49+
{ provide: Renderer2, useValue: renderer2 },
50+
{ provide: ElementRef, useValue: elementRef },
51+
{ provide: DOCUMENT, useValue: document },
52+
{ provide: NgControl, useValue: null },
53+
IgxDateTimeEditorDirective,
54+
]
55+
});
56+
})
3757
describe('Properties & Events', () => {
3858
it('should emit valueChange event on clear()', () => {
3959
inputFormat = 'dd/M/yy';
@@ -446,15 +466,11 @@ describe('IgxDateTimeEditor', () => {
446466
expect(dateTimeEditor.value).toEqual(new Date(2020, 5, 12, 23, 15, 14));
447467

448468
inputFormat = 'dd aa yyyy-MM mm-ss-hh';
449-
inputDate = '12 AM 2020-06 14-15-11';
450-
elementRef = { nativeElement: { value: inputDate } };
451-
initializeDateTimeEditor();
452469

453470
dateTimeEditor.inputFormat = inputFormat;
454471
expect(dateTimeEditor.mask).toEqual('00 LL 0000-00 00-00-00');
455472

456473
dateTimeEditor.value = new Date(2020, 5, 12, 11, 15, 14);
457-
spyOnProperty((dateTimeEditor as any), 'inputValue', 'get').and.returnValue(inputDate);
458474

459475
dateTimeEditor.increment(DatePart.AmPm);
460476
expect(dateTimeEditor.value).toEqual(new Date(2020, 5, 12, 23, 15, 14));

projects/igniteui-angular/src/lib/directives/focus/focus.directive.spec.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Component, DebugElement, ViewChild } from '@angular/core';
1+
import { Component, DebugElement, ElementRef, ViewChild } from '@angular/core';
22
import { TestBed, fakeAsync, tick, waitForAsync } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { IgxFocusDirective } from './focus.directive';
55

6-
import { EditorProvider } from '../../core/edit-provider';
6+
import { EDITOR_PROVIDER, EditorProvider } from '../../core/edit-provider';
77
import { IgxCheckboxComponent } from '../../checkbox/checkbox.component';
88
import { IgxDatePickerComponent } from '../../date-picker/public_api';
99
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
@@ -40,7 +40,7 @@ describe('igxFocus', () => {
4040

4141
const button: DebugElement = fix.debugElement.query(By.css('button'));
4242
const divs = fix.debugElement.queryAll(By.css('div'));
43-
const lastDiv = divs[divs.length - 1 ].nativeElement;
43+
const lastDiv = divs[divs.length - 1].nativeElement;
4444

4545
button.triggerEventHandler('click', null);
4646
tick(16);
@@ -57,21 +57,40 @@ describe('igxFocus', () => {
5757
expect(document.activeElement).toBe(document.body);
5858
}));
5959

60-
it('Should return EditorProvider element to focus', fakeAsync(() => {
61-
const elem = { nativeElement: document.createElement('button') };
60+
it('Should return EditorProvider element to focus', () => {
61+
const elementRef = { nativeElement: document.createElement('button') };
6262
const providerElem = document.createElement('input');
63+
6364
const provider: EditorProvider = {
6465
getEditElement: () => providerElem
65-
};
66-
let directive = new IgxFocusDirective(elem, null);
67-
expect(directive.nativeElement).toBe(elem.nativeElement);
68-
directive = new IgxFocusDirective(elem, []);
69-
expect(directive.nativeElement).toBe(elem.nativeElement);
70-
directive = new IgxFocusDirective(elem, [null]);
71-
expect(directive.nativeElement).toBe(elem.nativeElement);
72-
directive = new IgxFocusDirective(elem, [provider]);
73-
expect(directive.nativeElement).toBe(providerElem);
74-
}));
66+
};
67+
68+
TestBed.configureTestingModule({
69+
providers: [
70+
{ provide: ElementRef, useValue: elementRef },
71+
{ provide: EDITOR_PROVIDER, useValue: [provider] },
72+
IgxFocusDirective
73+
]
74+
});
75+
76+
const directive = TestBed.inject(IgxFocusDirective);
77+
expect(directive.nativeElement).toEqual(providerElem);
78+
});
79+
80+
it('Should fallback to ElementRef.nativeElement if no EDITOR_PROVIDER', () => {
81+
const elementRef = { nativeElement: document.createElement('button') };
82+
83+
TestBed.configureTestingModule({
84+
providers: [
85+
{ provide: ElementRef, useValue: elementRef },
86+
{ provide: EDITOR_PROVIDER, useValue: null },
87+
IgxFocusDirective
88+
]
89+
});
90+
91+
const directivew = TestBed.inject(IgxFocusDirective);
92+
expect(directivew.nativeElement).toBe(elementRef.nativeElement);
93+
});
7594

7695
it('Should correctly focus igx-checkbox, igx-radio, igx-switch and igx-date-picker', fakeAsync(() => {
7796
const fix = TestBed.createComponent(CheckboxPickerComponent);

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.spec.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,24 +1348,7 @@ export class TestIgxForOfDirective<T> extends IgxForOfDirective<T> {
13481348

13491349
public scrStepArray = [];
13501350
public scrTopArray = [];
1351-
constructor() {
1352-
const viewContainer = inject(ViewContainerRef);
1353-
const template = inject<TemplateRef<NgForOfContext<T>>>(TemplateRef);
1354-
const differs = inject(IterableDiffers);
1355-
const changeDet = inject(ChangeDetectorRef);
1356-
const zone = inject(NgZone);
1357-
const syncService = inject(IgxForOfScrollSyncService);
1358-
const platformUtil = inject(PlatformUtil);
1359-
1360-
super(viewContainer, template, differs, changeDet, zone, syncService, platformUtil, document);
13611351

1362-
this.viewContainer = viewContainer;
1363-
this.template = template;
1364-
this.differs = differs;
1365-
this.changeDet = changeDet;
1366-
this.zone = zone;
1367-
this.syncService = syncService;
1368-
}
13691352
public override onScroll(evt) {
13701353
const ind = this.scrTopArray.length - 1;
13711354
const prevScrTop = ind < 0 ? 0 : this.scrTopArray[ind];

projects/igniteui-angular/src/lib/directives/form-control/form-control.directive.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, DebugElement, ViewChild } from '@angular/core';
1+
import { Component, DebugElement, ElementRef, Renderer2, ViewChild } from '@angular/core';
22
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
33
import { FormsModule } from '@angular/forms';
44
import { By } from '@angular/platform-browser';
@@ -17,6 +17,14 @@ describe('IgcFormControlDirective - ', () => {
1717

1818
beforeEach(waitForAsync(() => {
1919
defineComponents(IgcRatingComponent);
20+
21+
TestBed.configureTestingModule({
22+
providers: [
23+
{ provide: ElementRef, useValue: elementRef },
24+
{ provide: Renderer2, useValue: renderer2Mock },
25+
IgcFormControlDirective
26+
]
27+
});
2028
}));
2129

2230
const elementRef = { nativeElement: document.createElement('igc-rating') };
@@ -35,7 +43,7 @@ describe('IgcFormControlDirective - ', () => {
3543
]);
3644

3745
it('should correctly implement interface methods - ControlValueAccessor ', () => {
38-
directive = new IgcFormControlDirective(elementRef, renderer2Mock);
46+
directive = TestBed.inject(IgcFormControlDirective);
3947
directive.registerOnChange(mockNgControl.registerOnChangeCb);
4048
directive.registerOnTouched(mockNgControl.registerOnTouchedCb);
4149

projects/igniteui-angular/src/lib/directives/mask/mask.directive.spec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { FormsModule } from '@angular/forms';
44
import { IgxMaskDirective } from './mask.directive';
55

66
import { UIInteractions } from '../../test-utils/ui-interactions.spec';
7-
import { Replaced } from './mask-parsing.service';
7+
import { MaskParsingService, Replaced } from './mask-parsing.service';
88
import { By } from '@angular/platform-browser';
99
import { IgxInputGroupComponent } from '../../input-group/input-group.component';
1010
import { IgxInputDirective } from '../input/input.directive';
11+
import { PlatformUtil } from '../../core/utils';
1112

1213
describe('igxMask', () => {
1314
// TODO: Refactor tests to reuse components
@@ -609,7 +610,18 @@ describe('igxMaskDirective ControlValueAccessor Unit', () => {
609610

610611
// init
611612
renderer2 = jasmine.createSpyObj('Renderer2', ['setAttribute']);
612-
mask = new IgxMaskDirective({ nativeElement: {} } as any, mockParser, renderer2, platformMock as any);
613+
614+
TestBed.configureTestingModule({
615+
providers: [
616+
{ provide: ElementRef, useValue: { nativeElement: {} } },
617+
{ provide: MaskParsingService, useValue: mockParser },
618+
{ provide: Renderer2, useValue: renderer2 },
619+
{ provide: PlatformUtil, useValue: platformMock },
620+
IgxMaskDirective
621+
]
622+
});
623+
624+
mask = TestBed.inject(IgxMaskDirective);
613625
mask.mask = format;
614626
mask.registerOnChange(mockNgControl.registerOnChangeCb);
615627
mask.registerOnTouched(mockNgControl.registerOnTouchedCb);

0 commit comments

Comments
 (0)