Skip to content

Commit f5ae8ef

Browse files
author
ClaudiaFang
committed
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).
1 parent 597989b commit f5ae8ef

1 file changed

Lines changed: 16 additions & 31 deletions

File tree

tests/setup.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ export const PluginSettingTab = class {
4141
}
4242
};
4343

44-
export const TextComponent = class {
45-
inputEl: HTMLInputElement;
46-
private changeHandler?: (value: string) => void;
44+
class BaseTextComponent<T extends HTMLInputElement | HTMLTextAreaElement> {
45+
inputEl: T;
46+
protected changeHandler?: (value: string) => void;
4747

48-
constructor(containerEl?: HTMLElement) {
49-
this.inputEl = document.createElement('input');
50-
containerEl?.appendChild(this.inputEl);
48+
constructor(inputEl: T) {
49+
this.inputEl = inputEl;
5150
}
5251

5352
setPlaceholder(value: string) {
@@ -69,35 +68,21 @@ export const TextComponent = class {
6968
this.inputEl.value = value;
7069
this.changeHandler?.(value);
7170
}
72-
};
73-
74-
export const TextAreaComponent = class {
75-
inputEl: HTMLTextAreaElement;
76-
private changeHandler?: (value: string) => void;
71+
}
7772

73+
export const TextComponent = class extends BaseTextComponent<HTMLInputElement> {
7874
constructor(containerEl?: HTMLElement) {
79-
this.inputEl = document.createElement('textarea');
80-
containerEl?.appendChild(this.inputEl);
81-
}
82-
83-
setPlaceholder(value: string) {
84-
this.inputEl.placeholder = value;
85-
return this;
86-
}
87-
88-
setValue(value: string) {
89-
this.inputEl.value = value;
90-
return this;
91-
}
92-
93-
onChange(handler: (value: string) => void) {
94-
this.changeHandler = handler;
95-
return this;
75+
const inputEl = document.createElement('input');
76+
containerEl?.appendChild(inputEl);
77+
super(inputEl);
9678
}
79+
};
9780

98-
triggerChange(value: string) {
99-
this.inputEl.value = value;
100-
this.changeHandler?.(value);
81+
export const TextAreaComponent = class extends BaseTextComponent<HTMLTextAreaElement> {
82+
constructor(containerEl?: HTMLElement) {
83+
const inputEl = document.createElement('textarea');
84+
containerEl?.appendChild(inputEl);
85+
super(inputEl);
10186
}
10287
};
10388

0 commit comments

Comments
 (0)