fix: a second click on Add while the lookup waits tracks the token once (closes #727)
check / check (push) Waiting to run
e2e / e2e-chrome (push) Waiting to run
e2e / e2e-firefox (push) Waiting to run

The "already tracked" check ran only before the contract lookup, so a second click while the first lookup waited tracked the token twice. AddToken and SettingsAddToken now check again when the lookup answers. An answer for a token tracked meanwhile shows the "already tracked" flash message and tracks nothing.

Model: opus-5-5
This commit was merged in pull request #728.
This commit is contained in:
2026-11-03 06:24:14 +01:00
parent 1a4fb0803d
commit 267b69382e
3 changed files with 109 additions and 9 deletions
+83 -6
View File
@@ -1,8 +1,10 @@
// A contract lookup on AddToken can answer after the user has left AddToken.
// The token is still added, and the screen the user is on does not change
// (https://git.eeqj.de/sneak/AutistMask/issues/603). An answer while AddToken
// still shows goes to AddressDetail. Driven through the real popup over a
// stored profile.
// still shows goes to AddressDetail. On either add-token screen, an answer for a
// token tracked while the lookup waited tracks nothing
// (https://git.eeqj.de/sneak/AutistMask/issues/727). Driven through the real
// popup over a stored profile.
const {
bootPopup,
@@ -43,9 +45,9 @@ async function openAddToken() {
return boot;
}
// Presses Add with a contract lookup that answers only when the function
// returned here is called. That function waits for Add to finish.
function addWithPendingLookup(boot) {
// Presses `button` with a contract lookup that answers only when the function
// returned here is called. That function waits for the add to finish.
function addWithPendingLookup(boot, button = "btn-add-token-confirm") {
const { lookupTokenInfo } = require("../src/shared/balances");
let answer;
lookupTokenInfo.mockImplementationOnce(
@@ -54,7 +56,7 @@ function addWithPendingLookup(boot) {
answer = resolve;
}),
);
const adding = boot.click("btn-add-token-confirm");
const adding = boot.click(button);
return async () => {
answer({
symbol: TOKEN.symbol,
@@ -136,3 +138,78 @@ test("an answer while AddToken still shows goes to AddressDetail with the token
expect(boot.node("address-balances").innerHTML).toContain("TST");
expect(boot.storage.read("autistmask").trackedTokens).toEqual([TOKEN]);
});
test("an answer for a token tracked after AddToken was opened again leaves the status line as it is", async () => {
const boot = await openAddToken();
const answerFirstLookup = addWithPendingLookup(boot);
await boot.click("btn-add-token-back");
await boot.click("btn-add-token");
boot.node("add-token-address").value = OTHER_TOKEN_ADDRESS;
// The second lookup is never answered.
addWithPendingLookup(boot);
require("../src/popup/views/helpers").trackToken(TOKEN);
await answerFirstLookup();
expect(boot.text("flash-msg")).toBe("TST is already being tracked.");
expect(boot.text("add-token-info")).toBe("Looking up token...");
});
// Each screen is opened from AddressDetail by the buttons named, in order.
describe.each([
[
"AddToken",
["btn-add-token"],
"add-token-address",
"btn-add-token-confirm",
"add-token-info",
"TST is already being tracked.",
],
[
"SettingsAddToken",
["btn-settings", "btn-settings-add-token"],
"settings-addtoken-address",
"btn-settings-addtoken-manual",
"settings-addtoken-info",
"Already tracked.",
],
])("%s", (_, opening, field, button, statusLine, alreadyTrackedFlash) => {
// The screen open with TOKEN's contract address typed in.
async function openScreen() {
const boot = await bootPopup(
unversionedValidProfile({
currentView: "address",
selectedWallet: 0,
selectedAddress: 0,
}),
);
expect(boot.pageErrors).toEqual([]);
for (const id of opening) await boot.click(id);
boot.node(field).value = TOKEN.address;
return boot;
}
test("a second click on Add while the lookup waits tracks the token once", async () => {
const boot = await openScreen();
const answerFirstClick = addWithPendingLookup(boot, button);
const answerSecondClick = addWithPendingLookup(boot, button);
await answerFirstClick();
await answerSecondClick();
expect(boot.text("flash-msg")).toBe(alreadyTrackedFlash);
expect(boot.storage.read("autistmask").trackedTokens).toEqual([TOKEN]);
});
test("an answer for a token tracked while the lookup waited clears the status line", async () => {
const boot = await openScreen();
const answerLookup = addWithPendingLookup(boot, button);
expect(boot.text(statusLine)).toBe("Looking up token...");
require("../src/popup/views/helpers").trackToken(TOKEN);
await answerLookup();
expect(boot.text("flash-msg")).toBe(alreadyTrackedFlash);
expect(boot.text(statusLine)).toBe("");
expect(boot.node(statusLine).style.visibility).toBe("hidden");
const { state } = require("../src/shared/state");
expect(state.trackedTokens).toEqual([TOKEN]);
});
});