Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit b40a84e

Browse files
test(textfield): Improve test coverage
1 parent 3fde87a commit b40a84e

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

packages/mdc-textfield/foundation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,15 @@ class MDCTextfieldFoundation extends MDCFoundation {
200200
const {LABEL_FLOAT_ABOVE, LABEL_SHAKE} = MDCTextfieldFoundation.cssClasses;
201201
const input = this.getNativeInput_();
202202

203-
this.adapter_.removeClassFromLabel(LABEL_SHAKE);
204-
this.showHelptext_();
205-
206203
if (input.value) {
207204
this.adapter_.addClassToLabel(LABEL_FLOAT_ABOVE);
208205
} else {
209206
this.adapter_.removeClassFromLabel(LABEL_FLOAT_ABOVE);
210207
}
211208

212209
if (!this.useCustomValidityChecking_) {
210+
this.adapter_.removeClassFromLabel(LABEL_SHAKE);
211+
this.showHelptext_();
213212
this.changeValidity_(input.checkValidity());
214213
}
215214
}

test/unit/mdc-textfield/foundation.test.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import {assert} from 'chai';
1818
import bel from 'bel';
19-
import lolex from 'lolex';
2019
import td from 'testdouble';
2120

2221
import {verifyDefaultAdapter} from '../helpers/foundation';
@@ -39,10 +38,10 @@ function setupHookTest() {
3938
return {foundation, mockAdapter, nativeInput};
4039
}
4140

42-
// Shims Object.getOwnPropertyDescriptor for the checkbox's WebIDL attributes. Used to test
41+
// Shims Object.getOwnPropertyDescriptor for the input's WebIDL attributes. Used to test
4342
// the behavior of overridding WebIDL properties in different browser environments. For example,
4443
// in Safari WebIDL attributes don't return get/set in descriptors.
45-
function withMockCheckboxDescriptorReturning(descriptor, runTests) {
44+
function withMockInputDescriptorReturning(descriptor, runTests) {
4645
const originalDesc = Object.getOwnPropertyDescriptor(Object, 'getOwnPropertyDescriptor');
4746
const mockGetOwnPropertyDescriptor = td.func('.getOwnPropertyDescriptor');
4847

@@ -214,7 +213,7 @@ test('#init does not add mdc-textfield__label--float-above class if the input do
214213

215214
test('#init handles case when WebIDL attrs cannot be overridden (Safari)', () => {
216215
const {foundation, nativeInput} = setupHookTest();
217-
withMockCheckboxDescriptorReturning(DESC_UNDEFINED, () => {
216+
withMockInputDescriptorReturning(DESC_UNDEFINED, () => {
218217
assert.doesNotThrow(() => {
219218
foundation.init();
220219
nativeInput.value = nativeInput.value + '_';
@@ -224,14 +223,14 @@ test('#init handles case when WebIDL attrs cannot be overridden (Safari)', () =>
224223

225224
test('#init handles case when property descriptors are not returned at all (Android Browser)', () => {
226225
const {foundation} = setupHookTest();
227-
withMockCheckboxDescriptorReturning(undefined, () => {
226+
withMockInputDescriptorReturning(undefined, () => {
228227
assert.doesNotThrow(() => foundation.init());
229228
});
230229
});
231230

232231
test('#destroy handles case when WebIDL attrs cannot be overridden (Safari)', () => {
233232
const {foundation} = setupHookTest();
234-
withMockCheckboxDescriptorReturning(DESC_UNDEFINED, () => {
233+
withMockInputDescriptorReturning(DESC_UNDEFINED, () => {
235234
assert.doesNotThrow(() => foundation.init(), 'init sanity check');
236235
assert.doesNotThrow(() => foundation.destroy());
237236
});
@@ -543,32 +542,33 @@ test('interacting with text field does not emit custom events if input is disabl
543542
td.verify(mockAdapter.notifyIconAction(), {times: 0});
544543
});
545544

546-
test('"value" property change hook removes mdc-textfield__label--float-above class', () => {
545+
test('"value" property change hook works properly', () => {
547546
const {foundation, mockAdapter, nativeInput} = setupHookTest();
548-
const clock = lolex.install();
547+
let inputValue = '';
549548

550-
withMockCheckboxDescriptorReturning({
551-
get: () => {},
552-
set: () => {},
549+
withMockInputDescriptorReturning({
550+
get: () => inputValue,
551+
set: (value) => inputValue = value,
553552
enumerable: false,
554553
configurable: true,
555554
}, () => {
556-
nativeInput.value = '_';
557555
foundation.init();
556+
557+
nativeInput.value = '_';
558+
td.verify(mockAdapter.addClassToLabel(cssClasses.LABEL_FLOAT_ABOVE));
559+
558560
nativeInput.value = '';
559561
td.verify(mockAdapter.removeClassFromLabel(cssClasses.LABEL_FLOAT_ABOVE));
560562
});
561-
562-
clock.uninstall();
563563
});
564564

565565
test('"value" property change hook does nothing if input is focused', () => {
566566
const {foundation, mockAdapter, nativeInput} = setupHookTest();
567-
const clock = lolex.install();
567+
let inputValue = '';
568568

569-
withMockCheckboxDescriptorReturning({
570-
get: () => {},
571-
set: () => {},
569+
withMockInputDescriptorReturning({
570+
get: () => inputValue,
571+
set: (value) => inputValue = value,
572572
enumerable: false,
573573
configurable: true,
574574
}, () => {
@@ -578,9 +578,12 @@ test('"value" property change hook does nothing if input is focused', () => {
578578
});
579579
foundation.init();
580580
focus();
581+
td.verify(mockAdapter.addClassToLabel(cssClasses.LABEL_FLOAT_ABOVE), {times: 1});
582+
583+
nativeInput.value = '_';
584+
td.verify(mockAdapter.addClassToLabel(cssClasses.LABEL_FLOAT_ABOVE), {times: 1});
585+
581586
nativeInput.value = '';
582587
td.verify(mockAdapter.removeClassFromLabel(cssClasses.LABEL_FLOAT_ABOVE), {times: 0});
583588
});
584-
585-
clock.uninstall();
586589
});

0 commit comments

Comments
 (0)