Skip to content

Commit

Permalink
Mock proposals API instead of GovernanceCanister in NnsProposals.spec…
Browse files Browse the repository at this point in the history
….ts (#5399)

# Motivation

We normally mock the API layer unless we are testing the API.
`NnsProposals.spec.ts` was mocking the canister and even had a TODO not
to do that.

# Changes

Mock `proposalsApi.queryProposals` instead of `GovernanceCanister`.

# Tests

Test only change.

# Todos

- [ ] Add entry to changelog (if necessary).
not necessary
  • Loading branch information
dskloetd committed Sep 3, 2024
1 parent c041085 commit 4f482c3
Showing 1 changed file with 3 additions and 45 deletions.
48 changes: 3 additions & 45 deletions frontend/src/tests/lib/pages/NnsProposals.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resetNeuronsApiService } from "$lib/api-services/governance.api-service";
import * as agent from "$lib/api/agent.api";
import * as governanceApi from "$lib/api/governance.api";
import * as proposalsApi from "$lib/api/proposals.api";
import { DEFAULT_PROPOSALS_FILTERS } from "$lib/constants/proposals.constants";
import { ACTIONABLE_PROPOSALS_PARAM } from "$lib/constants/routes.constants";
import NnsProposals from "$lib/pages/NnsProposals.svelte";
Expand All @@ -18,7 +18,6 @@ import {
mockAuthStoreSubscribe,
mockIdentity,
} from "$tests/mocks/auth.store.mock";
import { MockGovernanceCanister } from "$tests/mocks/governance.canister.mock";
import {
mockEmptyProposalsStoreSubscribe,
mockProposals,
Expand All @@ -28,11 +27,9 @@ import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import { NnsProposalListPo } from "$tests/page-objects/NnsProposalList.page-object";
import { render } from "$tests/utils/svelte.test-utils";
import { runResolvedPromises } from "$tests/utils/timers.test-utils";
import type { HttpAgent } from "@dfinity/agent";
import { GovernanceCanister, type ProposalInfo } from "@dfinity/nns";
import type { ProposalInfo } from "@dfinity/nns";
import { waitFor } from "@testing-library/svelte";
import type { Subscriber } from "svelte/store";
import { mock } from "vitest-mock-extended";

vi.mock("$lib/api/governance.api");

Expand All @@ -56,16 +53,9 @@ describe("NnsProposals", () => {
neuronsStore.reset();
proposalsFiltersStore.reset();
actionableProposalsSegmentStore.resetForTesting();
vi.spyOn(agent, "createAgent").mockResolvedValue(mock<HttpAgent>());

vi.spyOn(authStore, "subscribe").mockImplementation(mockAuthStoreSubscribe);

// TODO: Mock the canister call instead of the canister itself
const mockGovernanceCanister: MockGovernanceCanister =
new MockGovernanceCanister([]);
vi.spyOn(GovernanceCanister, "create").mockReturnValue(
mockGovernanceCanister
);
vi.spyOn(proposalsApi, "queryProposals").mockResolvedValue([]);
});

describe("logged in user", () => {
Expand Down Expand Up @@ -99,16 +89,9 @@ describe("NnsProposals", () => {
describe("Matching results", () => {
beforeEach(() => {
overrideFeatureFlagsStore.reset();
const mockGovernanceCanister: MockGovernanceCanister =
new MockGovernanceCanister(mockProposals);

vi.spyOn(proposalsStore, "subscribe").mockImplementation(
mockProposalsStoreSubscribe
);
vi.spyOn(GovernanceCanister, "create").mockImplementation(
(): GovernanceCanister => mockGovernanceCanister
);

vi.spyOn(governanceApi, "queryNeurons").mockResolvedValue([]);
actionableProposalsSegmentStore.set("all");
});
Expand Down Expand Up @@ -216,14 +199,7 @@ describe("NnsProposals", () => {
});

describe("No results", () => {
const mockGovernanceCanister: MockGovernanceCanister =
new MockGovernanceCanister([]);

beforeEach(() => {
vi.spyOn(GovernanceCanister, "create").mockImplementation(
(): GovernanceCanister => mockGovernanceCanister
);

vi.spyOn(governanceApi, "queryNeurons").mockResolvedValue([]);
actionableProposalsSegmentStore.set("all");
});
Expand Down Expand Up @@ -254,15 +230,6 @@ describe("NnsProposals", () => {
});

describe("neurons", () => {
beforeEach(() => {
// TODO: Mock the canister call instead of the canister itself
const mockGovernanceCanister: MockGovernanceCanister =
new MockGovernanceCanister([]);
vi.spyOn(GovernanceCanister, "create").mockReturnValue(
mockGovernanceCanister
);
});

it("should NOT load neurons", async () => {
await renderComponent();

Expand All @@ -273,20 +240,11 @@ describe("NnsProposals", () => {
});

describe("Matching results", () => {
const mockGovernanceCanister: MockGovernanceCanister =
new MockGovernanceCanister(mockProposals);

const mockLoadProposals = () =>
vi
.spyOn(proposalsStore, "subscribe")
.mockImplementation(mockProposalsStoreSubscribe);

beforeEach(() => {
vi.spyOn(GovernanceCanister, "create").mockImplementation(
(): GovernanceCanister => mockGovernanceCanister
);
});

it("should render proposals", async () => {
mockLoadProposals();

Expand Down

0 comments on commit 4f482c3

Please sign in to comment.