Skip to content

Commit

Permalink
fix: validate plainText option asynchronously (#4261)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart authored Nov 20, 2023
1 parent 5ea64af commit 24353d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
16 changes: 15 additions & 1 deletion components/tag-list/tag-list-item-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const TagListItemMixin = superclass => class extends LocalizeCoreElement(
this.keyboardTooltipShown = false;
this._id = getUniqueId();
this._plainText = '';
this._validatingPlainTextTimeout = null;
}

firstUpdated(changedProperties) {
Expand Down Expand Up @@ -216,7 +217,7 @@ export const TagListItemMixin = superclass => class extends LocalizeCoreElement(
}

_renderTag(tagContent, options = {}) {
if (options.plainText?.constructor !== String) throw new TypeError('options.plainText must be a string');
this._validatePlainText();
this._plainText = options.plainText || '';

const buttonText = this.localize('components.tag-list.clear', { value: this._plainText });
Expand Down Expand Up @@ -276,4 +277,17 @@ export const TagListItemMixin = superclass => class extends LocalizeCoreElement(
`;
}

_validatePlainText() {
clearTimeout(this._validatingPlainTextTimeout);
// don't error immediately in case it doesn't get set immediately
this._validatingPlainTextTimeout = setTimeout(() => {
this._validatingPlainTextTimeout = null;
if (!this.isConnected) return;
const hasPlainText = (this._plainText?.constructor === String) && this._plainText?.length > 0;
if (!hasPlainText) {
throw new Error(`TagListItemMixin: "${this.tagName.toLowerCase()}" called "_render()" with empty "plainText" option`);
}
}, 3000);
}

};
11 changes: 0 additions & 11 deletions components/tag-list/test/tag-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,5 @@ describe('d2l-tag-list-item-mixin-consumer', () => {
const child = elem.children[3];
expect(child._plainText).to.be.equal('Tag');
});

it('should error if not provided', async() => {
let error;
try {
await fixture(html`<d2l-tag-list-item-mixin-consumer></d2l-tag-list-item-mixin-consumer>`);
} catch (e) {
error = e;
}
expect(error?.name).to.equal('TypeError');
});

});
});
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 24353d4

Please sign in to comment.