mirror of
https://github.com/firstsun-dev/git-files-sync.git
synced 2026-07-22 17:20:30 +00:00
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:
parent
597989b53c
commit
f5ae8ef16d
1 changed files with 17 additions and 32 deletions
|
|
@ -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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue