Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 23b1098

Browse files
authored
test(text-field): Increase test coverage (#2104)
1 parent 77a1a42 commit 23b1098

3 files changed

Lines changed: 135 additions & 132 deletions

File tree

packages/textfield/text-field.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,10 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
191191
}
192192
set outlined(value: boolean) {
193193
const newValue = coerceBooleanProperty(value);
194-
if (newValue !== this._outlined) {
195-
this._outlined = newValue || (this._defaults && this._defaults.outlined) || false;
196-
if (this._initialized) {
197-
this._layout();
198-
}
199-
}
194+
this._outlined = newValue;
195+
this._layout();
200196
}
201-
private _outlined: boolean = false;
197+
private _outlined = false;
202198

203199
@Input()
204200
get disabled(): boolean {
@@ -207,7 +203,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
207203
set disabled(value: boolean) {
208204
this.setDisabledState(value);
209205
}
210-
private _disabled: boolean = false;
206+
private _disabled = false;
211207

212208
@Input()
213209
get required(): boolean {
@@ -240,7 +236,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
240236
set readonly(value: boolean) {
241237
this._readonly = coerceBooleanProperty(value);
242238
}
243-
private _readonly: boolean = false;
239+
private _readonly = false;
244240

245241
@Input()
246242
get fullwidth(): boolean {
@@ -253,7 +249,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
253249
this.placeholder = this.fullwidth ? this.label : '';
254250
}
255251
}
256-
private _fullwidth: boolean = false;
252+
private _fullwidth = false;
257253

258254
@Input()
259255
get helperText(): MdcHelperText | null {
@@ -294,7 +290,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
294290
this._foundation.setUseNativeValidation(this._useNativeValidation);
295291
}
296292
}
297-
private _useNativeValidation: boolean = true;
293+
private _useNativeValidation = true;
298294

299295
@Input()
300296
get characterCounter(): boolean {
@@ -309,7 +305,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
309305
}
310306
}
311307
}
312-
private _characterCounter: boolean = false;
308+
private _characterCounter = false;
313309

314310
@Input()
315311
get value(): any {
@@ -453,6 +449,8 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
453449

454450
// Force setter to be called in case id was not specified.
455451
this.id = this.id;
452+
453+
this._setDefaultGlobalOptions();
456454
}
457455

458456
ngAfterViewInit(): void {
@@ -487,7 +485,6 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
487485
this._asyncInitFoundation()
488486
.then(() => {
489487
this._initialized = true;
490-
this._setDefaultOptions();
491488

492489
if (!this.fullwidth && !this.outlined && !this.textarea) {
493490
this._ripple = new MdcRipple(this.elementRef);
@@ -629,14 +626,14 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
629626
if (newValue !== this._disabled) {
630627
this._disabled = newValue;
631628
if (this._initialized) {
632-
this._foundation?.setDisabled(newValue);
629+
this._foundation.setDisabled(newValue);
633630
}
634631
this._changeDetectorRef.markForCheck();
635632
}
636633
}
637634

638635
/** Set the default options here. */
639-
private _setDefaultOptions(): void {
636+
private _setDefaultGlobalOptions(): void {
640637
if (this._defaults && this._defaults.outlined) {
641638
this._outlined = this._defaults.outlined;
642639
}

test/select/select.test.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import {dispatchKeyboardEvent, dispatchMouseEvent} from '../testing/dispatch-eve
1515
import {
1616
MdcSelectModule,
1717
MdcSelect,
18-
MDC_SELECT_DEFAULT_OPTIONS,
19-
MdcSelectDefaultOptions
18+
MDC_SELECT_DEFAULT_OPTIONS
2019
} from '@angular-mdc/web';
2120

22-
function configureMdcSelectTestingModule(declarations: any[], providers: Provider[] = []) {
21+
function configureMdcTestingModule(declarations: any[], providers: Provider[] = []) {
2322
TestBed.configureTestingModule({
2423
imports: [
2524
MdcSelectModule,
@@ -35,9 +34,10 @@ function configureMdcSelectTestingModule(declarations: any[], providers: Provide
3534

3635
describe('MdcSelectModule', () => {
3736
let fixture: ComponentFixture<any>;
37+
let platform: {isBrowser: boolean};
3838

3939
beforeEach(async(() => {
40-
configureMdcSelectTestingModule([
40+
configureMdcTestingModule([
4141
SimpleTest,
4242
SelectFormControl,
4343
EnhancedSelect,
@@ -46,6 +46,46 @@ describe('MdcSelectModule', () => {
4646
]);
4747
}));
4848

49+
describe('Tests for SSR', () => {
50+
let testDebugElement: DebugElement;
51+
let testInstance: MdcSelect;
52+
let testComponent: SimpleTest;
53+
54+
beforeEach(() => {
55+
// Set the default Platform override that can be updated before component creation.
56+
platform = {isBrowser: false};
57+
58+
fixture = TestBed.createComponent(SimpleTest);
59+
fixture.detectChanges();
60+
61+
testDebugElement = fixture.debugElement.query(By.directive(MdcSelect));
62+
testInstance = testDebugElement.componentInstance;
63+
testComponent = fixture.debugElement.componentInstance;
64+
});
65+
66+
it('#should have mdc-select by default', () => {
67+
expect(testDebugElement.nativeElement.classList)
68+
.toContain('mdc-select', 'Expected to have mdc-select');
69+
});
70+
71+
it('#should handle mouse events', () => {
72+
testInstance.focus();
73+
dispatchKeyboardEvent(testInstance.elementRef.nativeElement, 'keydown', DOWN_ARROW);
74+
fixture.detectChanges();
75+
});
76+
77+
it('#should handle mouse events', fakeAsync(() => {
78+
dispatchMouseEvent(testInstance._selectedText.root, 'click');
79+
fixture.detectChanges();
80+
testInstance.focus();
81+
dispatchKeyboardEvent(testInstance._selectedText.root, 'keydown', DOWN_ARROW);
82+
dispatchKeyboardEvent(testInstance.elementRef.nativeElement, 'keydown', DOWN_ARROW);
83+
fixture.detectChanges();
84+
85+
flush();
86+
}));
87+
});
88+
4989
describe('basic behaviors', () => {
5090
let testDebugElement: DebugElement;
5191
let testNativeElement: HTMLElement;
@@ -329,7 +369,7 @@ describe('MdcSelectModule', () => {
329369
});
330370

331371
it('should be able to provide default values through an injection token', () => {
332-
configureMdcSelectTestingModule([NgModelSelect], [{
372+
configureMdcTestingModule([NgModelSelect], [{
333373
provide: MDC_SELECT_DEFAULT_OPTIONS,
334374
useValue: {
335375
outlined: true
@@ -342,7 +382,7 @@ it('should be able to provide default values through an injection token', () =>
342382
});
343383

344384
it('should be able to provide default values through an injection token', () => {
345-
configureMdcSelectTestingModule([NgModelSelect], [{
385+
configureMdcTestingModule([NgModelSelect], [{
346386
provide: MDC_SELECT_DEFAULT_OPTIONS,
347387
useValue: {
348388
outlined: null

0 commit comments

Comments
 (0)