Skip to content

Commit 204300f

Browse files
authored
fix: show error when no hints matched (#527)
This pull request include showing error if no hintable elements when following hints.
1 parent b4b10d4 commit 204300f

File tree

2 files changed

+64
-48
lines changed

2 files changed

+64
-48
lines changed

src/background/usecases/HintModeUseCase.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ export class HintModeUseCase {
4444

4545
const hintAction = this.hintActionFactory.createHintAction(hintModeName);
4646
const description = hintAction.description();
47-
await this.consoleClient.showInfo(
48-
tabId,
49-
`${description}: press a key to filter the hints or press Enter to select`,
50-
);
5147

5248
const viewport = await this.topFrameClient.getWindowViewport(tabId);
5349
const hintKeys = new HintTagProducer(hintchars);
@@ -77,6 +73,19 @@ export class HintModeUseCase {
7773
}
7874
}
7975

76+
if (targets.length === 0) {
77+
await this.consoleClient.showError(
78+
tabId,
79+
`${description}: no elements found to select`,
80+
);
81+
return;
82+
}
83+
84+
await this.consoleClient.showInfo(
85+
tabId,
86+
`${description}: press a key to filter the hints or press Enter to select`,
87+
);
88+
8089
await this.hintRepository.startHintMode(
8190
hintModeName,
8291
{ newTab, background },

test/background/usecases/HintModeUseCase.test.ts

+51-44
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,50 @@ describe("HintModeUseCase", () => {
2626
consoleClient,
2727
);
2828

29+
const mockGetFrameIds = vi
30+
.spyOn(frameRepository, "getFrameIds")
31+
.mockResolvedValue([100, 101, 102]);
32+
const mockGetProperty = vi
33+
.spyOn(propertySettings, "getProperty")
34+
.mockResolvedValue("abc");
35+
const mockGetWindowViewport = vi
36+
.spyOn(topFrameClient, "getWindowViewport")
37+
.mockResolvedValue({ width: 1000, height: 1200 });
38+
const mockGetFramePosition = vi
39+
.spyOn(topFrameClient, "getFramePosition")
40+
.mockImplementation((_tabId: number, frameId: number) => {
41+
switch (frameId) {
42+
case 100:
43+
return Promise.resolve({ x: 10, y: 20 });
44+
case 101:
45+
return Promise.resolve({ x: 11, y: 21 });
46+
case 102:
47+
return Promise.resolve({ x: 12, y: 22 });
48+
}
49+
return Promise.resolve(undefined);
50+
});
51+
const mockLookupTargets = vi.spyOn(hintClient, "lookupTargets");
52+
const mockAssignTags = vi.spyOn(hintClient, "assignTags").mockResolvedValue();
53+
const mockStartHintMode = vi
54+
.spyOn(hintRepository, "startHintMode")
55+
.mockResolvedValue(undefined);
56+
const mockShowInfo = vi.spyOn(consoleClient, "showInfo").mockResolvedValue();
57+
const mockShowError = vi
58+
.spyOn(consoleClient, "showError")
59+
.mockResolvedValue();
60+
2961
it("starts follow mode", async () => {
30-
const mockGetFrameIds = vi
31-
.spyOn(frameRepository, "getFrameIds")
32-
.mockResolvedValue([100, 101, 102]);
33-
const mockGetProperty = vi
34-
.spyOn(propertySettings, "getProperty")
35-
.mockResolvedValue("abc");
36-
const mockGetWindowViewport = vi
37-
.spyOn(topFrameClient, "getWindowViewport")
38-
.mockResolvedValue({ width: 1000, height: 1200 });
39-
const mockGetFramePosition = vi
40-
.spyOn(topFrameClient, "getFramePosition")
41-
.mockImplementation((_tabId: number, frameId: number) => {
42-
switch (frameId) {
43-
case 100:
44-
return Promise.resolve({ x: 10, y: 20 });
45-
case 101:
46-
return Promise.resolve({ x: 11, y: 21 });
47-
case 102:
48-
return Promise.resolve({ x: 12, y: 22 });
49-
}
50-
return Promise.resolve(undefined);
51-
});
52-
const mockLookupTargets = vi
53-
.spyOn(hintClient, "lookupTargets")
54-
.mockImplementation((_tabId, frameId: number) => {
55-
switch (frameId) {
56-
case 100:
57-
return Promise.resolve(["0"]);
58-
case 101:
59-
return Promise.resolve(["0", "1"]);
60-
case 102:
61-
return Promise.resolve(["0", "1", "2"]);
62-
}
63-
return Promise.resolve([]);
64-
});
65-
const mockAssignTags = vi
66-
.spyOn(hintClient, "assignTags")
67-
.mockResolvedValue();
68-
const mockStartHintMode = vi
69-
.spyOn(hintRepository, "startHintMode")
70-
.mockResolvedValue(undefined);
71-
const mockShowInfo = vi
72-
.spyOn(consoleClient, "showInfo")
73-
.mockResolvedValue();
62+
mockLookupTargets.mockImplementation((_tabId, frameId: number) => {
63+
switch (frameId) {
64+
case 100:
65+
return Promise.resolve(["0"]);
66+
case 101:
67+
return Promise.resolve(["0", "1"]);
68+
case 102:
69+
return Promise.resolve(["0", "1", "2"]);
70+
}
71+
return Promise.resolve([]);
72+
});
7473

7574
await sut.start(10, "hint.test", false, false);
7675

@@ -126,6 +125,14 @@ describe("HintModeUseCase", () => {
126125
expect(mockShowInfo).toHaveBeenCalledOnce();
127126
});
128127

128+
it("shows error when no hints found", async () => {
129+
mockLookupTargets.mockResolvedValue([]);
130+
131+
await sut.start(10, "hint.test", false, false);
132+
133+
expect(mockShowError).toHaveBeenCalledOnce();
134+
});
135+
129136
it("stops follow mode", async () => {
130137
const mockClearHints = vi
131138
.spyOn(hintClient, "clearHints")

0 commit comments

Comments
 (0)