Skip to content

Commit

Permalink
Remove resetAccountsForTesting from beforeEach (#6370)
Browse files Browse the repository at this point in the history
# Motivation

Since #5724 every Svelte store
(created with `writable`) gets reset automatically before each test.
`resetAccountsForTesting` is a
[function](https://github.com/dfinity/nns-dapp/blob/7c6bc968bf90465bc7f75ef9fd4731b1a81597fd/frontend/src/tests/utils/accounts.test-utils.ts#L114)
that resets 2 stores and does nothing else.
So there is no need to call this in `beforeEach`.

# Changes

1. Remove `resetAccountsForTesting` in `beforeEach` of individual tests.
2. Remove `resetAccountsForTesting` inside tests where it's immediately
followed by `setAccountsForTesting`.

# Tests

Still pass.

# Todos

- [ ] Add entry to changelog (if necessary).
not necessary
  • Loading branch information
dskloetd authored Feb 8, 2025
1 parent 7c6bc96 commit 6e017ea
Show file tree
Hide file tree
Showing 25 changed files with 22 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ import {
} from "$tests/mocks/icp-accounts.store.mock";
import { mockSnsMainAccount } from "$tests/mocks/sns-accounts.mock";
import { mockSnsFullProject } from "$tests/mocks/sns-projects.mock";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { setSnsProjects } from "$tests/utils/sns.test-utils";
import { fireEvent, render, waitFor } from "@testing-library/svelte";

describe("SelectAccountDropdown", () => {
describe("no accounts", () => {
beforeEach(() => {
resetAccountsForTesting();
});

const props = { rootCanisterId: OWN_CANISTER_ID };
it("should render spinner", () => {
const { getByTestId } = render(SelectAccountDropdown, { props });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {
import { mockNeuron } from "$tests/mocks/neurons.mock";
import { NnsAvailableMaturityItemActionPo } from "$tests/page-objects/NnsAvailableMaturityItemAction.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import type { NeuronInfo } from "@dfinity/nns";
import { render } from "@testing-library/svelte";

Expand All @@ -32,7 +29,6 @@ describe("NnsAvailableMaturityItemAction", () => {

beforeEach(() => {
resetIdentity();
resetAccountsForTesting();
});

it("should render available maturity", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import { createMockNeuron, mockNeuron } from "$tests/mocks/neurons.mock";
import { mockRewardEvent } from "$tests/mocks/nns-reward-event.mock";
import { NnsNeuronAdvancedSectionPo } from "$tests/page-objects/NnsNeuronAdvancedSection.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { normalizeWhitespace } from "$tests/utils/utils.test-utils";
import { NeuronState, type NeuronInfo } from "@dfinity/nns";
import { render } from "@testing-library/svelte";
Expand Down Expand Up @@ -47,7 +44,6 @@ describe("NnsNeuronAdvancedSection", () => {
vi.useFakeTimers();
vi.setSystemTime(nowInSeconds * 1000);
resetIdentity();
resetAccountsForTesting();
});

it("should render neuron data", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {
import { mockNeuron } from "$tests/mocks/neurons.mock";
import { NnsNeuronDissolveDelayItemActionPo } from "$tests/page-objects/NnsNeuronDissolveDelayItemAction.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { NeuronState, type NeuronInfo } from "@dfinity/nns";
import { render } from "@testing-library/svelte";

Expand All @@ -39,7 +36,6 @@ describe("NnsNeuronDissolveDelayItemAction", () => {
};

beforeEach(() => {
resetAccountsForTesting();
resetIdentity();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import { mockNetworkEconomics } from "$tests/mocks/network-economics.mock";
import { mockNeuron } from "$tests/mocks/neurons.mock";
import { NnsNeuronPageHeadingPo } from "$tests/page-objects/NnsNeuronPageHeading.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { setIcpPrice } from "$tests/utils/icp-swap.test-utils";
import type { NeuronInfo } from "@dfinity/nns";
import { NeuronType } from "@dfinity/nns";
Expand All @@ -34,7 +31,6 @@ describe("NnsNeuronPageHeading", () => {

beforeEach(() => {
resetIdentity();
resetAccountsForTesting();

setIcpPrice(10);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import { mockNetworkEconomics } from "$tests/mocks/network-economics.mock";
import { mockFullNeuron, mockNeuron } from "$tests/mocks/neurons.mock";
import { NnsNeuronCardPo } from "$tests/page-objects/NnsNeuronCard.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { render } from "$tests/utils/svelte.test-utils";
import type { Neuron } from "@dfinity/nns";
import { NeuronState, NeuronType } from "@dfinity/nns";
Expand All @@ -30,8 +27,6 @@ describe("NnsNeuronCard", () => {
beforeEach(() => {
vi.useFakeTimers().setSystemTime(nowInSeconds * 1000);
resetIdentity();

resetAccountsForTesting();
});

it("renders a Card", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import {
} from "$tests/mocks/icp-accounts.store.mock";
import { ReportingTransactionsPo } from "$tests/page-objects/ReportingTransactions.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { render } from "@testing-library/svelte";

vi.mock("$lib/api/icp-ledger.api");
Expand All @@ -33,7 +30,6 @@ describe("ReportingTransactions", () => {

beforeEach(() => {
resetIdentity();
resetAccountsForTesting();

vi.spyOn(exportToCsv, "generateCsvFileToSave").mockImplementation(() =>
Promise.resolve()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe("ReportingTransactionsButton", () => {

beforeEach(() => {
resetIdentity();
resetAccountsForTesting();

spySaveGeneratedCsv = vi
.spyOn(reportingSaveCsvToFile, "saveGeneratedCsv")
Expand Down Expand Up @@ -212,8 +211,6 @@ describe("ReportingTransactionsButton", () => {
});

it("should fetch transactions for accounts and neurons", async () => {
resetAccountsForTesting();

setAccountsForTesting({
main: mockMainAccount,
});
Expand Down Expand Up @@ -286,7 +283,6 @@ describe("ReportingTransactionsButton", () => {
const beginningOfYearInNanoseconds =
BigInt(beginningOfYear.getTime()) * NANOS_IN_MS;

resetAccountsForTesting();
setAccountsForTesting({
main: mockMainAccount,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import { mockSnsProposal } from "$tests/mocks/sns-proposals.mock";
import { nnsUniverseMock } from "$tests/mocks/universe.mock";
import { SelectUniverseCardPo } from "$tests/page-objects/SelectUniverseCard.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { runResolvedPromises } from "$tests/utils/timers.test-utils";
import type { ProposalInfo } from "@dfinity/nns";
import { Principal } from "@dfinity/principal";
Expand All @@ -41,7 +38,6 @@ describe("SelectUniverseCard", () => {
};

beforeEach(() => {
resetAccountsForTesting();
resetIdentity();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import {
createIcpUserToken,
icpTokenBase,
} from "$tests/mocks/tokens-page.mock";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { setIcpPrice } from "$tests/utils/icp-swap.test-utils";
import { TokenAmountV2 } from "@dfinity/utils";
import { get } from "svelte/store";
Expand Down Expand Up @@ -104,10 +101,6 @@ describe("icp-tokens-list-user.derived", () => {
};

describe("icpTokensListVisitors", () => {
beforeEach(() => {
resetAccountsForTesting();
});

it("should return empty if no accounts", () => {
expect(get(icpTokensListUser)).toEqual([loadingUserTokenData]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ import {
createIcpUserToken,
icpTokenBase,
} from "$tests/mocks/tokens-page.mock";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { setCkETHCanisters } from "$tests/utils/cketh.test-utils";
import { setIcpSwapUsdPrices } from "$tests/utils/icp-swap.test-utils";
import { setSnsProjects } from "$tests/utils/sns.test-utils";
Expand Down Expand Up @@ -213,7 +210,6 @@ describe("tokens-list-user.derived", () => {

describe("tokensListUserStore", () => {
beforeEach(() => {
resetAccountsForTesting();
authStore.setForTesting(mockIdentity);

setSnsProjects([snsTetris, snsPacman]);
Expand Down
9 changes: 1 addition & 8 deletions frontend/src/tests/lib/modals/accounts/ReceiveModal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ import {
import { renderModal } from "$tests/mocks/modal.mock";
import { ReceiveModalPo } from "$tests/page-objects/ReceiveModal.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import type { Principal } from "@dfinity/principal";

describe("ReceiveModal", () => {
const reloadSpy = vi.fn();

beforeEach(() => {
resetAccountsForTesting();
});

const qrCodeLabel = "test QR code";
const logo = "logo";
const logoArialLabel = "logo aria-label";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import { renderModal } from "$tests/mocks/modal.mock";
import { mockNeuron } from "$tests/mocks/neurons.mock";
import { DisburseNnsNeuronModalPo } from "$tests/page-objects/DisburseNnsNeuronModal.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import {
advanceTime,
runResolvedPromises,
Expand Down Expand Up @@ -124,10 +121,6 @@ describe("DisburseNnsNeuronModal", () => {
});

describe("when accounts store is empty", () => {
beforeEach(() => {
resetAccountsForTesting();
});

it("should fetch accounts and render account selector", async () => {
const mainBalanceE8s = 10_000_000n;
let resolveQueryAccount;
Expand Down Expand Up @@ -166,7 +159,6 @@ describe("DisburseNnsNeuronModal", () => {
describe("when no accounts and user navigates away", () => {
let spyQueryAccount: MockInstance;
beforeEach(() => {
resetAccountsForTesting();
const now = Date.now();
vi.useFakeTimers().setSystemTime(now);
const mainBalanceE8s = 10_000_000n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import {
} from "$tests/mocks/icp-accounts.store.mock";
import { renderModal } from "$tests/mocks/modal.mock";
import { mockNeuron } from "$tests/mocks/neurons.mock";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { fireEvent } from "@testing-library/dom";
import { waitFor } from "@testing-library/svelte";

Expand All @@ -35,10 +32,6 @@ describe("IncreaseNeuronStakeModal", () => {
});

describe("when accounts store is empty", () => {
beforeEach(() => {
resetAccountsForTesting();
});

it("should fetch accounts and render account selector", async () => {
const mainBalanceE8s = 10_000_000n;
vi.spyOn(ledgerApi, "queryAccountBalance").mockResolvedValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import {
import { renderModal } from "$tests/mocks/modal.mock";
import { MergeNeuronsModalPo } from "$tests/page-objects/MergeNeuronsModal.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { runResolvedPromises } from "$tests/utils/timers.test-utils";
import { toastsStore } from "@dfinity/gix-components";
import { NeuronState, type NeuronInfo } from "@dfinity/nns";
Expand Down Expand Up @@ -45,7 +42,6 @@ describe("MergeNeuronsModal", () => {
vi.spyOn(authServices, "getAuthenticatedIdentity").mockResolvedValue(
testIdentity
);
resetAccountsForTesting();
});

const selectAndTestTwoNeurons = async ({ po, neurons }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ import { renderModal } from "$tests/mocks/modal.mock";
import { mockFullNeuron, mockNeuron } from "$tests/mocks/neurons.mock";
import { NnsStakeNeuronModalPo } from "$tests/page-objects/NnsStakeNeuronModal.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import {
advanceTime,
runResolvedPromises,
Expand Down Expand Up @@ -520,7 +517,6 @@ describe("NnsStakeNeuronModal", () => {

beforeEach(() => {
neuronsStore.setNeurons({ neurons: [newNeuron], certified: true });
resetAccountsForTesting();
const mainBalanceE8s = 10_000_000n;
vi.spyOn(ledgerApi, "queryAccountBalance").mockResolvedValue(
mainBalanceE8s
Expand Down Expand Up @@ -563,7 +559,6 @@ describe("NnsStakeNeuronModal", () => {
describe("when no accounts and user navigates away", () => {
let spyQueryAccount: MockInstance;
beforeEach(() => {
resetAccountsForTesting();
const now = Date.now();
vi.useFakeTimers().setSystemTime(now);
const mainBalanceE8s = 10_000_000n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ import { rootCanisterIdMock } from "$tests/mocks/sns.api.mock";
import { ParticipateSwapModalPo } from "$tests/page-objects/ParticipateSwapModal.page-object";
import type { TransactionReviewPo } from "$tests/page-objects/TransactionReview.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import {
resetAccountsForTesting,
setAccountsForTesting,
} from "$tests/utils/accounts.test-utils";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import {
advanceTime,
runResolvedPromises,
Expand All @@ -53,7 +50,6 @@ describe("ParticipateSwapModal", () => {
beforeEach(() => {
cancelPollAccounts();
resetIdentity();
resetAccountsForTesting();
snsTicketsStore.setNoTicket(rootCanisterIdMock);

vi.spyOn(snsServices, "getSwapAccount").mockResolvedValue(
Expand Down Expand Up @@ -275,7 +271,6 @@ describe("ParticipateSwapModal", () => {
let resolveQueryAccounts;

beforeEach(() => {
resetAccountsForTesting();
queryAccountBalanceSpy = vi
.spyOn(ledgerApi, "queryAccountBalance")
.mockResolvedValue(mainBalanceE8s);
Expand Down Expand Up @@ -345,7 +340,6 @@ describe("ParticipateSwapModal", () => {
describe("when no accounts and user navigates away", () => {
let spyQueryAccount: MockInstance;
beforeEach(() => {
resetAccountsForTesting();
const now = Date.now();
vi.useFakeTimers().setSystemTime(now);
const mainBalanceE8s = 10_000_000n;
Expand Down
Loading

0 comments on commit 6e017ea

Please sign in to comment.