From 7261fd0bb5f932a929556695c98a67e114589247 Mon Sep 17 00:00:00 2001 From: Patty RoDee Date: Fri, 5 Apr 2019 15:24:49 -0700 Subject: [PATCH] fix(text-field): Set character counter in setValue (#4572) (cherry picked from commit bce2e639317aa3f84f5e8c211ea99338cf210437) --- packages/mdc-textfield/foundation.ts | 1 + test/unit/mdc-textfield/foundation.test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/packages/mdc-textfield/foundation.ts b/packages/mdc-textfield/foundation.ts index 0756c53fb50..70635afff4b 100644 --- a/packages/mdc-textfield/foundation.ts +++ b/packages/mdc-textfield/foundation.ts @@ -289,6 +289,7 @@ export class MDCTextFieldFoundation extends MDCFoundation { // Prevent Safari from moving the caret to the end of the input when the value has not changed. if (this.getValue() !== value) { this.getNativeInput_().value = value; + this.setCharacterCounter_(value.length); } const isValid = this.isValid(); this.styleValidity_(isValid); diff --git a/test/unit/mdc-textfield/foundation.test.js b/test/unit/mdc-textfield/foundation.test.js index ae118992d37..e1dbbd44006 100644 --- a/test/unit/mdc-textfield/foundation.test.js +++ b/test/unit/mdc-textfield/foundation.test.js @@ -206,6 +206,22 @@ test('#setValue does not affect disabled state', () => { td.verify(mockAdapter.removeClass(cssClasses.INVALID), {times: 1}); }); +test('#setValue updates character counter when present', () => { + const {foundation, mockAdapter, characterCounter} = setupTest({useCharacterCounter: true}); + const nativeInput = { + type: 'text', + value: '', + maxLength: 4, + validity: { + valid: true, + }, + }; + td.when(mockAdapter.getNativeInput()).thenReturn(nativeInput); + + foundation.setValue('ok'); + td.verify(characterCounter.setCounterValue(2, 4), {times: 1}); +}); + test('#isValid for native validation', () => { const {foundation, nativeInput} = setupValueTest({value: '', optIsValid: true}); assert.isOk(foundation.isValid());