refactor(tests): dedupe TextComponent/TextAreaComponent mocks

Extract shared setPlaceholder/setValue/onChange/triggerChange logic
into a BaseTextComponent<T> generic to fix SonarCloud's new-code
duplication gate on PR #49 (8.3% > 3% threshold).
This commit is contained in:
ClaudiaFang 2026-07-13 12:33:11 +00:00
parent 597989b53c
commit f5ae8ef16d

View file

@ -41,13 +41,12 @@ export const PluginSettingTab = class {
} }
}; };
export const TextComponent = class { class BaseTextComponent<T extends HTMLInputElement | HTMLTextAreaElement> {
inputEl: HTMLInputElement; inputEl: T;
private changeHandler?: (value: string) => void; protected changeHandler?: (value: string) => void;
constructor(containerEl?: HTMLElement) { constructor(inputEl: T) {
this.inputEl = document.createElement('input'); this.inputEl = inputEl;
containerEl?.appendChild(this.inputEl);
} }
setPlaceholder(value: string) { setPlaceholder(value: string) {
@ -69,35 +68,21 @@ export const TextComponent = class {
this.inputEl.value = value; this.inputEl.value = value;
this.changeHandler?.(value); this.changeHandler?.(value);
} }
}
export const TextComponent = class extends BaseTextComponent<HTMLInputElement> {
constructor(containerEl?: HTMLElement) {
const inputEl = document.createElement('input');
containerEl?.appendChild(inputEl);
super(inputEl);
}
}; };
export const TextAreaComponent = class { export const TextAreaComponent = class extends BaseTextComponent<HTMLTextAreaElement> {
inputEl: HTMLTextAreaElement;
private changeHandler?: (value: string) => void;
constructor(containerEl?: HTMLElement) { constructor(containerEl?: HTMLElement) {
this.inputEl = document.createElement('textarea'); const inputEl = document.createElement('textarea');
containerEl?.appendChild(this.inputEl); containerEl?.appendChild(inputEl);
} super(inputEl);
setPlaceholder(value: string) {
this.inputEl.placeholder = value;
return this;
}
setValue(value: string) {
this.inputEl.value = value;
return this;
}
onChange(handler: (value: string) => void) {
this.changeHandler = handler;
return this;
}
triggerChange(value: string) {
this.inputEl.value = value;
this.changeHandler?.(value);
} }
}; };