Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always vi.restoreAllMocks() before each test #5788

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import * as canistersServices from "$lib/services/canisters.services";
import { detachCanister } from "$lib/services/canisters.services";
import { fireEvent, render } from "@testing-library/svelte";
import UnlinkActionButtonTest from "./UnlinkActionButtonTest.svelte";

vitest.mock("$lib/services/canisters.services", () => {
return {
detachCanister: vitest.fn().mockResolvedValue({ success: true }),
};
});

describe("DissolveActionButton", () => {
beforeEach(() => {
vitest.clearAllMocks();
vi.spyOn(canistersServices, "detachCanister").mockResolvedValue({
success: true,
});
});

it("renders button", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FollowSnsTopicSection from "$lib/components/sns-neuron-detail/FollowSnsTopicSection.svelte";
import * as snsNeuronsServices from "$lib/services/sns-neurons.services";
import { removeFollowee } from "$lib/services/sns-neurons.services";
import { shortenWithMiddleEllipsis } from "$lib/utils/format.utils";
import { getSnsNeuronIdAsHexString } from "$lib/utils/sns-neuron.utils";
Expand All @@ -13,10 +14,6 @@ import type { SnsNeuron } from "@dfinity/sns";
import { fireEvent, waitFor, type RenderResult } from "@testing-library/svelte";
import type { SvelteComponent } from "svelte";

vi.mock("$lib/services/sns-neurons.services", () => ({
removeFollowee: vi.fn().mockReturnValue({ success: true }),
}));

describe("FollowSnsTopicSection", () => {
const reload = vi.fn();
const followee1 = createMockSnsNeuron({ id: [1, 2, 3] });
Expand All @@ -27,6 +24,12 @@ describe("FollowSnsTopicSection", () => {
followees: [[nervousSystemFunctionMock.id, { followees }]],
};

beforeEach(() => {
vi.spyOn(snsNeuronsServices, "removeFollowee").mockResolvedValue({
success: true,
});
});

const renderComponent = (): RenderResult<SvelteComponent> =>
renderSelectedSnsNeuronContext({
Component: FollowSnsTopicSection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { icpAccountsStore } from "$lib/derived/icp-accounts.derived";
import IcpTransactionModal from "$lib/modals/accounts/IcpTransactionModal.svelte";
import * as icpAccountsServices from "$lib/services/icp-accounts.services";
import { transferICP } from "$lib/services/icp-accounts.services";
import { resetIdentity } from "$tests/mocks/auth.store.mock";
import {
Expand All @@ -12,12 +13,6 @@ import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import { queryToggleById } from "$tests/utils/toggle.test-utils";
import { fireEvent, waitFor } from "@testing-library/svelte";

vi.mock("$lib/services/icp-accounts.services", () => {
return {
transferICP: vi.fn().mockResolvedValue({ success: true }),
};
});

describe("IcpTransactionModal", () => {
const renderTransactionModal = () =>
renderModal({
Expand All @@ -34,6 +29,9 @@ describe("IcpTransactionModal", () => {
beforeEach(() => {
resetIdentity();

vi.spyOn(icpAccountsServices, "transferICP").mockResolvedValue({
success: true,
});
vi.spyOn(icpAccountsStore, "subscribe").mockImplementation(
mockAccountsStoreSubscribe([mockSubAccount])
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ledgerApi from "$lib/api/icp-ledger.api";
import * as nnsDappApi from "$lib/api/nns-dapp.api";
import IncreaseNeuronStakeModal from "$lib/modals/neurons/IncreaseNeuronStakeModal.svelte";
import * as neuronsServices from "$lib/services/neurons.services";
import { topUpNeuron } from "$lib/services/neurons.services";
import { resetIdentity } from "$tests/mocks/auth.store.mock";
import {
Expand All @@ -16,14 +17,6 @@ import {
import { fireEvent } from "@testing-library/dom";
import { waitFor } from "@testing-library/svelte";

vi.mock("$lib/api/nns-dapp.api");
vi.mock("$lib/api/icp-ledger.api");
vi.mock("$lib/services/neurons.services", () => {
return {
topUpNeuron: vi.fn().mockResolvedValue({ success: true }),
};
});

describe("IncreaseNeuronStakeModal", () => {
const renderTransactionModal = () =>
renderModal({
Expand All @@ -35,6 +28,10 @@ describe("IncreaseNeuronStakeModal", () => {

beforeEach(() => {
resetIdentity();

vi.spyOn(neuronsServices, "topUpNeuron").mockResolvedValue({
success: true,
});
});

describe("when accounts store is empty", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AppPath } from "$lib/constants/routes.constants";
import SnsIncreaseStakeNeuronModal from "$lib/modals/sns/neurons/SnsIncreaseStakeNeuronModal.svelte";
import * as snsAccountsServices from "$lib/services/sns-accounts.services";
import * as snsNeuronsServices from "$lib/services/sns-neurons.services";
import { increaseStakeNeuron } from "$lib/services/sns-neurons.services";
import { icrcAccountsStore } from "$lib/stores/icrc-accounts.store";
import { page } from "$mocks/$app/stores";
Expand All @@ -22,25 +24,6 @@ import { ICPToken } from "@dfinity/utils";
import { fireEvent, waitFor, type RenderResult } from "@testing-library/svelte";
import type { SvelteComponent } from "svelte";

vi.mock("$lib/services/sns-neurons.services", () => {
return {
increaseStakeNeuron: vi.fn().mockResolvedValue({ success: true }),
};
});

vi.mock("$lib/services/sns-accounts.services", () => {
return {
loadSnsAccounts: vi.fn().mockResolvedValue(undefined),
};
});

vi.mock("$lib/stores/busy.store", () => {
return {
startBusy: vi.fn(),
stopBusy: vi.fn(),
};
});

describe("SnsIncreaseStakeNeuronModal", () => {
const reloadNeuron = vi.fn();
const rootCanisterId = mockPrincipal;
Expand Down Expand Up @@ -77,6 +60,13 @@ describe("SnsIncreaseStakeNeuronModal", () => {
data: { universe: rootCanisterId.toText() },
});
setSnsProjects([snsProjectParams]);

vi.spyOn(snsNeuronsServices, "increaseStakeNeuron").mockResolvedValue({
success: true,
});
vi.spyOn(snsAccountsServices, "loadSnsAccounts").mockResolvedValue(
undefined
);
});

describe("accounts and params are loaded", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SnsStakeMaturityModal from "$lib/modals/sns/neurons/SnsStakeMaturityModal.svelte";
import * as snsNeuronsServices from "$lib/services/sns-neurons.services";
import { stakeMaturity } from "$lib/services/sns-neurons.services";
import { formattedMaturity } from "$lib/utils/sns-neuron.utils";
import { mockPrincipal } from "$tests/mocks/auth.store.mock";
Expand All @@ -8,15 +9,15 @@ import { selectPercentage } from "$tests/utils/neurons-modal.test-utils";
import { fireEvent, waitFor, type RenderResult } from "@testing-library/svelte";
import type { SvelteComponent } from "svelte";

vi.mock("$lib/services/sns-neurons.services", () => {
return {
stakeMaturity: vi.fn().mockResolvedValue({ success: true }),
};
});

describe("SnsStakeMaturityModal", () => {
const reloadNeuron = vi.fn();

beforeEach(() => {
vi.spyOn(snsNeuronsServices, "stakeMaturity").mockResolvedValue({
success: true,
});
});

const renderSnsStakeMaturityModal = async (): Promise<
RenderResult<SvelteComponent>
> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { selectedUniverseIdStore } from "$lib/derived/selected-universe.derived";
import { snsSelectedTransactionFeeStore } from "$lib/derived/sns/sns-selected-transaction-fee.store";
import SnsStakeNeuronModal from "$lib/modals/sns/neurons/SnsStakeNeuronModal.svelte";
import * as snsNeuronsServices from "$lib/services/sns-neurons.services";
import { stakeNeuron } from "$lib/services/sns-neurons.services";
import { icrcAccountsStore } from "$lib/stores/icrc-accounts.store";
import { page } from "$mocks/$app/stores";
Expand All @@ -17,12 +18,6 @@ import type { Principal } from "@dfinity/principal";
import { TokenAmount } from "@dfinity/utils";
import type { Subscriber } from "svelte/store";

vi.mock("$lib/services/sns-neurons.services", () => {
return {
stakeNeuron: vi.fn().mockResolvedValue({ success: true }),
};
});

describe("SnsStakeNeuronModal", () => {
const ledgerCanisterId = principal(3);
const token = { name: "POP", symbol: "POP", decimals: 8 };
Expand Down Expand Up @@ -59,6 +54,9 @@ describe("SnsStakeNeuronModal", () => {
},
});

vi.spyOn(snsNeuronsServices, "stakeNeuron").mockResolvedValue({
success: true,
});
vi.spyOn(snsSelectedTransactionFeeStore, "subscribe").mockImplementation(
mockSnsSelectedTransactionFeeStoreSubscribe()
);
Expand Down
10 changes: 10 additions & 0 deletions frontend/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ import {
setDefaultTestConstants,
} from "./src/tests/utils/mockable-constants.test-utils";

// Restore all mocks original behvior before each test.
//
// NOTE: This restores mocks created with vi.spyOn() to their production
// behavior, but returns mocks created on modules with vi.mock() to mocks that
// return undefined. Regardless, it will make sure that each test starts with
// the same behavior for all mocks.
beforeEach(() => {
vi.restoreAllMocks();
});

// Reset every store before each test.
const resetStoreFunctions = vi.hoisted(() => {
return [];
Expand Down